import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; public abstract class GraphicsFunction implements GraphicsObject , MouseListener , MouseMotionListener { protected Container container = null; protected Point startPoint = new Point(0, 0); protected Point prevPoint = new Point(0, 0); protected Point currPoint = new Point(0, 0); protected Color color = Color.black; protected boolean done = true; //////////////////////////////////////// // MouseListener Methods public void mouseReleased( MouseEvent e ) { currPoint.x = e.getX(); currPoint.y = e.getY(); done = true; draw( ); container.removeMouseListener( this ); container.removeMouseMotionListener( this ); } public void mousePressed( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } //////////////////////////////////////// // MouseMotionListener Methods public void mouseDragged( MouseEvent e ) { prevPoint.x = currPoint.x; prevPoint.y = currPoint.y; currPoint.x = e.getX(); currPoint.y = e.getY(); draw( ); } public void mouseMoved( MouseEvent e ) { } //////////////////////////////////////// // GraphicsFunction Methods public void setColor( Color c ) { color = c; } public void setContainer( Container container ) { this.container = container; this.container.addMouseListener( this ); this.container.addMouseMotionListener( this ); } public void setLocation( int x, int y ) { startPoint.x = x; startPoint.y = y; currPoint.x = x; currPoint.y = y; prevPoint.x = x; prevPoint.y = y; done = false; } public void draw( ) { Graphics g = container.getGraphics( ); if (done == false) { Color background = container.getBackground(); g.setColor( color ); g.setXORMode( background ); drawClear( g ); drawFinal( g ); } else { g.setColor( color ); g.setPaintMode( ); drawFinal( g ); } g.dispose( ); } protected abstract void drawClear( Graphics g ); protected abstract void drawFinal( Graphics g ); }