Lab 12 Building your own objects

 Jeepers peepers where'd you get those eyes?

 

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.

 

 

Three things you need to know:

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:


In order to make the Eyes, we have to do 3 things:

  1. Define the instance variables
  2. Define the constructor method
  3. Write an instance method to draw the Eyes, to find out the values of its parameters, and to change these values.

1. Defining the instance variables

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 width + width/3 in the constructor

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

2. Defining the Constructor method

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;

3. Writing the drawEyes() method

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.

4. Testing your methods with theBeatles() method

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

Specify the parameters in the constructor. Don't forget to invoke drawEyes() method on each pair of eyes! The picture should look like the one above.

5. Instance Methods for the Eyes Class

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.
  1. Tiling a picture of the eyes:

    
    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:

  2. Tiling a given picture with alternating colors:

    
    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:

  3. The method

     
    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.

  4. Now use the shadows method to draw the Beatles eyes picture with shodows in the method theBeatlesWithShadows(g):