import java.awt.*; import java.applet.Applet; public class Ojos extends Applet { int mousex=0, mousey=0; String mousemsg = ""; int Mx = -1; int My = -1; int Ancho, AnchoR; int Alto, AltoR ; int OjoIzq, OjoDch; Color Pupila = Color.black; Color Orbita = Color.white; Image Imagen; Graphics OjoCG; Dimension d; public void init () { setBackground(Color.darkGray); d=size(); Ancho = d.width / 3 ; Alto = Ancho + ((Ancho * 8)/ 100) ; OjoIzq = (d.height - Alto) / 2 ; OjoDch = OjoIzq + Ancho + ((Ancho * 10)/100) ; AnchoR = (Ancho * 35) / 100; AltoR = AnchoR + 5; } public void update (Graphics g){ paint (g); } public void paint(Graphics g){ g.setColor(Orbita); g.fillOval(OjoIzq,OjoIzq,Ancho, Alto); g.setColor(Orbita); g.fillOval(OjoDch,OjoIzq,Ancho, Alto); g.setColor(Pupila); g.fillOval(posicionix(Mx),posicioniy(My),AnchoR,AltoR); g.setColor(Pupila); g.fillOval(posiciondx(Mx),posiciondy(My),AnchoR,AltoR); OjoCG = Imagen.getGraphics(); OjoCG.setColor(getBackground()); OjoCG.fillRect(0,0,Ancho,Alto); g.drawImage(Imagen,0,0,this); } public boolean mouseMove(Event evt, int x, int y) { Mx=x; My=y; if ( x < ((d.width * 20)/100)) Mx=(d.width*20)/100 ; else if ( x > (d.width - ((d.width * 20)/100))) Mx = (d.width - ((d.width*20)/100)) ; if ( y < ((d.height * 25)/100)) My= ((d.height*25)/100) ; else if ( y > (d.height - ((d.height * 25)/100))) My = (d.height - ((d.height*25)/100)) ; repaint(); return true; } public boolean mouseDown(Event evt, int x , int y){ if( x > y) Pupila = Color.blue.darker(); if( x < y) Pupila = Color.green.darker(); repaint(); return true; } public boolean mouseExit(Event evt, int x , int y){ Mx=My= -1; if (Mx == -1) Pupila= Color.black; repaint(); return true; } public int posicioniy (int My){ int py=1; if (My == -1) py = OjoIzq + Alto - (Alto / 2); else py = (My/3) + OjoIzq; py = (py > OjoIzq+Alto-AltoR)? (OjoIzq+Alto-AltoR) : py; return py; } public int posicionix (int Mx){ int px=1; if (Mx == -1) px = OjoIzq + ( Ancho / 3); else px = (Mx/4) + OjoIzq; px = (px > OjoIzq+Ancho-AnchoR)? (OjoIzq+Ancho-AnchoR) : px; return px; } public int posiciondy (int My){ int py=1; if (My == -1) py = OjoIzq + Alto - (Alto / 2); else py = (My/3) + OjoIzq; py = (py > OjoIzq+Alto-AltoR)? (OjoIzq+Alto-AltoR) : py; return py; } public int posiciondx (int Mx){ int px=1; if (Mx == -1) px = OjoDch + ( Ancho / 3); else px = (Mx/4) + OjoDch; px = (px > OjoDch+Ancho-AnchoR)? (OjoDch+Ancho-AnchoR) : px; return px; } }