[2] | 1 |
|
---|
| 2 | import java.awt.*;
|
---|
| 3 |
|
---|
| 4 | public class Ogen
|
---|
| 5 | extends java.applet.Applet{
|
---|
| 6 |
|
---|
| 7 | //blx holds previous x-value of of left eye, etc...
|
---|
| 8 | double blx,bly,brx,bry;
|
---|
| 9 | //new values of eyes
|
---|
| 10 | double lx=150, ly=150, rx=260, ry=150;
|
---|
| 11 |
|
---|
| 12 | public boolean mouseMove(Event evt,int x,int y)
|
---|
| 13 | {
|
---|
| 14 | //move to corner of window and calc schale
|
---|
| 15 | double k = schaallengte(x-150,y-150);
|
---|
| 16 | lx = k * (double)(x-150) + 150;
|
---|
| 17 | ly = k * (double)(y-150) + 150;
|
---|
| 18 | //move to corner of window and calc scale
|
---|
| 19 | k = schaallengte(x-260,y-150);
|
---|
| 20 | rx = k * (double)(x-260) + 260;
|
---|
| 21 | ry = k * (double)(y-150) + 150;
|
---|
| 22 | repaint(); // call to update
|
---|
| 23 | return true;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | //get the scale factor to make sure the eyeballs remains in the eye
|
---|
| 27 | //itself, by the concept of Inversive geometry, see:
|
---|
| 28 | //http://en.wikipedia.org/wiki/Inversive_geometry
|
---|
| 29 | public double schaallengte(int x,int y)
|
---|
| 30 | {
|
---|
| 31 | return (double)(38) / Math.sqrt((double)(x*x+y*y+1444));
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public void paint(Graphics g)
|
---|
| 35 | {
|
---|
| 36 | //out black circle of eyes (solid)
|
---|
| 37 | g.setColor(Color.black);
|
---|
| 38 | g.fillOval(100,100,100,100);
|
---|
| 39 | g.fillOval(210,100,100,100);
|
---|
| 40 | //put white circle in there to make the circle non-solid
|
---|
| 41 | g.setColor(Color.white);
|
---|
| 42 | g.fillOval(104,104,92,92);
|
---|
| 43 | g.fillOval(214,104,92,92);
|
---|
| 44 | //draw eyes, calling update might be more handy
|
---|
| 45 | g.setColor(Color.black);
|
---|
| 46 | g.fillOval((int)(blx=lx-8),(int)(bly=ly-8),16,16);
|
---|
| 47 | g.fillOval((int)(brx=rx-8),(int)(bry=ry-8),16,16);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public void update(Graphics g)
|
---|
| 51 | {
|
---|
| 52 | // delete old eyes by painting them white again
|
---|
| 53 | g.setColor(Color.white);
|
---|
| 54 | g.fillOval((int)blx,(int)bly,16,16);
|
---|
| 55 | g.fillOval((int)brx,(int)bry,16,16);
|
---|
| 56 | // set new eyes, the minus 8 will make it a good center, else it
|
---|
| 57 | // will pass the border and whipe the eyeball
|
---|
| 58 | g.setColor(Color.black);
|
---|
| 59 | g.fillOval((int)(blx=lx-8),(int)(bly=ly-8),16,16);
|
---|
| 60 | g.fillOval((int)(brx=rx-8),(int)(bry=ry-8),16,16);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|