/* * File: MouseEvents.java * * MouseEvents and movable polygons * * Copyright: Northeast Parallel Architectures Center * */ import java.awt.*; import java.awt.event.*; public class MouseEvents extends java.applet.Applet implements MouseListener, MouseMotionListener { // Instance variables: private final int numPoly = 3; // number of movable polygons private MovablePolygon polygon[]; // array of movable polygons private Image buffer; // image object for double buffering private Graphics gOffScreen; // graphics object for double buffering // Mouse handling variables: private int current = -1; // index of polygon being dragged private int hit = -1; // index of polygon containing cursor private int xoffset = 0; // relative x-coordinate of mouse click private int yoffset = 0; // relative y-coordinate of mouse click // Default polygon color: private final Color polyColor = Color.cyan; // Selected polygon color: private final Color polyColorSelect = Color.red; // Applet colors: private final Color bgColor = Color.black; private final Color fgColor = Color.white; public void init() { setBackground( bgColor ); setForeground( fgColor ); // Instantiate polygon array: polygon = new MovablePolygon[ numPoly ]; // Instantiate a square: polygon[0] = new MovablePolygon( 75, 75, 50, 4 ); polygon[0] = ( MovablePolygon ) polygon[0].centerRotate( Math.PI/4 ); polygon[0].setDelta( 2, 3 ); polygon[0].setColor( Color.red ); // Instantiate a regular hexagon: polygon[1] = new MovablePolygon( 250, 60, 50, 6 ); polygon[1].setDelta( -3, 2 ); polygon[1].setColor( Color.blue ); // Instantiate an equilateral triangle: polygon[2] = new MovablePolygon( 60, 250, 50, 3 ); polygon[2].setDelta( -2, 2 ); polygon[2].setColor( Color.green ); // Create an off-screen image for double buffering: buffer = createImage( getSize().width, getSize().height ); // Get off-screen graphics context: gOffScreen = buffer.getGraphics(); // Register the applet to listen for ALL mouse events: addMouseListener( this ); addMouseMotionListener( this ); } // end method init() // Override java.awt.Component.update: public void update( Graphics g ) { // Fill background: gOffScreen.setColor( getBackground() ); gOffScreen.fillRect( 1, 1, getSize().width - 2, getSize().height - 2 ); // Draw boundary: gOffScreen.setColor( getForeground() ); gOffScreen.drawRect( 0, 0, getSize().width - 1, getSize().height - 1 ); paint( gOffScreen ); // Draw the buffer in the applet window: g.drawImage( buffer, 0, 0, this ); } // Override java.awt.Component.paint: public void paint( Graphics g ) { // Paint each polygon: for ( int i = 0; i < numPoly; i++ ) { polygon[i].fill(g); } } // MouseMotionListener event handlers: public void mouseMoved( MouseEvent e ) { hit = -1; for ( int i = 0; i < numPoly; i++ ) { if ( polygon[i].contains( e.getX(), e.getY() ) ) { hit = i; break; } } for ( int i = 0; i < numPoly; i++ ) { polygon[i].setColor( (i == hit) ? polyColorSelect : polyColor ); } repaint(); } public void mouseDragged( MouseEvent e ) { if ( current != -1 ) { int x = e.getX() + xoffset; int y = e.getY() + yoffset; polygon[current].translate( x, y ); repaint(); } } // MouseListener event handlers: public void mousePressed( MouseEvent e ) { current = hit; if ( current != -1 ) { xoffset = polygon[current].getBounds().x - e.getX(); yoffset = polygon[current].getBounds().y - e.getY(); } } public void mouseReleased( MouseEvent e ) { current = -1; } public void mouseClicked( MouseEvent e ) { } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } } // end class MouseEvents