// CS111 Fall 2000 Lab 10 import java.awt.*; import java.applet.Applet; import java.util.*; public class Peeper extends Applet { public void paint(Graphics g){ // We all live in a yellow submarine // a yellow submarine a yellow submarine Eyes john = new Eyes(g); john.setPosition(new Point(10,130)); Eyes paul = new Eyes(g); paul.resize(50); paul.setEyeColor(Color.blue); paul.setPupilColor(Color.white); paul.setPosition(new Point(150, 125)); Eyes ringo = new Eyes(g); ringo.resize(25); ringo.setEyeColor(Color.cyan); ringo.setPupilColor(Color.magenta); ringo.setPosition(new Point(280, 150)); Eyes george = new Eyes(g); george.setEyeColor(Color.black); george.setPupilColor(Color.red); george.setPosition(new Point(350, 120)); } } class Eyes { //instance variable declarations private Point position; private Color eyeColor; private Color pupilColor; private Graphics gfx; private int width; private int bridge; //constructor method public Eyes (Graphics g) { this.position = new Point(1, 5); this.eyeColor = Color.red; this.pupilColor = Color.yellow; this.width = 40; this.bridge = width+(width/3); this.gfx = g; this.drawEyes(); } //instance methods public void setPosition(Point p){ this.clearEyes(); this.position.x = p.x; this.position.y = p.y; this.drawEyes(); } public void setEyeColor(Color c){ this.eyeColor = c; this.drawEyes(); } public void setPupilColor(Color c){ this.pupilColor = c; this.drawEyes(); } public void resize(int size){ this.clearEyes(); this.width = size; this.drawEyes(); } public void clearEyes(){ int x = position.x; int y = position.y; //clears one eye gfx.setColor(Color.white); gfx.fillOval(x, y, width, 2*width/3); gfx.setColor(Color.white); gfx.drawOval(x, y, width, 2*width/3); gfx.setColor(Color.white); gfx.fillOval(x+width/3,y+width/10, width/4, width/2); //clears other eye gfx.setColor(Color.white); gfx.fillOval(x+bridge, y, width, 2*width/3); gfx.setColor(Color.white); gfx.drawOval(x+bridge, y, width, 2*width/3); gfx.setColor(Color.white); gfx.fillOval(x+bridge+width/3,y+width/10, width/4, width/2); } public void drawEyes(){ int x = position.x; int y = position.y; //draws one eye gfx.setColor(eyeColor); gfx.fillOval(x, y, width, 2*width/3); gfx.setColor(Color.black); gfx.drawOval(x, y, width, 2*width/3); gfx.setColor(pupilColor); gfx.fillOval(x+width/3,y+width/10, width/4, width/2); //draws other eye gfx.setColor(eyeColor); gfx.fillOval(x+bridge, y, width, 2*width/3); gfx.setColor(Color.black); gfx.drawOval(x+bridge, y, width, 2*width/3); gfx.setColor(pupilColor); gfx.fillOval(x+bridge+width/3,y+width/10, width/4, width/2); } }