/* * @(#)eyes.java v0.1 11/97 by Eero Tamminen * * Only documentation on AWT I have is JDK source and examples. * It seems fairly sufficiant ;-). * * History: * - 13/11, Decided finally to tackle Java. * - 14/11, Browsed JDK examples. My first Java code. * - 15/11, Browsed some AWT class sources. First version of Eyes. * - 16/11, v0.1. Added eye 'shadow' and pupil highlight. * - 18/11, Read first real*) Java documentation. * Added update(angle) and getAngle() methods to Eye. * Changed code to use Java Coding Conventions. * - 19/11, Added AppletFrame so that Eyes.class can be executable. * * *) Earlier I have read only all kinds of Java hype ('Archimedes company' * example was especially irritating). I had dabbled a bit with Python * since last autumn and that proved a great help because (programming) * Python is so similar to Java. There's even a Python WWW-browser that * can run Python applets (scripts). */ import java.awt.*; import java.applet.Applet; import java.lang.Math; class Eye { private int rx, ry; /* eye inside radius */ private int rxp, ryp; /* pupil radius */ private int midx, midy; /* eye midpoint */ private int pupx, pupy; /* pupil midpoint */ private double angle; /* current pupil direction */ /* * Set eye & pupil size & position variables. * * eye background is white and pupil black. */ void reset(int x, int y, int w, int h) { rx = w / 2 - 1; ry = h / 2 - 1; midx = pupx = x + rx; midy = pupy = y + ry; rxp = rx / 4; ryp = ry / 4; rx -= (rxp + 2); ry -= (ryp + 2); } /* * Draw eye. */ void draw(Graphics g) { int w = rx + rxp + 2; int h = ry + ryp + 2; g.setColor(Color.white); g.fillOval(midx - w, midy - h, 2*w, 2*h); g.setColor(Color.black); g.drawOval(midx - w, midy - h, 2*w, 2*h); g.drawOval(midx - w, midy - h, 2*w + 1, 2*h + 1); drawPupil(g); } private void drawPupil(Graphics g) { g.setColor(Color.black); g.fillOval(pupx - rxp, pupy - ryp, 2*rxp, 2*ryp); g.setColor(Color.white); g.fillRect(pupx - rxp/2, pupy - ryp/2, rxp/3, ryp/3); } /* * Move pupil to given position. */ private void movePupil(Graphics g, int x, int y) { if ((x == pupx) && (y == pupy)) { return; } g.setColor(Color.white); g.fillOval(pupx - rxp, pupy - ryp, 2*rxp, 2*ryp); pupx = x; pupy = y; drawPupil(g); } /* * move pupil to given angle on eye edge. */ void update(Graphics g, double angle) { int x, y; x = midx - (int) (rx * Math.sin(angle)); y = midy + (int) (ry * Math.cos(angle)); movePupil(g, x, y); this.angle = angle; } /* * Move pupil according to mouse position. */ void update(Graphics g, int mx, int my) { double angle; /* relative to eye middle position */ mx -= midx; my -= midy; /* angle measured clockwise, zero is down */ if (my == 0) { angle = Math.PI / 2.0; } else { angle = Math.atan((double) Math.abs(mx) / (double) Math.abs(my)); } if ((my >= 0) && (mx >= 0)) { angle = -angle; } else if ((my < 0) && (mx < 0)) { angle = Math.PI - angle; } else if ((my < 0) && (mx >= 0)) { angle = -Math.PI + angle; } if (mx < 0) { mx = -mx; } if (mx > rx) { mx = rx; } if (my < 0) { my = -my; } if (my > ry) { my = ry; } mx = midx - (int) (mx * Math.sin(angle)); my = midy + (int) (my * Math.cos(angle)); movePupil(g, mx, my); this.angle = angle; } /* * Return pupil direction. */ double getAngle() { return angle; } } public class Eyes extends java.applet.Applet implements Runnable { protected Eye left; protected Eye right; /* * Initialize the applet. */ public void init() { left = new Eye(); right = new Eye(); } public void run() { /* should this do something? */ } /* * (re-)size and draw the eyes. * * paint() is a virtual Component method (default update() method * uses this after clearing the Component Graphics bg first). */ public void paint(Graphics g) { /* get eye rectangle size for the applet window */ int w = size().width / 2; int h = size().height; left.reset(0, 0, w, h); right.reset(w, 0, w, h); right.draw(g); left.draw(g); } /* * Update pupil positions everytime mouse is moved... */ public boolean mouseMove(java.awt.Event evt, int x, int y) { Graphics g = getGraphics(); left.update(g, x, y); right.update(g, x, y); return true; } /* * if run as executable, needs a frame to run in. */ public static void main(String args[]) { AppletFrame.startApplet(new Eyes(), "Java Eyes!", 120, 90); } } /* Generic Applet to Application Frame * @(#)AppletFrame.java 1.4 02 Dec 1995 15:28:07 * @author Kevin A. Smith * * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved. * * * I modified the original demo/GraphicsTest/AppletFrame.java source (JDK * example) to the needs of Eyes class above. - Eero */ class AppletFrame extends Frame { public static void startApplet(Applet a, String title, int w, int h) { AppletFrame f; a.init(); f = new AppletFrame(title); f.add("Center", a); f.pack(); f.resize(w, h); f.show(); } public AppletFrame(String name) { super(name); } public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) { System.exit(0); return true; } return super.handleEvent(e); } }