Slides15 - Vernon Computer Science

advertisement
Layout of Chapter 15
This final chapter is special and focuses primarily
on graphics.
There are no homework exercises, quizzes or
tests with this chapter.
However, there are 2 MAJOR Lab Projects based
on the information in this chapter.
The first half of the chapter focuses on skills
needed for the first project. The second half
focuses on skills needed for the Final Project.
Ignore Deprecated API
Warning Messages
Java is constantly improving. When newer and improved classes
become available, the older classes are officially labeled “deprecated”.
When you use a deprecated class, you get a warning message as seen
below. Warnings are not the same thing as errors. Warnings can be
ignored. We are choosing to use the older technique for mouse
interaction because it is simpler. The newer technique may be
preferred, but it requires knowledge of AP Computer Science topics.
// Java1501.java
// This program counts the number of times a mouse is clicked.
// Both left-clicks and right-clicks are counted using the <mouseDown> event.
// Ignore the "deprecated API" warning.
import java.applet.Applet;
import java.awt.*;
public class Java1501 extends Applet
{
int numClicks;
public void init() { numClicks = 0; }
public void paint(Graphics g)
{
g.drawString("Mouse is clicked " + numClicks + " times.",20,20);
}
public boolean mouseDown(Event e, int x, int y)
{
numClicks++;
repaint();
return true;
}
}
// Java1502.java
// This program displays the position of the mouse every time it is clicked
// using the <mouseDown> method.
import java.applet.Applet;
import java.awt.*;
public class Java1502 extends Applet
{
int xCoord, yCoord;
public void paint(Graphics g)
{
g.drawString("Mouse clicked at (" + xCoord + "," + yCoord + ")",20,20);
}
public boolean mouseDown(Event e, int x, int y)
{
xCoord = x;
yCoord = y;
repaint();
return true;
}
}
// Java1503.java
// This program demonstrates how to determine if the mouse is inside or
// outside the Applet window using the <mouseEnter> and <mouseExit> methods.
import java.applet.Applet;
import java.awt.*;
public class Java1503 extends Applet
{
boolean insideApplet;
public void paint(Graphics g)
{
if (insideApplet)
g.drawString("Mouse is inside applet",20,20);
else
g.drawString("Mouse is outside applet",20,20);
}
public boolean mouseEnter(Event e, int x, int y)
{
insideApplet = true;
repaint();
return true;
}
public boolean mouseExit(Event e, int x, int y)
{
insideApplet = false;
repaint();
return true;
}
}
// Java1504.java
// This program determines if a mouse is clicked once or twice using the
// <clickCount> method. This method works for the left or right button.
boolean singleClick,doubleClick;
public void paint(Graphics g)
{
if (singleClick)
g.drawString("Mouse was clicked once",20,20);
if (doubleClick)
g.drawString("Mouse was clicked twice",20,20);
}
public boolean mouseDown(Event e, int x, int y)
{
switch (e.clickCount)
{
case 1:
singleClick = true;
doubleClick = false;
break;
case 2:
doubleClick = true;
singleClick = false;
}
repaint();
return true;
}
// Java1505.java
// This program displays the position of the mouse every time it moves
// using the <mouseMove> method.
import java.applet.Applet;
import java.awt.*;
public class Java1505 extends Applet
{
int xCoord, yCoord;
public void paint(Graphics g)
{
g.drawString("Mouse is located at (" + xCoord + "," + yCoord + ")",20,20);
}
public boolean mouseMove(Event e, int x, int y)
{
xCoord = x;
yCoord = y;
repaint();
return true;
}
}
// Java1506.java
// This program demonstrates how display to a single graphics image from a .gif file
// at any x,y location on the screen. The same image can be displayed multiple times.
import java.awt.*;
public class Java1506 extends java.applet.Applet
{
Image picture;
public void init()
{
picture = getImage(getDocumentBase(),"LSchram.gif");
}
public void paint(Graphics g)
{
g.drawImage(picture,0,0,this);
g.drawImage(picture,750,200,this);
g.drawImage(picture,350,450,this);
}
}
// Java1507.java
// This program demonstrates that several different images can be displayed.
import java.awt.*;
public class Java1507 extends java.applet.Applet
{
Image picture1, picture2, picture3, picture4, picture5, picture6;
public void init()
{
picture1 = getImage(getDocumentBase(),"Knight.gif");
picture2 = getImage(getDocumentBase(),"MouseWithCheese.gif");
picture3 = getImage(getDocumentBase(),"smile.gif");
picture4 = getImage(getDocumentBase(),"shark.gif");
picture5 = getImage(getDocumentBase(),"Spider.gif");
picture6 = getImage(getDocumentBase(),"TEDDY.gif");
}
public void paint(Graphics g)
{
g.drawImage(picture1,100,100,this);
g.drawImage(picture2,400,100,this);
g.drawImage(picture3,700,100,this);
g.drawImage(picture4,100,400,this);
g.drawImage(picture5,400,400,this);
g.drawImage(picture6,700,400,this);
}
}
// Java1508.java
// This program demonstrates that animated gifs are inserted in the same way as
// normals gifs. Animated gifs can cause a flickering side-effect on the screen.
import java.awt.*;
public class Java1507 extends java.applet.Applet
{
Image picture1, picture2, picture3, picture4, picture5, picture6;
public void init()
{
picture1 = getImage(getDocumentBase(),"space.gif");
picture2 = getImage(getDocumentBase(),"computer.gif");
picture3 = getImage(getDocumentBase(),"gears.gif");
picture4 = getImage(getDocumentBase(),"butterfly.gif");
picture5 = getImage(getDocumentBase(),"pizza.gif");
picture6 = getImage(getDocumentBase(),"jet.gif");
}
public void paint(Graphics g)
{
g.drawImage(picture1,100,100,this);
g.drawImage(picture2,450,100,this);
g.drawImage(picture3,700,100,this);
g.drawImage(picture4,100,400,this);
g.drawImage(picture5,400,350,this);
g.drawImage(picture6,700,400,this);
}
}
// Java1509.java
// This program demonstrates that .png files also work.
import java.awt.*;
public class Java1509 extends java.applet.Applet
{
Image picture1, picture2, picture3, picture4, picture5, picture6;
public void init()
{
picture1 = getImage(getDocumentBase(),"Bunny.png");
picture2 = getImage(getDocumentBase(),"pineapple.png");
picture3 = getImage(getDocumentBase(),"Penguin.png");
picture4 = getImage(getDocumentBase(),"Koala.png");
picture5 = getImage(getDocumentBase(),"house.png");
picture6 = getImage(getDocumentBase(),"bird.png");
}
public void paint(Graphics g)
{
g.drawImage(picture1,100,100,this);
g.drawImage(picture2,400,100,this);
g.drawImage(picture3,700,100,this);
g.drawImage(picture4,100,400,this);
g.drawImage(picture5,400,400,this);
g.drawImage(picture6,700,400,this);
}
}
// Java1510.java
// This program demonstrates .jpg files can be used as well.
// .jpg files are typically used for photographs.
import java.awt.*;
public class Java1510 extends java.applet.Applet
{
Image picture1, picture2, picture3, picture4;
public void init()
{
picture1 = getImage(getDocumentBase(),"Paris.jpg");
picture2 = getImage(getDocumentBase(),"LetSleepingDogsLie.jpg");
picture3 = getImage(getDocumentBase(),"AllSmiling.jpg");
picture4 = getImage(getDocumentBase(),"Schrams.jpg");
}
public void paint(Graphics g)
{
g.drawImage(picture1,0,0,this);
g.drawImage(picture2,500,0,this);
g.drawImage(picture3,0,330,this);
g.drawImage(picture4,316,330,this);
}
}
// Java1511.java
// Problem #1 -- The image file has the wrong format.
// This program demonstrates that .bmp files cannot be displayed.
// Nothing will show up.
// To fix this, load the bitmap file in Paint and resave it as a .gif,
// .png or .jpg file.
import java.awt.*;
public class Java1511 extends java.applet.Applet
{
Image picture;
public void init()
{
picture = getImage(getDocumentBase(),"ShortCircuit.bmp");
}
public void paint(Graphics g)
{
g.drawImage(picture,100,100,this);
}
}
Fixing the Problem
Load
ShortCircut.bmp
in Microsoft Paint.
If double-clicking
the file does not
work, right click
the file, select
“Open with”
and then select
“Paint”.
Click this
tab.
Click “Save as”.
Now select PNG,
JPEG or GIF.
Click Save.
// Java1511.java
// Problem #1 -- The image file has the wrong format.
// This program demonstrates that .bmp files cannot be displayed.
// Nothing will show up.
Return
toandJCreator.
// To fix this, load the bitmap
file in Paint
resave it as a .gif,
// .png or .jpg file.
Remember to alter the name
import java.awt.*;
of the file so it matches the
public class Java1511 extends
java.applet.Applet
name
of
the
new
file
that
you
{
just created. Execute again.
Image picture;
public void init()
{
picture = getImage(getDocumentBase(),"ShortCircuit.png");
}
public void paint(Graphics g)
{
g.drawImage(picture,100,100,this);
}
}
// Java1512.java
// Problem #2 -- The image file is not in the correct location.
// This program demonstrates that an image cannot be displayed if it is not in the
// correct folder. When you execute the program, nothing is displayed. Move the
// "DallasSkyline.png" file from the "Dallas" subfolder to the "Programs15" folder
// and execute the program again. After the file is moved, the program should work.
import java.awt.*;
public class Java1512 extends java.applet.Applet
{
Image picture;
public void init()
{
picture = getImage(getDocumentBase(),"DallasSkyline.png");
}
public void paint(Graphics g)
{
g.drawImage(picture,0,100,this);
}
}
Fixing the Problem
The problem
is that the
image file is
in the Dallas
subfolder
and not in
the
Programs15
folder with
the rest of
the files.
Open the
Dallas
subfolder.
Right-click this file and select “Copy”.
Return to the Programs15 folder and paste the image file.
// Java1512.java
// Problem #2 -- The image file is not in the correct location.
// This program demonstrates that an image cannot be displayed if it is not in the
// correct folder. When you execute the program, nothing is displayed. Move the
// "DallasSkyline.png" file from the "Dallas" subfolder to the "Programs15" folder
// and execute the program again. After the file is moved, the program should work.
Return to JCreator.
public class Java1512 extends
java.applet.Applet
Execute
the program again.
{
This time no alterations to
Image picture;
the program are necessary.
public void init()
import java.awt.*;
{
picture = getImage(getDocumentBase(),"DallasSkyline.png");
}
public void paint(Graphics g)
{
g.drawImage(picture,0,100,this);
}
}
Images in the Viewer
are Bigger than They Appear
If you double click the
file castillo.JPG it
might show up in a
picture viewer like the
one on the right.
This is NOT the actual
size of the picture.
The true size of the
picture is
demonstrated in the
next program.
// Java1513.java
// Problem #3 -- The image file is too big.
// This program will be used to demonstrate what to do when a picture is too big.
import java.awt.*;
public class Java1513 extends java.applet.Applet
{
Image picture;
public void init()
{
picture = getImage(getDocumentBase(),"castillo.jpg");
}
public void paint(Graphics g)
{
Expo.setFont(g,"Snap ITC",Font.PLAIN,48);
Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);
g.drawImage(picture,0,100,this);
}
}
Load the picture in Paint and click Resize.
Select Pixels and enter 550 for vertical.
Since “Maintain aspect ratio” is
selected the Horizontal value
will adjust automatically after
you change the vertical.
You can also change the picture
size by entering a Percentage.
50 percent would make the
picture have half the length and
half the width.
Press OK and save the resized picture.
Do NOT close this file!
We will use it again soon.
// Java1513.java
// Problem #3 -- The image file is too big.
// This program will be used to demonstrate what to do when a picture is too big.
import java.awt.*;
Return to JCreator.
public class Java1513 extends java.applet.Applet
{
Execute the program
Image picture;
again.
No alterations are necessary.
public void init()
{
picture = getImage(getDocumentBase(),"castillo.jpg");
}
public void paint(Graphics g)
{
Expo.setFont(g,"Snap ITC",Font.PLAIN,48);
Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);
g.drawImage(picture,0,100,this);
}
}
Solution #2 – Specify Image Dimensions
Return to Paint.
Hit <Control>-<Z> to
“Undo” the resizing.
Re-save the image.
// Java1513.java
// Problem #3 -- The image file is too big.
// This program will be used to demonstrate what to do when a picture is too big.
import java.awt.*;
Return to JCreator. Add the
below to
the drawImage command.
public void init()
Re-compile and execute.
{
The picture will display ¼ its
picture = getImage(getDocumentBase(),"castillo.jpg");
}
actual size of 3008 by 2000.
public class Java1513 extends java.applet.Applet
{
2 numbers shown
Image picture;
public void paint(Graphics g)
{
Expo.setFont(g,"Snap ITC",Font.PLAIN,48);
Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);
g.drawImage(picture,0,100,752,500,this);
}
}
Actual Size:
3008 by 2000
3008 / 4 = 752
2000 / 4 = 500
With dimensions
of 752 by 500 the
image is ¼ the
width and ¼ the
height or 1/16 the
total area.
// Java1513.java
// Problem #3 -- The image file is too big.
// This program will be used to demonstrate what to do when a picture is too big.
import java.awt.*;
Change the 2 numbers in
command.
Re-compile and execute.
public void init()
The picture will display ⅛ its
{
actual size of 3008 by 2000.
picture = getImage(getDocumentBase(),"castillo.jpg");
public class Java1513 extends java.applet.Applet
{
the drawImage
Image picture;
}
public void paint(Graphics g)
{
Expo.setFont(g,"Snap ITC",Font.PLAIN,48);
Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);
g.drawImage(picture,0,100,376,250,this);
}
}
Actual Size:
3008 by 2000
3008 / 8 = 376
2000 / 8 = 250
With dimensions
of 376 by 250 the
image is ⅛ the
width and ⅛ the
height or 1/64 the
total area.
// Java1513.java
// Problem #3 -- The image file is too big.
// This program will be used to demonstrate what to do when a picture is too big.
import java.awt.*;
st number in
Change
only
the
1
public class Java1513 extends java.applet.Applet
{
the drawImage command.
Image picture;
Re-compile and execute.
public void init()
What happened to the output?
{
picture = getImage(getDocumentBase(),"castillo.jpg");
}
public void paint(Graphics g)
{
Expo.setFont(g,"Snap ITC",Font.PLAIN,48);
Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);
g.drawImage(picture,0,100,1000,250,this);
}
}
Actual Size:
3008 by 2000
Display Size:
1000 by 250
The display dimensions are not
proportional to the original dimensions.
The result is that the image gets warped.
Solution #3 – Crop the Image
Return to Paint.
Use the scroll bars to find this image:
Use the Select tool to outline what you want
Click the Copy icon.
Click the
icon and create a new file.
Click Paste.
DO NOT SAVE YET!
All of this extra white space is
currently part of the picture.
You need to click the bottom right
corner and drag it toward the picture.
Now you can save.
Save this file as castillo.JPG,
return to JCreator and execute
the program again.
// Java1513.java
// Problem #3 -- The image file is too big.
// This program will be used to demonstrate what to do when a picture is too big.
import java.awt.*;
Return to JCreator. Remove
public class Java1513 extendsthe
java.applet.Applet
extra numbers from the
{
drawImage command so
Image picture;
that it will display the image
public void init()
actual size.
{
picture = getImage(getDocumentBase(),"castillo.jpg");
Re-compile and execute.
}
public void paint(Graphics g)
{
Expo.setFont(g,"Snap ITC",Font.PLAIN,48);
Expo.drawString(g,"El Castillo de San Felipe del Morro",15,60);
g.drawImage(picture,0,100,this);
}
}
Graphics Editors
There are many Graphics Editors, any of
which could be used to edit these images.
Some are simplistic like Microsoft Paint and
some are more advanced and complicated.
Examples were shown with Microsoft Paint
because that software is readily available.
// Java1514.java
// This program demonstrates 84 of the 85 colors of the Deluxe Expo class
// There is no white circle drawn since white is the background color.
// NOTE: The 10 primary colors in the Deluxe Expo class are
//
red, green, blue, orange, cyan, magenta, yellow, gray, pink and tan.
//
Each of these colors actually has 5 shades.
//
Example: The 5 shades of red are red, darkRed, lightRed, darkerRed and lighterRed.
//
There are also 35 special colors like lime, teal, navy and chartreuse to name a few.
import java.awt.*;
import java.applet.*;
public class Java1514 extends Applet
{
public void paint(Graphics g)
{
int radius = 60;
Expo.setColor(g,Expo.darkerRed);
Expo.setColor(g,Expo.darkRed);
Expo.setColor(g,Expo.red);
Expo.setColor(g,Expo.lightRed);
Expo.setColor(g,Expo.lighterRed);
Expo.setColor(g,Expo.brown);
Expo.setColor(g,Expo.violet);
Expo.setColor(g,Expo.darkerGreen);
Expo.setColor(g,Expo.darkGreen);
Expo.setColor(g,Expo.green);
Expo.setColor(g,Expo.lightGreen);
Expo.setColor(g,Expo.lighterGreen);
Expo.fillCircle(g, 60, 60,radius);
Expo.fillCircle(g,140, 60,radius);
Expo.fillCircle(g,220, 60,radius);
Expo.fillCircle(g,300, 60,radius);
Expo.fillCircle(g,380, 60,radius);
Expo.fillCircle(g,460, 60,radius);
Expo.fillCircle(g,540, 60,radius);
Expo.fillCircle(g,620, 60,radius);
Expo.fillCircle(g,700, 60,radius);
Expo.fillCircle(g,780, 60,radius);
Expo.fillCircle(g,860, 60,radius);
Expo.fillCircle(g,940, 60,radius);
Expo.setColor(g,Expo.darkerBlue);
Expo.setColor(g,Expo.darkBlue);
Expo.setColor(g,Expo.blue);
Expo.setColor(g,Expo.lightBlue);
Expo.setColor(g,Expo.lighterBlue);
Expo.setColor(g,Expo.purple);
Expo.setColor(g,Expo.turquoise);
Expo.setColor(g,Expo.darkerOrange);
Expo.setColor(g,Expo.darkOrange);
Expo.setColor(g,Expo.orange);
Expo.setColor(g,Expo.lightOrange);
Expo.setColor(g,Expo.lighterOrange);
Expo.fillCircle(g, 60,140,radius);
Expo.fillCircle(g,140,140,radius);
Expo.fillCircle(g,220,140,radius);
Expo.fillCircle(g,300,140,radius);
Expo.fillCircle(g,380,140,radius);
Expo.fillCircle(g,460,140,radius);
Expo.fillCircle(g,540,140,radius);
Expo.fillCircle(g,620,140,radius);
Expo.fillCircle(g,700,140,radius);
Expo.fillCircle(g,780,140,radius);
Expo.fillCircle(g,860,140,radius);
Expo.fillCircle(g,940,140,radius);
Expo.setColor(g,Expo.darkerCyan);
Expo.setColor(g,Expo.darkCyan);
Expo.setColor(g,Expo.cyan);
Expo.setColor(g,Expo.lightCyan);
Expo.setColor(g,Expo.lighterCyan);
Expo.setColor(g,Expo.plum);
Expo.setColor(g,Expo.indigo);
Expo.setColor(g,Expo.darkerMagenta);
Expo.setColor(g,Expo.darkMagenta);
Expo.setColor(g,Expo.magenta);
Expo.setColor(g,Expo.lightMagenta);
Expo.setColor(g,Expo.lighterMagenta);
Expo.fillCircle(g, 60,220,radius);
Expo.fillCircle(g,140,220,radius);
Expo.fillCircle(g,220,220,radius);
Expo.fillCircle(g,300,220,radius);
Expo.fillCircle(g,380,220,radius);
Expo.fillCircle(g,460,220,radius);
Expo.fillCircle(g,540,220,radius);
Expo.fillCircle(g,620,220,radius);
Expo.fillCircle(g,700,220,radius);
Expo.fillCircle(g,780,220,radius);
Expo.fillCircle(g,860,220,radius);
Expo.fillCircle(g,940,220,radius);
Expo.setColor(g,Expo.darkerYellow);
Expo.setColor(g,Expo.darkYellow);
Expo.setColor(g,Expo.yellow);
Expo.setColor(g,Expo.lightYellow);
Expo.setColor(g,Expo.lighterYellow);
Expo.setColor(g,Expo.aqua);
Expo.setColor(g,Expo.aquaMarine);
Expo.setColor(g,Expo.darkerPink);
Expo.setColor(g,Expo.darkPink);
Expo.setColor(g,Expo.pink);
Expo.setColor(g,Expo.lightPink);
Expo.setColor(g,Expo.lighterPink);
Expo.fillCircle(g, 60,300,radius);
Expo.fillCircle(g,140,300,radius);
Expo.fillCircle(g,220,300,radius);
Expo.fillCircle(g,300,300,radius);
Expo.fillCircle(g,380,300,radius);
Expo.fillCircle(g,460,300,radius);
Expo.fillCircle(g,540,300,radius);
Expo.fillCircle(g,620,300,radius);
Expo.fillCircle(g,700,300,radius);
Expo.fillCircle(g,780,300,radius);
Expo.fillCircle(g,860,300,radius);
Expo.fillCircle(g,940,300,radius);
Expo.setColor(g,Expo.goldenRod);
Expo.setColor(g,Expo.gold);
Expo.setColor(g,Expo.silver);
Expo.setColor(g,Expo.bronze);
Expo.setColor(g,Expo.teal);
Expo.setColor(g,Expo.maroon);
Expo.setColor(g,Expo.fuschia);
Expo.setColor(g,Expo.lavender);
Expo.setColor(g,Expo.lime);
Expo.setColor(g,Expo.navy);
Expo.setColor(g,Expo.chartreuse);
Expo.setColor(g,Expo.fireBrick);
Expo.fillCircle(g, 60,380,radius);
Expo.fillCircle(g,140,380,radius);
Expo.fillCircle(g,220,380,radius);
Expo.fillCircle(g,300,380,radius);
Expo.fillCircle(g,380,380,radius);
Expo.fillCircle(g,460,380,radius);
Expo.fillCircle(g,540,380,radius);
Expo.fillCircle(g,620,380,radius);
Expo.fillCircle(g,700,380,radius);
Expo.fillCircle(g,780,380,radius);
Expo.fillCircle(g,860,380,radius);
Expo.fillCircle(g,940,380,radius);
}
}
Expo.setColor(g,Expo.moccasin);
Expo.setColor(g,Expo.salmon);
Expo.setColor(g,Expo.olive);
Expo.setColor(g,Expo.khaki);
Expo.setColor(g,Expo.crimson);
Expo.setColor(g,Expo.orchid);
Expo.setColor(g,Expo.sienna);
Expo.setColor(g,Expo.melon);
Expo.setColor(g,Expo.tangerine);
Expo.setColor(g,Expo.terraCotta);
Expo.setColor(g,Expo.pumpkin);
Expo.setColor(g,Expo.mahogany);
Expo.fillCircle(g, 60,460,radius);
Expo.fillCircle(g,140,460,radius);
Expo.fillCircle(g,220,460,radius);
Expo.fillCircle(g,300,460,radius);
Expo.fillCircle(g,380,460,radius);
Expo.fillCircle(g,460,460,radius);
Expo.fillCircle(g,540,460,radius);
Expo.fillCircle(g,620,460,radius);
Expo.fillCircle(g,700,460,radius);
Expo.fillCircle(g,780,460,radius);
Expo.fillCircle(g,860,460,radius);
Expo.fillCircle(g,940,460,radius);
Expo.setColor(g,Expo.darkerTan);
Expo.setColor(g,Expo.darkTan);
Expo.setColor(g,Expo.tan);
Expo.setColor(g,Expo.lightTan);
Expo.setColor(g,Expo.lighterTan);
Expo.setColor(g,Expo.amber);
Expo.setColor(g,Expo.black);
Expo.setColor(g,Expo.darkerGray);
Expo.setColor(g,Expo.darkGray);
Expo.setColor(g,Expo.gray);
Expo.setColor(g,Expo.lightGray);
Expo.setColor(g,Expo.lighterGray);
Expo.fillCircle(g, 60,540,radius);
Expo.fillCircle(g,140,540,radius);
Expo.fillCircle(g,220,540,radius);
Expo.fillCircle(g,300,540,radius);
Expo.fillCircle(g,380,540,radius);
Expo.fillCircle(g,460,540,radius);
Expo.fillCircle(g,540,540,radius);
Expo.fillCircle(g,620,540,radius);
Expo.fillCircle(g,700,540,radius);
Expo.fillCircle(g,780,540,radius);
Expo.fillCircle(g,860,540,radius);
Expo.fillCircle(g,940,540,radius);
The drawRoundedRectangle Method
Expo.drawRoundedRectangle(g, x1, y1, x2, y2, pixels);
Draws a rectangle with a top-left corner at coordinate
(x1,y1) and a bottom-right hand corner of (x2,y2).
Pixels determines how curved the corners will be.
x1, y1
pixels
x2, y2
The drawSpiral Method
Expo.drawSpiral(g, centerX, centerY, radius);
The parameters for Expo.drawSpiral are identical
to Expo.drawCircle.
centerX, centerY
radius
The drawBurst Method
Expo.drawBurst(g, centerX, centerY, radius, numLines);
The first 4 parameters for Expo.drawBurst are the
same as Expo.drawCircle.
The last parameter indicates the number of lines.
centerX, centerY
radius
The drawRandomBurst Method
Expo.drawRandomBurst(g, centerX, centerY, radius, numLines);
The first 4 parameters for Expo.drawRandomBurst
are the same as Expo.drawCircle.
The last parameter indicates the number of random lines.
centerX, centerY
radius
// Java1515.java
// This program demonstrates the drawSpiral, drawBurst and drawRandomBurst
// methods of the Deluxe Expo class.
// All three of these methods share the same parameters as drawCircle.
// The two "Burst" methods have an additional parameter for the number of lines.
// It also demonstrates the drawRoundedRectangle method of the Deluxe Expo class.
// It shares the same parameters as drawRectangle, except there is an extra parameter
// to control how "rounded" the corners will be.
import java.awt.*;
import java.applet.*;
public class Java1515 extends Applet
{
public void paint(Graphics g)
{
Expo.drawRoundedRectangle(g,10,20,990,300,25);
Expo.drawSpiral(g,180,160,100);
Expo.drawBurst(g,500,160,100,8);
Expo.drawRandomBurst(g,820,160,100,300);
Expo.drawRoundedRectangle(g,10,320,990,640,100);
Expo.drawSpiral(g,180,480,150);
Expo.drawBurst(g,500,480,150,40);
Expo.drawRandomBurst(g,820,480,150,5000);
}
}
// Java1516.java
// This program demonstrates the "drawThick" methods of the Deluxe Expo class.
// All "draw" methods in the Deluxe Expo class have a corresponding "drawThick"
// method with the same parameters, except that the "drawThick" methods have
// an extra parameter to control the "thickness".
import java.awt.*;
import java.applet.*;
public class Java1516 extends Applet
{
public void paint(Graphics g)
{
int t = 12; // thickness
Expo.setColor(g,Expo.blue);
Expo.drawThickLine(g,100,80,900,80,t);
Expo.drawThickRectangle(g,50,30,950,620,t);
Expo.drawThickCircle(g,500,310,200,t);
Expo.drawThickOval(g,400,300,30,40,t);
Expo.drawThickStar(g,400,300,100,10,t);
Expo.drawThickOval(g,600,300,30,40,t);
Expo.drawThickStar(g,600,300,90,6,t);
Expo.drawThickArc(g,500,415,105,65,90,270,t);
Expo.drawThickRoundedRectangle(g,100,100,200,600,25,t);
Expo.drawThickRoundedRectangle(g,800,100,900,500,50,t);
Expo.drawThickSpiral(g,280,115,100,t);
Expo.drawThickSpiral(g,700,115,100,t);
Expo.setColor(g,Expo.red);
Expo.drawThickBurst(g,150,350,150,20,t);
Expo.drawThickRandomBurst(g,850,350,150,300,t);
Expo.setColor(g,Expo.darkGreen);
Expo.drawThickRegularPolygon(g,150,555,40,3,t);
Expo.drawThickRegularPolygon(g,250,555,40,4,t);
Expo.drawThickRegularPolygon(g,350,555,40,5,t);
Expo.drawThickRegularPolygon(g,450,555,40,6,t);
Expo.drawThickRegularPolygon(g,550,555,40,7,t);
Expo.drawThickRegularPolygon(g,650,555,40,8,t);
Expo.drawThickRegularPolygon(g,750,555,40,9,t);
Expo.drawThickRegularPolygon(g,850,555,40,10,t);
}
}
// Java1517.java
// This program shows how to have user input in a graphics program with the
// enterIntGUI and enterStringGUI methods of the Deluxe Expo class.
// Methods enterDoubleGUI and enterCharGUI are also available.
import java.awt.*;
public class Java1517 extends java.applet.Applet
{
int amount;
String imageFile;
Image picture;
public void init()
{
String imageFile = Expo.enterStringGUI("What image do you wish to display?");
picture = getImage(getDocumentBase(), imageFile);
amount =
Expo.enterIntGUI("How many times do you wish to display this image?");
}
}
public void paint(Graphics g)
{
for (int j = 1; j <=amount; j++)
{
int x = Expo.random(0,900);
int y = Expo.random(0,550);
g.drawImage(picture,x,y,this);
}
}
Java1517.java Input
As before, you are provided with the
file Expo.html which documents all
of the methods in the Deluxe Expo
class (both old and new).
// Java1518.java
This program shows how to display multiple pages of graphics output.
import java.awt.*;
public class Java1518 extends java.applet.Applet
{
int numClicks;
Image picture1, picture2, picture3, picture4, picture5;
public void init()
{
numClicks = 0;
picture1 = getImage(getDocumentBase(),"LSchram.gif");
picture2 = getImage(getDocumentBase(),"JSchram.jpg");
picture3 = getImage(getDocumentBase(),"nederlands.gif");
picture4 = getImage(getDocumentBase(),"leon&isolde.jpg");
picture5 = getImage(getDocumentBase(),"JPIILogo.jpg");
// The following MediaTracker/try/catch code ensures that
// all images are loaded before the program continues.
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(picture1,1);
tracker.addImage(picture2,1);
tracker.addImage(picture3,1);
tracker.addImage(picture4,1);
tracker.addImage(picture5,1);
try
{
tracker.waitForAll();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
public void paint(Graphics g)
{
switch (numClicks)
{
case 0 : page1(g); break;
case 1 : page2(g); break;
case 2 : page3(g); break;
}
}
public boolean mouseDown(Event e, int x, int y)
{
numClicks++;
repaint();
return true;
}
public void page1(Graphics g)
{
Expo.setFont(g,"Arial",Font.BOLD,100);
Expo.drawString(g,"TITLE PAGE",218,100);
Expo.setColor(g,Expo.yellow);
Expo.drawThickPreciseSpiral(g,510,370,275,50,15);
Expo.setColor(g,Expo.red);
Expo.fillStar(g,510,370,175,8);
Expo.setColor(g,Expo.green);
Expo.drawThickStar(g,510,370,225,8,25);
Expo.setColor(g,Expo.blue);
Expo.drawThickStar(g,510,370,275,8,25);
g.drawImage(picture1,425,285,this);
Expo.setFont(g,"Times Roman",Font.PLAIN,20);
Expo.drawString(g,"My name is Leon Schram.",30,300);
Expo.drawString(g,"Click once to continue.",760,550);
}
public void page2(Graphics g)
{
Expo.setFont(g,"Algerian",Font.BOLD,100);
Expo.drawString(g,"PAGE 2",200,100);
Expo.setColor(g,Expo.blue);
Expo.fillRectangle(g,100,250,900,500);
Expo.setColor(g,Expo.chartreuse);
g.drawImage(picture3,200,300,this);
g.drawImage(picture4,500,280,this);
Expo.setColor(g,Expo.blue);
Expo.setFont(g,"Times Roman",Font.PLAIN,20);
Expo.drawString(g,"I was born in the Netherlands in
1945 and married my sweet wife Isolde in 1967.",100,200);
Expo.drawString(g,"Click once to continue.",750,550);
}
public void page3(Graphics g)
{
Expo.setFont(g,"Impact",Font.BOLD,100);
Expo.drawString(g,"PAGE 3",200,100);
Expo.setColor(g,Expo.darkGreen);
Expo.fillRoundedRectangle(g,460,170,960,515,50);
g.drawImage(picture5,150,225,this);
g.drawImage(picture2,500,193,this);
Expo.setFont(g,"Times Roman",Font.PLAIN,20);
Expo.drawString(g,"I teach Computer Science at John
Paul II High School with my son John.",100,150);
Expo.drawString(g,"Click once to continue.",750,550);
}
}
End of Part 1
Program Java1518.java concludes the
Part 1 section which prepares you for
the Personal Slide Show project.
See the GraphicsLab09.docx file for
assignment details.
Beginning of Part 2
The remaining programs are
to help prepare you for the
Final Graphics Project.
This will be the biggest
assignment of the year.
// Java1519.java
// This program draws a small square at every mouse click position.
// There are 2 problems with the program:
// First, it draws a square in the upper left hand corner automatically before we have a
// chance to do anything. Second, it will only draws one square at a time.
import java.applet.Applet;
import java.awt.*;
public class Java1519 extends Applet
{
int xCoord;
int yCoord;
public void paint(Graphics g)
{
Expo.setColor(g,Expo.red);
Expo.fillRectangle(g,xCoord,yCoord,xCoord+15,yCoord+15);
}
public boolean mouseDown(Event e, int x, int y)
{
xCoord = x; yCoord = y;
repaint();
return true;
}
}
// Java1520.java
// This program cures the problem of the initial red square by adding a "firstPaint" boolean variable.
import java.applet.Applet;
import java.awt.*;
public class Java1520 extends Applet
{
int xCoord;
int yCoord;
boolean firstPaint;
public void init() { firstPaint = true; }
public void paint(Graphics g)
{
if (firstPaint)
firstPaint = false;
else
{
Expo.setColor(g,Expo.red);
Expo.fillRectangle(g,xCoord,yCoord,xCoord+15,yCoord+15);
}
}
public boolean mouseDown(Event e, int x, int y)
{
xCoord = x;
yCoord = y;
repaint();
return true;
}
}
// Java1521.java
// This program adds the clever "update" method which prevents the computer from clearing the
// paint window every time it is refreshed. This allows you to draw more than one square at a time.
import java.applet.Applet;
import java.awt.*;
public class Java1521 extends Applet
{
int xCoord;
int yCoord;
boolean firstPaint;
public void init() { firstPaint = true; }
public void paint(Graphics g)
{
if (firstPaint)
firstPaint = false;
else
{
Expo.setColor(g,Expo.red);
Expo.fillRectangle(g,xCoord,yCoord,xCoord+15,yCoord+15);
}
}
public boolean mouseDown(Event e, int x, int y)
{
xCoord = x; yCoord = y;
repaint();
return true;
}
public void update(Graphics g) { paint(g); }
}
// Java1522.java
// This program changes "mouseDown" to "mouseDrag" and makes the square smaller.
// This should create the effect of having a freehand pencil.
import java.applet.Applet;
import java.awt.*;
public class Java1522 extends Applet
{
int xCoord,yCoord;
public void init()
{
xCoord = 0;
yCoord = 0;
}
public void paint(Graphics g)
{
Expo.setColor(g,Expo.red);
Expo.drawPoint(g,xCoord,yCoord);
}
public boolean mouseDrag(Event e, int x, int y)
{
xCoord = x;
yCoord = y;
repaint();
return true;
}
public void update(Graphics g) { paint(g); }
}
// Java1523.java
// This program draws a straight line from the point where the mouse is clicked
// to the point where the mouse is released.
import java.applet.Applet;
import java.awt.*;
public class Java1523 extends Applet
{
int startX,startY,endX,endY;
public void paint(Graphics g)
{
Expo.drawLine(g,startX,startY,endX,endY);
}
public boolean mouseDown(Event e, int x, int y)
{
startX = x;
startY = y;
return true;
}
public boolean mouseUp(Event e, int x, int y)
{
endX = x;
endY = y;
repaint();
return true;
}
}
// Java1524.java
// This program draws a straight line from the point where the mouse is clicked
// to the point where the mouse is released. In this example the line is
// constantly visible. This is sometimes called the "rubber band" effect.
import java.applet.Applet;
import java.awt.*;
public class Java1524 extends Applet
{
int startX,startY,endX,endY;
public void paint(Graphics g)
{
Expo.drawLine(g,startX,startY,endX,endY);
}
public boolean mouseDown(Event e, int x, int y)
{
startX = x;
startY = y;
return true;
}
public boolean mouseDrag(Event e, int x, int y)
{
endX = x;
endY = y;
repaint();
return true;
}
}
// Java1525.java
// This program shows that method "update" can causes a peculiar "side effect" in some cases.
// Watch what happens when you try to draw a line.
import java.applet.Applet;
import java.awt.*;
public class Java1525 extends Applet
{
int startX,startY,endX,endY;
public void paint(Graphics g)
{
Expo.drawLine(g,startX,startY,endX,endY);
}
public boolean mouseDown(Event e, int x, int y)
{
startX = x; startY = y;
return true;
}
public boolean mouseDrag(Event e, int x, int y)
{
endX = x; endY = y;
repaint();
return true;
}
public void update(Graphics g) { paint(g);
}
}
// Java1526.java
// This program is almost identical to Java1524.java.
// The only difference is that "drawLine" has been changed to "drawRectangle".
// Note how the rectangles are draw with the same "rubber band effect" as the lines.
import java.applet.Applet;
import java.awt.*;
public class Java1526 extends Applet
{
int startX,startY,endX,endY;
public void paint(Graphics g)
{
Expo.drawRectangle(g,startX,startY,endX,endY);
}
public boolean mouseDown(Event e, int x, int y)
{
startX = x;
startY = y;
return true;
}
public boolean mouseDrag(Event e, int x, int y)
{
endX = x;
endY = y;
repaint();
return true;
}
}
Drawing Circles
Drawing circles is more complicated than drawing
lines or rectangles.
A circles has a center and a radius.
mouseDown determines the center.
mouseUp determines a point on the rim of the circle.
To find the radius we need to find the distance
between these 2 points using the distance formula
which is based on the Pythagorean Theorem.
Pythagorean Theorem
This Theorem states that in a right triangle,
the square of the hypotenuse
(longest side) is equal to the
sum of the squares of the
a
c
2 shorter sides.
So
c2 = a 2 + b2
b
The actual formula for finding the hypotenuse is:
Distance Formula
The distance between 2 points in a coordinate
plane is essentially the hypotenuse of the right
triangle they form.
(x1, y1)
(x2, y2)
(x2,y2)
// Java1527.java
// This program will draw circles using a special "getRadius" method
// which is based on the Pythagorean Formula.
import java.applet.Applet;
import java.awt.*;
public class Java1527 extends Applet
{
int centerX,centerY,rimX,rimY;
public void paint(Graphics g)
{
int radius = getRadius(centerX,centerY,rimX,rimY);
Expo.drawCircle(g,centerX,centerY,radius);
}
public boolean mouseDown(Event e, int x, int y)
{
centerX = x; centerY = y;
return true;
}
public boolean mouseDrag(Event e, int x, int y)
{
rimX = x; rimY = y;
repaint();
return true;
}
public int getRadius(int x1, int y1, int x2, int y2)
{
double radius = Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
return (int) radius;
}
}
// Java1528.java
// This uses the <Rectangle> class with the
// <inside> method to determine if a certain
// square has been clicked.
import java.applet.Applet;
import java.awt.*;
public class Java1528 extends Applet
{
Rectangle red, green, blue;
int numColor;
public void paint(Graphics g)
{
Expo.setColor(g,Expo.red);
Expo.fillRectangle(g,100,75,250,225);
Expo.setColor(g,Expo.green);
Expo.fillRectangle(g,100,275,250,425);
Expo.setColor(g,Expo.blue);
Expo.fillRectangle(g,100,475,250,625);
Expo.setFont(g,"Arial",Font.BOLD,28);
switch (numColor)
{
case 1:
Expo.setColor(g,Expo.red);
g.drawString("Mouse clicked inside red",300,150);
break;
case 2:
Expo.setColor(g,Expo.green);
g.drawString("Mouse clicked inside green",300,350);
break;
case 3:
Expo.setColor(g,Expo.blue);
g.drawString("Mouse clicked inside blue",300,550);
break;
case 4:
Expo.setColor(g,Expo.black);
g.drawString(
"Mouse clicked outside the colored squares",300,40);
break;
}
public void init()
{
red = new Rectangle(100,75,150,150);
green = new Rectangle(100,275,150,150);
blue = new Rectangle(100,475,150,150);
numColor = 0;
}
public boolean mouseDown(Event e, int x, int y)
{
if(red.inside(x,y))
numColor = 1;
else if(green.inside(x,y))
numColor = 2;
else if(blue.inside(x,y))
numColor = 3;
else
numColor = 4;
repaint();
return true;
}
}
}
// Java1529.java
// This program proves that objects of the <Rectangle> class are abstract to the viewer and
// not visible. The three square are intentionally not displayed. The program still works
// like the previous program, but you must guess at the location of the squares.
import java.applet.Applet;
import java.awt.*;
public class Java1529 extends Applet
{
Rectangle red, green, blue;
int numColor;
public void paint(Graphics g)
{
// Note: The solid colored squares are not drawn this time.
Expo.setFont(g,"Arial",Font.BOLD,28);
switch (numColor)
{
case 1:
Expo.setColor(g,Expo.red);
g.drawString("Mouse clicked inside red",300,150);
break;
case 2:
Expo.setColor(g,Expo.green);
g.drawString("Mouse clicked inside green",300,350);
break;
case 3:
Expo.setColor(g,Expo.blue);
g.drawString("Mouse clicked inside blue",300,550);
break;
case 4:
Expo.setColor(g,Expo.black);
g.drawString(
"Mouse clicked outside the colored squares",300,40);
break;
}
public void init()
{
red = new Rectangle(100,75,150,150);
green = new Rectangle(100,275,150,150);
blue = new Rectangle(100,475,150,150);
numColor = 0;
}
public boolean mouseDown(Event e, int x, int y)
{
if(red.inside(x,y))
numColor = 1;
else if(green.inside(x,y))
numColor = 2;
else if(blue.inside(x,y))
numColor = 3;
else
numColor = 4;
repaint();
return true;
}
}
}
Difference Between
Drawing Rectangles and
Creating Rectangle Objects
Expo.drawRectangle(g,x1,y1,x2,y2); Rectangle area = new Rectangle(x,y,width,height);
// Java1530.java Animation Part 1
// This program simply draws a solid black circle.
// This will eventually be "animated" as we go through
// the next few programs.
import java.awt.*;
public class Java1530 extends java.applet.Applet
{
public void paint(Graphics g)
{
Expo.fillCircle(g,50,325,25);
}
}
// Java1531.java Animation Part 2
// This program simply draws the solid black circle at 10 different locations.
// This is not yet animation because we see all 10 circles at the same time.
import java.awt.*;
public class Java1531 extends java.applet.Applet
{
public void paint(Graphics g)
{
Expo.fillCircle(g,50,325,25);
Expo.fillCircle(g,150,325,25);
Expo.fillCircle(g,250,325,25);
Expo.fillCircle(g,350,325,25);
Expo.fillCircle(g,450,325,25);
Expo.fillCircle(g,550,325,25);
Expo.fillCircle(g,650,325,25);
Expo.fillCircle(g,750,325,25);
Expo.fillCircle(g,850,325,25);
Expo.fillCircle(g,950,325,25);
}
}
// Java1532.java Animation Part 3
// This program erases every circle before it draws the next one.
// This is necessary for animation; however, the process happens
// so fast you cannot see the circle move.
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,450,325,25);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,450,325,25);
import java.awt.*;
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,550,325,25);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,550,325,25);
public class Java1532 extends java.applet.Applet
{
public void paint(Graphics g)
{
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,50,325,25);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,50,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,650,325,25);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,650,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,750,325,25);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,750,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,150,325,25);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,150,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,850,325,25);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,850,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,250,325,25);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,250,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,350,325,25);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,350,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,950,325,25);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,950,325,25);
}
}
// Java1533.java Animation Part 4
// This program adds a short 1 second delay after each circle is drawn
// to give you a chance to see it before it is erased.
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,650,325,25);
Expo.delay(1000);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,650,325,25);
import java.awt.*;
public class Java1533 extends java.applet.Applet
{
public void paint(Graphics g)
{
Expo.setColor(g,Expo.black); Expo.setColor(g,Expo.black);
Expo.fillCircle(g,350,325,25);
Expo.fillCircle(g,50,325,25);
Expo.delay(1000);
Expo.delay(1000);
Expo.setColor(g,Expo.white); Expo.setColor(g,Expo.white);
Expo.fillCircle(g,350,325,25);
Expo.fillCircle(g,50,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,150,325,25);
Expo.delay(1000);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,150,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,250,325,25);
Expo.delay(1000);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,250,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,750,325,25);
Expo.delay(1000);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,750,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,850,325,25);
Expo.delay(1000);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,850,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,450,325,25);
Expo.delay(1000);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,450,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,550,325,25);
Expo.delay(1000);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,550,325,25);
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,950,325,25);
Expo.delay(1000);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,950,325,25);
}
}
Program Note
The output of program Java1533.java
was simulated with PowerPoint.
It will not be possible to simulate the
output of the remaining programs.
You need to execute them on
your own computer to truly
see what they are doing.
// Java1534.java Animation Part 5
// This program has the same output as the previous program.
// The program is now much shorter since a for loop is used.
import java.awt.*;
public class Java1534 extends java.applet.Applet
{
public void paint(Graphics g)
{
for (int x = 50; x <= 950; x+=100)
{
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,x,325,25);
Expo.delay(1000);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,x,325,25);
}
}
}
// Java1535.java Animation Part 6
// This program makes the animation smoother by using a smaller delay
// and a smaller increment in the for loop.
import java.awt.*;
public class Java1535 extends java.applet.Applet
{
public void paint(Graphics g)
{
for (int x = 50; x <= 950; x+=10)
{
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,x,325,25);
Expo.delay(100);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,x,325,25);
}
}
}
// Java1536.java Animation Part 7
// This program makes the animation as smooth as possible by having an
// increment of just 1 pixel in the for loop. The delay is also made smaller.
import java.awt.*;
public class Java1536 extends java.applet.Applet
{
public void paint(Graphics g)
{
for (int x = 50; x <= 950; x+=1)
{
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,x,325,25);
Expo.delay(10);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,x,325,25);
}
}
}
Animation Review
To achieve animation, you need to do
the following:
1) Draw something
2) Delay for a fraction of a second
3) Erase it by drawing over it in the
background color.
4) Redraw it in a slightly different
location and repeat.
// Java1537.java Animation Part 8
// This program draws and erases 3 circles instead of one to animate a snowman across the screen.
// The flickering that you will see is normal for this type of animation.
// In AP Computer Science you will learn a more advanced type of animation that eliminates the flicker.
import java.awt.*;
public class Java1537 extends java.applet.Applet
{
public void paint(Graphics g)
{
for (int x = 50; x <= 950; x+=1)
{
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,x,290,15);
Expo.fillCircle(g,x,325,25);
Expo.fillCircle(g,x,380,40);
Expo.delay(10);
Expo.setColor(g,Expo.white);
Expo.fillCircle(g,x,290,15);
Expo.fillCircle(g,x,325,25);
Expo.fillCircle(g,x,380,40);
}
}
}
// Java1538.java Animation Part 9
// This program shows that an image from a file can also be made to move across the screen.
// As with the snowman, there can be considerable flickering.
import java.awt.*;
public class Java1538 extends java.applet.Applet
{
Image picture;
public void init()
{
picture = getImage(getDocumentBase(),"bunny.png");
}
public void paint(Graphics g)
{
for (int x = -100; x <= 1100; x+=3)
{
g.drawImage(picture,x,200,this);
Expo.delay(30);
Expo.setColor(g,Expo.white);
Expo.fillRectangle(g,x,200,x+100,300);
}
}
}
// Java1539.java Animation Part 10
// This program demonstrates the problem that occurs with a multi-colored background.
import java.awt.*;
public class Java1539 extends java.applet.Applet
{
public void paint(Graphics g)
{
for (int c = 1; c <= 9; c++)
{
Expo.setColor(g,c);
Expo.fillRectangle(g, (c-1)*111, 0, c*111, 650);
}
for (int x = 50; x <= 950; x+=3)
{
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,x,290,15);
Expo.fillCircle(g,x,325,25);
Expo.fillCircle(g,x,380,40);
Expo.delay(30);
Expo.setColor(g,Expo.red);
Expo.fillCircle(g,x,290,15);
Expo.fillCircle(g,x,325,25);
Expo.fillCircle(g,x,380,40);
}
}
}
// Java1540.java Animation Part 11
// This program shows how to handle a multi-colored background.
// Instead of "draw-and-erase", it is "draw-and-redraw".
// The entire background is redrawn every time the object moves.
// NOTE: This does have the potential for serious flickering.
import java.awt.*;
public class Java1540 extends java.applet.Applet
{
public void paint(Graphics g)
{
for (int x = 50; x <= 950; x+=3)
{
for (int c = 1; c <= 9; c++)
{
Expo.setColor(g,c);
Expo.fillRectangle(g, (c-1)*111, 0, c*111, 650);
}
Expo.setColor(g,Expo.black);
Expo.fillCircle(g,x,290,15);
Expo.fillCircle(g,x,325,25);
Expo.fillCircle(g,x,380,40);
Expo.delay(30);
}
}
}
Programming Knowledge
Never Becomes Obsolete
Some people may be concerned that with technology
changing so rapidly, the programming knowledge that
they have acquired in this course will soon be out of
date. While new programming languages pop up from
time to time, the logic of programming has never
changed.
The next three programs all do the exact same thing.
The first is written in Java. The second in C++ and the
third in Pascal. Even though you may have never seen
C++ or Pascal, you may be surprised how familiar these
programs will look to you now that you know Java.
// Java1541.java
// This program will count backwards from 20 to 0 and displays which numbers
// are even and which are odd.
// Compare this Java code to the code from other languages.
public class Java1541
{
public static void main (String args[])
{
System.out.println("\nJAVA1541.JAVA\n\n");
for (int j = 20; j >= 0; j--)
{
if (j % 2 == 0)
{
System.out.println(j + " is an even number.");
}
else
{
System.out.println(j + " is an odd number.");
}
}
System.out.println();
}
}
// CPlusPlus.CPP
// This program will count backwards from 20 to 0 and displays which numbers
// are even and which are odd.
// Compare this C++ code to the code from other languages.
#include <iostream.h>
#include <conio.h>
void main()
{
cout << endl << "CPlusPlus.CPP" << endl << endl;
for (int j = 20; j >= 0; j--)
{
if (j % 2 == 0)
{
cout << j << " is an even number." << endl;
}
else
{
cout << j << " is an odd number." << endl;
}
}
cout << endl;
getch(); // makes the computer wait until a character is pressed
}
{ Pascal.PAS
This program will count backwards from 20 to 0 and displays which numbers
are even and which are odd.
Compare this Pascal code to the code from other languages. }
PROGRAM My_Pascal_Program;
VAR
J : INTEGER; (* Loop Counter *)
BEGIN
WRITELN;
WRITELN('Pascal.PAS');
WRITELN;
FOR J := 20 DOWNTO 0 DO BEGIN
IF J MOD 2 = 0 THEN BEGIN
WRITELN(J,' is an even number.')
END
ELSE BEGIN
WRITELN(J,' is an odd number.')
END
END
END.
Download