home *** CD-ROM | disk | FTP | other *** search
/ Internet Gallery / INTERGAL.bin / intergal / prgs / idv21 / data.z / javaeyes.java < prev    next >
Text File  |  1995-10-08  |  4KB  |  187 lines

  1. import java.awt.Graphics;
  2. import java.awt.Font;
  3. import java.awt.Color;
  4. import java.awt.Event;
  5.  
  6. /*
  7.  
  8. JavaEyes  version 1.0
  9.  
  10. Written by  Arjan van de Ven
  11. Burg.Schafratstr. 38
  12. 5427 SR  Boekel
  13. The Netherlands
  14. +31-4922-322320
  15.  
  16. arjan@stack.urc.tue.nl
  17.  
  18.  
  19. This applet is released to the Public Domain
  20.  
  21. */
  22.  
  23.  
  24. public class Javaeyes extends java.applet.Applet implements Runnable 
  25.    {
  26.  
  27.    Thread runner;
  28.    int    lX = 40;
  29.    int    lY = 40;
  30.    int    rX = 70;
  31.    int    rY = 40;
  32.    int    lEyeX=-1,rEyeX=-1,lEyeY=-1,rEyeY=-1;
  33.    boolean    rPoked=false, lPoked=false;
  34.    boolean    sleeping = false;
  35.    
  36.    public void start() 
  37.       {
  38.       if (runner==null) 
  39.          {
  40.          runner = new Thread(this);
  41.          runner.start();
  42.          }
  43.       }
  44.    public void stop() 
  45.       {
  46.       if (runner!=null) 
  47.          {
  48.          runner.stop(); runner=null;
  49.          }
  50.       }
  51.    public void run() 
  52.       {
  53.       while (true) 
  54.          {
  55.          try 
  56.             {
  57.             Thread.sleep(1000); 
  58.             }
  59.          catch ( InterruptedException e) 
  60.             {
  61.             }
  62.          }
  63.       }
  64.    
  65.    public void paint(Graphics g) 
  66.       {
  67.       if (lPoked && (sleeping==false)) 
  68.          {
  69.          g.setColor(ALeftpushcolor);
  70.          }
  71.       else
  72.          {
  73.          g.setColor(ALeftopencolor);
  74.          }
  75.       g.fillOval(lX+1,lY+1,19,19);
  76.       if (rPoked && (sleeping==false)) 
  77.          {
  78.          g.setColor(ARightpushcolor);
  79.          }
  80.       else
  81.          {
  82.          g.setColor(ARightopencolor);
  83.          }
  84.       g.fillOval(rX+1,rY+1,19,19);
  85.       g.setColor(Color.black);
  86.       g.drawOval(lX,lY,20,20);
  87.       g.drawOval(rX,rY,20,20);
  88.       if ((sleeping == false) && (lEyeX>=0)) 
  89.          {
  90.          g.fillOval(lEyeX,lEyeY,6,6);
  91.          g.fillOval(rEyeX,rEyeY,6,6);
  92.          }
  93.       else
  94.          {
  95.          //g.drawLine(lX,lY+10,lX+20,lY+10);
  96.          //g.drawLine(rX,rY+10,rX+20,rY+10);
  97.          g.drawArc(rX,rY+4,20,10,0,-180);
  98.          g.drawArc(lX,lY+4,20,10,0,-180);
  99.          }
  100.       }
  101.    public void update(Graphics g) 
  102.       {
  103.       paint(g);
  104.       }
  105.    
  106.    public boolean mouseDown(Event evt, int x, int y)
  107.       {
  108.       float dx,dy;
  109.       float length;
  110.       dx = 1.0f * (x - lX-10);
  111.       dy = 1.0f * (y - lY-10);
  112.       length = (float) (Math.sqrt(dx*dx + dy*dy));
  113.       if (length<8) 
  114.          {
  115.          lPoked = true;
  116.          repaint();
  117.          }
  118.       ;
  119.       dx = 1.0f * (x - rX-10);
  120.       dy = 1.0f * (y - rY-10);
  121.       length = (float) (Math.sqrt(dx*dx + dy*dy));
  122.       if (length<8) 
  123.          {
  124.          rPoked = true;repaint();
  125.          }
  126.       ;
  127.       return false;
  128.       }
  129.    
  130.    public boolean mouseMove(Event evt, int x, int y)
  131.       {
  132.       float dx,dy;
  133.       float length;
  134.       int _lEyeX,_lEyeY;
  135.       int _rEyeX,_rEyeY;
  136.       dx = 1.0f * (x - lX-10);
  137.       dy = 1.0f * (y - lY-10);
  138.       length = (float) (Math.sqrt(dx*dx + dy*dy));
  139.       if (length>7) 
  140.          {
  141.          dx = dx * (7 / length);
  142.          dy = dy * (7 / length);
  143.          }
  144.       _lEyeX = (int) (lX + dx+10-2);
  145.       _lEyeY = (int) (lY + dy+10-2);
  146.       dx = 1.0f * (x - rX-10);
  147.       dy = 1.0f * (y - rY-10);
  148.       length = (float) (Math.sqrt(dx*dx + dy*dy));
  149.       if (length>7) 
  150.          {
  151.          dx = dx * (7 / length);
  152.          dy = dy * (7 / length);
  153.          }
  154.       _rEyeX = (int) (rX + dx+10-2);
  155.       _rEyeY = (int) (rY + dy+10-2);
  156.       if ((_rEyeX != rEyeX) || (_rEyeY != rEyeY) ||
  157.       (_lEyeX != lEyeX) || (_lEyeY != lEyeY)) 
  158.          {
  159.          rEyeX = _rEyeX;
  160.          rEyeY = _rEyeY;
  161.          lEyeX = _lEyeX;
  162.          lEyeY = _lEyeY;
  163.          repaint();
  164.          }
  165.       
  166.       return false;
  167.       }
  168.    
  169.    public boolean mouseEnter(Event evt, int x, int y) 
  170.       {
  171.       sleeping = false;
  172.       repaint();
  173.       return false;
  174.       }
  175.    
  176.    public boolean mouseExit(Event evt, int x, int y) 
  177.       {
  178.       sleeping = true;
  179.       repaint();
  180.       lPoked =false;
  181.       rPoked = false;
  182.       return false;
  183.       }
  184.    
  185.    }
  186.  
  187.