1 /* 2 * File: MouseEvents.java 3 * 4 * MouseEvents and movable polygons 5 * 6 * Copyright: Northeast Parallel Architectures Center 7 * 8 */ 9 10 import java.awt.*; 11 import java.awt.event.*; 12 13 public class MouseEvents extends java.applet.Applet 14 implements MouseListener, MouseMotionListener { 15 16 // Instance variables: 17 private final int numPoly = 3; // number of movable polygons 18 private MovablePolygon polygon[]; // array of movable polygons 19 private Image buffer; // image object for double buffering 20 private Graphics gOffScreen; // graphics object for double buffering 21 22 // Mouse handling variables: 23 private int current = -1; // index of polygon being dragged 24 private int hit = -1; // index of polygon containing cursor 25 private int xoffset = 0; // relative x-coordinate of mouse click 26 private int yoffset = 0; // relative y-coordinate of mouse click 27 28 // Default polygon color: 29 private final Color polyColor = Color.cyan; 30 // Selected polygon color: 31 private final Color polyColorSelect = Color.red; 32 // Applet colors: 33 private final Color bgColor = Color.black; 34 private final Color fgColor = Color.white; 35 36 public void init() { 37 38 setBackground( bgColor ); 39 setForeground( fgColor ); 40 41 // Instantiate polygon array: 42 polygon = new MovablePolygon[ numPoly ]; 43 44 // Instantiate a square: 45 polygon[0] = new MovablePolygon( 75, 75, 50, 4 ); 46 polygon[0] = ( MovablePolygon ) polygon[0].centerRotate( Math.PI/4 ); 47 polygon[0].setDelta( 2, 3 ); 48 polygon[0].setColor( Color.red ); 49 50 // Instantiate a regular hexagon: 51 polygon[1] = new MovablePolygon( 250, 60, 50, 6 ); 52 polygon[1].setDelta( -3, 2 ); 53 polygon[1].setColor( Color.blue ); 54 55 // Instantiate an equilateral triangle: 56 polygon[2] = new MovablePolygon( 60, 250, 50, 3 ); 57 polygon[2].setDelta( -2, 2 ); 58 polygon[2].setColor( Color.green ); 59 60 // Create an off-screen image for double buffering: 61 buffer = createImage( getSize().width, getSize().height ); 62 // Get off-screen graphics context: 63 gOffScreen = buffer.getGraphics(); 64 65 // Register the applet to listen for ALL mouse events: 66 addMouseListener( this ); 67 addMouseMotionListener( this ); 68 69 } // end method init() 70 71 // Override java.awt.Component.update: 72 public void update( Graphics g ) { 73 // Fill background: 74 gOffScreen.setColor( getBackground() ); 75 gOffScreen.fillRect( 1, 1, getSize().width - 2, getSize().height - 2 ); 76 // Draw boundary: 77 gOffScreen.setColor( getForeground() ); 78 gOffScreen.drawRect( 0, 0, getSize().width - 1, getSize().height - 1 ); 79 paint( gOffScreen ); 80 81 // Draw the buffer in the applet window: 82 g.drawImage( buffer, 0, 0, this ); 83 } 84 85 // Override java.awt.Component.paint: 86 public void paint( Graphics g ) { 87 // Paint each polygon: 88 for ( int i = 0; i < numPoly; i++ ) { 89 polygon[i].fill(g); 90 } 91 } 92 93 // MouseMotionListener event handlers: 94 public void mouseMoved( MouseEvent e ) { 95 hit = -1; 96 for ( int i = 0; i < numPoly; i++ ) { 97 if ( polygon[i].contains( e.getX(), e.getY() ) ) { 98 hit = i; break; 99 } 100 } 101 102 for ( int i = 0; i < numPoly; i++ ) { 103 polygon[i].setColor( (i == hit) ? polyColorSelect : polyColor ); 104 } 105 repaint(); 106 } 107 public void mouseDragged( MouseEvent e ) { 108 if ( current != -1 ) { 109 int x = e.getX() + xoffset; 110 int y = e.getY() + yoffset; 111 polygon[current].translate( x, y ); 112 repaint(); 113 } 114 } 115 116 // MouseListener event handlers: 117 public void mousePressed( MouseEvent e ) { 118 current = hit; 119 if ( current != -1 ) { 120 xoffset = polygon[current].getBounds().x - e.getX(); 121 yoffset = polygon[current].getBounds().y - e.getY(); 122 } 123 } 124 public void mouseReleased( MouseEvent e ) { 125 current = -1; 126 } 127 public void mouseClicked( MouseEvent e ) { } 128 public void mouseEntered( MouseEvent e ) { } 129 public void mouseExited( MouseEvent e ) { } 130 131 } // end class MouseEvents 132