// ------------------------------------------------------ // imports // ------------------------------------------------------ import java.awt.event.MouseMotionListener; import java.awt.event.MouseEvent; import java.awt.Graphics2D; import java.awt.Graphics; import java.awt.Color; import javax.swing.JPanel; import java.util.Random; // ------------------------------------------------------ // Eyes // ------------------------------------------------------ public class Eyes extends JPanel implements MouseMotionListener { // ------------------------------------------------------ // Constructor // ------------------------------------------------------ public Eyes() { addMouseMotionListener(this); } // ------------------------------------------------------ // paintComponent - Draw our three sets of eyes // ------------------------------------------------------ public void paintComponent(Graphics g) { super.paintComponent(g); int w = getWidth(); int h = getHeight(); int xc = (w / 2); int yc = (h / 2); int size = Math.min(w, h); double r = .3 * size; double angle = 110.0; for(int i = 0; i < 3; i ++, angle += 120) { double radians = Math.toRadians(angle); double xValue = xc + r * Math.cos(radians); double yValue = yc + r * Math.sin(radians); double offset = (.0625 * size); // -------------------------------------------- // Each eye is a circle, 'xFirst' is the center // of the left eye, 'xSecond' the center of // the right eye // -------------------------------------------- double xFirst = xValue - offset; double xSecond = xValue + offset; Color eyeColor = (i == 0 ? BLUE : i == 1 ? BROWN : GREEN); drawEyes(g, eyeColor, xFirst, xSecond, yValue, size / 10); } } // ------------------------------------------------------ // mouseMoved - Pass 'e' on to 'handleMouse' // ------------------------------------------------------ public void mouseMoved(MouseEvent e) { handleMouse(e); } // ------------------------------------------------------ // mouseDragged - Pass 'e' on to 'handleMouse' // ------------------------------------------------------ public void mouseDragged(MouseEvent e) { handleMouse(e); } // ------------------------------------------------------ // r - Return 'value' rounded to the nearest integer // ------------------------------------------------------ private static int r(double value) { return((int)Math.round(value)); } // ------------------------------------------------------ // getAngle - Find the angle between (x, y) and the // most recent mouse location // ------------------------------------------------------ private double getAngle(double x, double y) { double xDelta = (mMouseX - x); double yDelta = (mMouseY - y); double angle = Math.atan2(yDelta, xDelta); return(angle); } // ------------------------------------------------------ // handleMouse - Save off the mouse's current location, // this is where our eyes will be looking // ------------------------------------------------------ private void handleMouse(MouseEvent e) { mMouseX = e.getX(); mMouseY = e.getY(); repaint(); } // ------------------------------------------------------ // doCircle - Draw a circle at (x, y) with 'radius' // and filled (or drawn) based on 'fill' // ------------------------------------------------------ private static void doCircle( double x, double y, double radius, Graphics g, boolean fill ) { int xValue = r(x - radius / 2); int yValue = r(y - radius / 2); int rValue = r(radius); if(fill) g.fillOval(xValue, yValue, rValue, rValue); else g.drawOval(xValue, yValue, rValue, rValue); } // ------------------------------------------------------ // drawEyes - Draw two eyes, one at (xFirst, yValue) // the other at (xSecond, yValue) using // 'eyeColor' and radius 'r1' // ------------------------------------------------------ private void drawEyes( Graphics g, Color eyeColor, double xFirst, double xSecond, double yValue, double r1 ) { doCircle(xFirst, yValue, r1, g, false); doCircle(xSecond, yValue, r1, g, false); double a1 = getAngle(xFirst, yValue); double a2 = getAngle(xSecond, yValue); // ---------------------------------- // The iris's radius and the pupil's // ---------------------------------- double r2 = .4 * r1; double r3 = .2 * r1; double x1 = xFirst + r3 * Math.cos(a1); double y1 = yValue + r3 * Math.sin(a1); double x2 = xSecond + r3 * Math.cos(a2); double y2 = yValue + r3 * Math.sin(a2); // ---------------------------------- // Draw the two irises, first filled // using 'eyeColor' then outlined // in black // ---------------------------------- g.setColor(eyeColor); doCircle(x1, y1, r2, g, true); doCircle(x2, y2, r2, g, true); g.setColor(Color.black); doCircle(x1, y1, r2, g, false); doCircle(x2, y2, r2, g, false); // ----------------------- // Now draw the 2 pupils // ----------------------- doCircle(x1, y1, r3, g, true); doCircle(x2, y2, r3, g, true); } // ------------------------------------------------------ // Member Variables // ------------------------------------------------------ public static final Color BLUE = new Color(140, 160, 195); public static final Color BROWN = new Color(170, 130, 110); public static final Color GREEN = new Color(130, 155, 110); private int mMouseX; private int mMouseY; }