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