1 /* 2 * 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 paint( g ); 64 } 65 66 // Override java.awt.Component.paint: 67 public void paint( Graphics g ) { 68 // Fill background: 69 gOffScreen.setColor( getBackground() ); 70 gOffScreen.fillRect( 1, 1, getSize().width - 2, getSize().height - 2 ); 71 // Draw boundary: 72 gOffScreen.setColor( getForeground() ); 73 gOffScreen.drawRect( 0, 0, getSize().width - 1, getSize().height - 1 ); 74 75 // Paint each object: 76 for ( int i = 0; i < numObj; i++ ) { 77 object[i].paint(gOffScreen); 78 79 // Draw the buffer in the applet window: 80 g.drawImage( buffer, 0, 0, this ); 81 } 82 } 83 84 // MouseMotionListener event handlers: 85 public void mouseMoved( MouseEvent e ) { 86 hit = -1; 87 for ( int i = 0; i < numObj; i++ ) { 88 if ( object[i].isInside( e.getX(), e.getY() ) ) { 89 hit = i; break; 90 } 91 } 92 93 for ( int i = 0; i < numObj; i++ ) { 94 object[i].setColor( (i == hit) ? objColorSelect : objColor ); 95 } 96 repaint(); 97 } 98 public void mouseDragged( MouseEvent e ) { 99 if ( current != -1 ) { 100 int x = e.getX() + xoffset; 101 int y = e.getY() + yoffset; 102 object[current].setPoint( x, y ); 103 repaint(); 104 } 105 } 106 107 // MouseListener event handlers: 108 public void mousePressed( MouseEvent e ) { 109 current = hit; 110 if ( current != -1 ) { 111 xoffset = object[current].getX() - e.getX(); 112 yoffset = object[current].getY() - e.getY(); 113 } 114 } 115 public void mouseReleased( MouseEvent e ) { 116 current = -1; 117 } 118 public void mouseClicked( MouseEvent e ) { } 119 public void mouseEntered( MouseEvent e ) { } 120 public void mouseExited( MouseEvent e ) { } 121 122 } 123