Download the Peeper
folder from lab11_programs
folder
from the CS111 download folder.
You'll edit Peeper.java
in order to create the picture below.
You're going to write a java program that has two classes. There is a Peeper class and an Eyes class. The Peeper class has the methods that create pictures made of pairs of eyes, and the Eyes class definition will define Eyes objects using their instance variables, constructor method and instance methods.
1. The origin (0,0) of our graphics coordinate system is in the upper left hand corner (unlike BuggleWorld)
2. An Eyes object (one Eyes object is actually a pair of eyes) looks like this:
3. Here's how to draw the Eyes object:
Eyes objects are defined by the following variables:
int width |
The width of an eye |
int bridge |
The distance from the left side of one eye to the left of
the other. Set it to |
Point position |
The position of the upper left corner |
Color eyeColor |
The Color of the eye background |
Color pupilColor |
The Color of the pupil |
Graphics gfx |
The graphics object for drawing to the right part of the applet window |
Define these variables at the top of the Eye class
The constructor method should initialize the instance variables to the given values when an eye object is created.
To draw correctly to the applet's Canvas, the Eyes object needs to have access to the Graphics object from the paint method. The easiest way to do this is to use a parameter in the constructor method (this is the "Graphics g" parameter in the method definition). The Eyes object can keep track of this object by assigning its value to the "gfx" instance variable, as follows:
gfx = g;
Use the java graphics methods drawOval and fillOval to draw the Eyes above. Want to know more about these methods? Click here for the Java API.
Now you can fill in the code for the method theBeatles in the Peeper class. It creates 4 pairs of eyes with the following parameters:
eyes |
eyeColor |
pupilColor |
position |
width |
john |
red |
yellow |
(10,130) |
40 |
paul |
blue |
white |
(150,125) |
25 |
ringo |
cyan |
magenta |
(280,250) |
65 |
george |
orange |
blue |
(350,120) |
90 |
Now you are ready to write some eyes methods, such as setting eye background color, the pupil color, the position of the eyes, and the width of the eyes. The methods names are as follows:
public void setEyeColor(Color c) Set the eye color of the Eyes to c. public void setPupilColor(Color c) Set the pupil color of the Eyes to c. public Color getEyeColor() Return the color of the eye. public Color getPupilColor() Return the color of the pupil. public void setPosition(Point p) Set the new eyes position to be the point p. public Point getPosition() Returns the position of the eyes. public int getWidth() Returns the width of the eyes (the value of the instance variable width). public void resize(int new_width) Set the width of the eyes to new_width. Note that the bridge must change, too. public int getTotalWidth() Returns the total width taken up by the eyes plus some padding needed to draw two pairs of eyes one right next to the other. I used 2*bridge + width/5 in my pictures, but feel free to change it. public int getTotalHeight() Returns the total height of the eyes plus some padding. I used (2*width)/3 + width/5 in my pictures.Using the Eyes methods that you have written, fill in the code the Peeper methods described below. To test the method, remove the comments from the method invocations in the Paint method of the Peeper class. Comment out the other method invocations, otherwise the pictures will be drawn one on top of the other.
public void tile(Graphics g, Eyes e)
The method takes the picture of eyes and repeats it on the applet screen as many times as possible, row by row. The width of the screen is 600, the height is 400. It's OK if your pictures go over the border of the sceern slightly.
You should use nested loops (for
or while
) to draw
the pattern. In order to draw the eyes in the new spot, use the method setPosition()
of Eyes. The result of the method invocation
tile(g, new Eyes(g, 20, new Point(1,5), Color.black, Color.magenta));
is below:
public void alternate(Graphics g, Eyes e)
The method is similar to the previous one, but the colors of the eye and the pupil ae switched from one picture to the next. The method invocation
alternate(g, new Eyes(g, 40, new Point(1,5), Color.blue, Color.cyan));
gives the following picture:
shadows(Graphics g, Eyes e)
takes a picture of the eyes and draws it with "shadows" (darker, smaller copies of the eyes picture at its right lower corner). For example, the invocation
shadows(g, new Eyes(g, 80, new Point(100,100), Color.cyan, Color.blue));
gives the following picture:
Use the Color method darker() to make the color darker.
-
Now use the shadows method to draw the Beatles eyes picture with shodows in
the method theBeatlesWithShadows(g):