1  /*
  2   *  File:  mEventApplet.java
  3   *
  4   *  MouseEvents and movable objects
  5   */
  6  
  7  import java.applet.*;
  8  import java.awt.*;
  9  import java.awt.event.*;
 10  
 11  public class mEventApplet extends Applet 
 12                            implements MouseListener, MouseMotionListener {
 13                            
 14    // Instance variables:
 15    private int numObj = 3;       // number of movable objects
 16    private mPoint object[];      // array of movable objects
 17    private Image buffer;         // image object for double buffering
 18    private Graphics gOffScreen;  // graphics object for double buffering
 19    
 20    // Mouse handling variables:
 21    private int current = -1;
 22    private int hit = -1;
 23    private int xoffset = 0;
 24    private int yoffset = 0;
 25    
 26    // Colors:
 27    private Color objColor = Color.cyan;
 28    private Color objColorSelect = Color.red;
 29    private Color bgColor = Color.black;
 30    private Color fgColor = Color.white;
 31  
 32    public void init() {
 33      setBackground( bgColor );
 34      setForeground( fgColor );
 35  
 36      // Instantiate object array:
 37      object = new mPoint[ numObj ];
 38  
 39      // Instantiate a rectangle:
 40      object[0] = new mRectangle( 10, 10, 100, 100 );
 41      object[0].setColor( objColor );
 42  
 43      // Instantiate a circle:
 44      object[1] = new mOval( 200, 10, 100, 100 );
 45      object[1].setColor( objColor );
 46  
 47      // Instantiate a triangle:
 48      object[2] = new mTriangle( 10, 200, 100, 100 );
 49      object[2].setColor( objColor );
 50  
 51      // Create an off-screen image for double buffering:
 52      buffer = createImage( getSize().width, getSize().height );
 53      // Get off-screen graphics context:
 54      gOffScreen = buffer.getGraphics(); 
 55  
 56      // Register the applet to listen for ALL mouse events:
 57      addMouseListener( this );
 58      addMouseMotionListener( this );
 59    }
 60  
 61    // Override java.awt.Component.update:
 62    public void update( Graphics g ) {
 63      // Fill background:
 64      gOffScreen.setColor( getBackground() );
 65      gOffScreen.fillRect( 1, 1, getSize().width - 2, getSize().height - 2 );
 66      // Draw boundary:
 67      gOffScreen.setColor( getForeground() );
 68      gOffScreen.drawRect( 0, 0, getSize().width - 1, getSize().height - 1 );
 69      paint( gOffScreen );
 70      
 71      // Draw the buffer in the applet window:
 72      g.drawImage( buffer, 0, 0, this );
 73    }
 74    
 75    // Override java.awt.Component.paint:
 76    public void paint( Graphics g ) {
 77      // Paint each object:
 78      for ( int i = 0; i < numObj; i++ ) {
 79        object[i].paint(g);
 80      }
 81    }
 82  
 83    // MouseMotionListener event handlers:
 84    public void mouseMoved( MouseEvent e ) {
 85      hit = -1;
 86      for ( int i = 0; i < numObj; i++ ) {
 87        if ( object[i].isInside( e.getX(), e.getY() ) ) {
 88          hit = i; break;
 89        }
 90      }
 91      
 92      for ( int i = 0; i < numObj; i++ ) {
 93        object[i].setColor( (i == hit) ? objColorSelect : objColor );
 94      }
 95      repaint();
 96    }
 97    public void mouseDragged( MouseEvent e ) {
 98      if ( current != -1 ) {
 99        int x = e.getX() + xoffset;
100        int y = e.getY() + yoffset;
101        object[current].setPoint( x, y );
102        repaint();
103      }
104    }
105  
106    // MouseListener event handlers:
107    public void mousePressed( MouseEvent e ) {
108      current = hit;
109      if ( current != -1 ) {
110        xoffset = object[current].getX() - e.getX();
111        yoffset = object[current].getY() - e.getY();
112      }
113    }
114    public void mouseReleased( MouseEvent e ) {
115      current = -1;
116    }
117    public void mouseClicked( MouseEvent e ) { }
118    public void mouseEntered( MouseEvent e ) { }
119    public void mouseExited( MouseEvent e ) { }
120  
121  }
122