/* Copyright F. P. Marin ( Mon Dec 28 16:18:46 1998 ) E-mail: felix@bloch.ciens.ucv.ve Permission to use, copy, modify and distribute this software and its documentation for NON-COMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies. F. P. Marin makes no representations or warranties about the suitability of the software, either express or implied, including but no limited to the implied warranties of merchantability, fitness for a particular purpose, or non-infringement. F. P. Marin shall not be liable for any damage suffered by license as a result of using, modifying or distributing this software or its derivatives. HTML code is: */ import java.awt.*; import java.awt.event.*; import java.util.Random; public class Eyes extends java.applet.Applet { Eye ey[]=new Eye[2]; Graphics theg; Image theimg; int b,dHair=4,g,h,xiHair,xfHair,r,w; ThickArc eyebrow[]=new ThickArc[2],mouth; Orbit orbt[]=new Orbit[2]; Random rndm=new Random(); Rectangle horizNose=new Rectangle(),vertNose=new Rectangle(); public void init() { h=size().height; w=size().width; xiHair=(int)(0.1*w + 0.5); xfHair=w - xiHair; orbt[0]=new Orbit((int)(w/4.0 + 0.5),(int)(h/2.0 + 0.5),this); ey[0]=new Eye(orbt[0]); int x=(int)(orbt[0].x - 0.75*orbt[0].maxRadius + 0.5); int y=(int)(orbt[0].y - 1.75*orbt[0].maxRadius + 0.5); int width=(int)(2.0*(orbt[0].x - x) + 0.5); eyebrow[0]=new ThickArc(x,y,width,60,180,180,5, Color.green.brighter()); orbt[1]=new Orbit(w - orbt[0].x,orbt[0].y,this); ey[1]=new Eye(orbt[1]); x+=orbt[1].x - orbt[0].x; eyebrow[1]=new ThickArc(x,y,width,60,180,180,5, Color.cyan.brighter()); x=(int)(0.1*w + 0.5); y=(int)(orbt[0].y + 0.075*(h - orbt[0].y) + 0.5); mouth=new ThickArc(x,y,(int)(w - 2.0*x + 0.5),160,180,180,10, Color.red.brighter()); vertNose.width=20; vertNose.x=(int)((w - vertNose.width)/2.0 + 0.5); vertNose.y=orbt[0].y - orbt[0].minRadius; vertNose.height=(int)(2.1*orbt[0].maxRadius + 0.5); horizNose.width=(int)(0.4*w + 0.5); horizNose.x=(int)((w - horizNose.width)/2.0 + 0.5); horizNose.y=vertNose.y + vertNose.height; horizNose.height=20; theimg=createImage(w,h); theg=theimg.getGraphics(); repaint(); } public void update(Graphics g) { theg.setColor(Color.white.brighter()); theg.fillRect(0,0,w,h); drawHair(theg); drawNose(theg); theg.setColor(Color.red.brighter()); theg.drawRect(0,0,w - 1,h - 1); orbt[0].draw(theg); orbt[1].draw(theg); ey[0].draw(theg); ey[1].draw(theg); mouth.draw(theg); eyebrow[0].draw(theg); eyebrow[1].draw(theg); paint(g); } public void paint(Graphics g) { g.drawImage(theimg,0,0,this); } public final void drawHair(Graphics g) { for ( int i=xiHair ; i0.0 ) { xMouse/=m; yMouse/=m; x=(int)(mother.x + dist*xMouse + 0.5); y=(int)(mother.y + dist*yMouse + 0.5); return true; } return false; } static private double magnitude(double x0,double y0) { x0=Math.abs(x0); y0=Math.abs(y0); if ( x00.0 ) { y0/=x0; x0*=Math.sqrt(1.0 + y0*y0); } return x0; } } class Orbit { final double twopi=2.0*Math.PI; Eyes mother; double r,angle; int i,maxRadius,minRadius,x,xDot,y,yDot; public Orbit(int x,int y,Eyes mother) { this.x=x; this.y=y; this.mother=mother; maxRadius=(int)((mother.w + mother.h)/9.0 + 0.5); maxRadius=(int)(0.95*maxRadius + 0.5); minRadius=maxRadius - 10; } public void draw(Graphics g) { g.setColor(Color.black.brighter()); g.fillOval(x - maxRadius,y - maxRadius,2*maxRadius,2*maxRadius); g.setColor(Color.white.brighter()); g.fillOval(x - minRadius,y - minRadius,2*minRadius,2*minRadius); g.setColor(Color.red.brighter()); for ( i=0 ; i<10 ; ++i ) { r=Math.sqrt(mother.rndm.nextDouble())*minRadius; angle=mother.rndm.nextDouble()*twopi; xDot=x + (int)(r*Math.cos(angle)); yDot=y + (int)(r*Math.sin(angle)); g.fillOval(xDot - 3,yDot - 3,6,6); } } } class ThickArc extends Rectangle { Color color; int startAngle,arcAngle,repeat; public ThickArc(int x,int y,int w,int h,int sa,int aa,int repeat, Color color) { super(x,y,w,h); startAngle=sa; arcAngle=aa; this.repeat=repeat; this.color=color; } public void draw(Graphics g) { int yc=y; g.setColor(color); for ( int i=0 ; i