Advertisement
Guest User

Simple java applet

a guest
Feb 5th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. /**
  6.    This is a simple applet.
  7. */
  8.  
  9. public class WatchMe extends JApplet
  10. {
  11.     private int currentX = 0; //cursor's current X position
  12.     private int currentY = 0; //cursor's current Y position
  13.     private int leyeX = 118; //pupil's default X position
  14.     private int reyeX = 148; //pupil's default X position
  15.     private int leyeY = 68; //pupil's default Y position
  16.     private int reyeY = 68; //pupil's default Y position
  17.    
  18.    public void init()
  19.    {
  20.       getContentPane().setBackground(Color.white);
  21.      
  22.       // Add a mouse motion listener to this applet.
  23.       addMouseMotionListener(new MyMouseMotionListener());
  24.    }
  25.    
  26.    public void paint(Graphics g)
  27.    {
  28.        //Call the superclass paint method.
  29.        super.paint(g);
  30.        //draw a black unfilled rectangle
  31.        g.setColor(Color.black);
  32.        
  33.        //draw the eyes
  34.        g.drawOval(110, 55, 25, 40);
  35.        g.drawOval(140, 55, 25, 40);
  36.        
  37.        //draw the pupils
  38.        g.setColor(Color.black);
  39.        g.fillOval(leyeX, leyeY, 10, 10);
  40.        g.fillOval(reyeX, reyeY, 10, 10);
  41.    }
  42.    
  43.    private class MyMouseMotionListener
  44.                         implements MouseMotionListener
  45.    {
  46.        public void mouseDragged(MouseEvent e)
  47.        {
  48.                currentX = e.getX();
  49.                currentY = e.getY();
  50.                if (currentX > 149)
  51.                 {
  52.                     leyeX = 123;
  53.                     reyeX = 153;
  54.                 }
  55.                if (currentX < 149)
  56.                 {
  57.                     leyeX = 113;
  58.                     reyeX = 143;
  59.                 }
  60.                if (currentY > 99)
  61.                 {
  62.                     leyeY = 73;
  63.                     reyeY = 73;
  64.                 }
  65.                if (currentY < 99)
  66.                 {
  67.                     leyeY = 63;
  68.                     reyeY = 63;
  69.                 }
  70.                
  71.                //repaint the window
  72.                repaint();
  73.                
  74.        }  
  75.        
  76.        public void mouseMoved(MouseEvent e)
  77.        {
  78.                currentX = e.getX();
  79.                currentY = e.getY();
  80.                if (currentX > 149)
  81.                 {
  82.                     leyeX = 123;
  83.                     reyeX = 153;
  84.                 }
  85.                if (currentX < 149)
  86.                 {
  87.                     leyeX = 113;
  88.                     reyeX = 143;
  89.                 }
  90.                if (currentY > 99)
  91.                 {
  92.                     leyeY = 73;
  93.                     reyeY = 73;
  94.                 }
  95.                if (currentY < 99)
  96.                 {
  97.                     leyeY = 63;
  98.                     reyeY = 63;
  99.                 }
  100.                
  101.                //repaint the window
  102.                repaint();
  103.                
  104.        }        
  105.    }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement