Class Graphics2DTestPlusUserDrawing.RectDrawingListener

java.lang.Object
  extended byjavax.swing.event.MouseInputAdapter
      extended byGraphics2DTestPlusUserDrawing.RectDrawingListener
All Implemented Interfaces:
java.util.EventListener, javax.swing.event.MouseInputListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener
Enclosing class:
Graphics2DTestPlusUserDrawing

public static class Graphics2DTestPlusUserDrawing.RectDrawingListener
extends javax.swing.event.MouseInputAdapter

Class RectDrawingListener extends JFC's MouseInputAdpater to provide handlers for three mouse events: mouse pressed, mouse dragged, and mouse released. The comments for the event-handling methods describe in detail what each method does to allow the user to draw rectangles.

The general drawing strategy is to add and remove rectangles from the canvas shapeList as the mouse is being dragged, repainting the canvas at each addition. Since repainting is done efficiently by JFC, this technique is visually OK even if the canvas shapeList contains a large number of other rectangles to be drawn.


Constructor Summary
Graphics2DTestPlusUserDrawing.RectDrawingListener()
           
 
Method Summary
 void mouseDragged(java.awt.event.MouseEvent e)
          Remove the previously drawn rect and add a new one that extends from the starting point to the current mouse position.
 void mousePressed(java.awt.event.MouseEvent e)
          Record the initial mouse press as the starting point of the rectangle to be drawn.
 void mouseReleased(java.awt.event.MouseEvent e)
          Remove the previously drawn rect and add a new rect in its final position.
 
Methods inherited from class javax.swing.event.MouseInputAdapter
mouseClicked, mouseEntered, mouseExited, mouseMoved
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Graphics2DTestPlusUserDrawing.RectDrawingListener

public Graphics2DTestPlusUserDrawing.RectDrawingListener()
Method Detail

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Record the initial mouse press as the starting point of the rectangle to be drawn. Add a single-point rectangle to the shapeList to start off the add/remove drawing process. In this way, mouseDragged will do a remove first, then add a new rect.


mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Remove the previously drawn rect and add a new one that extends from the starting point to the current mouse position. Then call canvas.repaint, which will efficiently redraw the entire canvas, including all of the other items in the shapeList. The actual repaint method being invoked is the one inherited from awt.Component.


mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Remove the previously drawn rect and add a new rect in its final position. Then call repaint.