Class Graphics2DTestPlusButton.MovableButtonMouseListener

java.lang.Object
  extended byjavax.swing.event.MouseInputAdapter
      extended byGraphics2DTestPlusButton.MovableButtonMouseListener
All Implemented Interfaces:
java.util.EventListener, javax.swing.event.MouseInputListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener
Enclosing class:
Graphics2DTestPlusButton

public static class Graphics2DTestPlusButton.MovableButtonMouseListener
extends javax.swing.event.MouseInputAdapter

Class MovableButtonMouseListener 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 button to be movable on the drawing canvas.


Field Summary
protected  int normalizeX
          The normalized x coordinate of where the mouse is pressed on the button
protected  int normalizeY
          The normalized y coordinate of where the mouse is pressed on the button
 
Constructor Summary
Graphics2DTestPlusButton.MovableButtonMouseListener()
           
 
Method Summary
 void mouseDragged(java.awt.event.MouseEvent e)
          Drag the movingButton around.
 void mousePressed(java.awt.event.MouseEvent e)
          Respond to the initial mouse press by allocating a visual clone of the button and hiding the button temporarily.
 void mouseReleased(java.awt.event.MouseEvent e)
          Move the original button to the ending position of the movingButton, make the button visible again, and get rid of the movingButton.
 
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
 

Field Detail

normalizeX

protected int normalizeX
The normalized x coordinate of where the mouse is pressed on the button


normalizeY

protected int normalizeY
The normalized y coordinate of where the mouse is pressed on the button

Constructor Detail

Graphics2DTestPlusButton.MovableButtonMouseListener

public Graphics2DTestPlusButton.MovableButtonMouseListener()
Method Detail

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Respond to the initial mouse press by allocating a visual clone of the button and hiding the button temporarily. The visual clone is named "movingButton". Using the visual clone makes it easier to compute drag locations, since the coordinates given to mouseDragged stay relative to the fixed position of the original button. If we try to change the position of the original button as it's being dragged, the normalization arithmetic is a pain.

The normalizeX and Y values are computed because the x/y position of the input MouseEvent is relative to the upper left corner of the button, whereas the x/y position of the button is relative to the upper left corner of the entire panel. Without these values, the mouseDragged logic would not work properly.


mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Drag the movingButton around. The implementation of this is quite simple, given what JFC is doing for us behind the scene. Namely, JFC is calling this method as often as it can while the mouse is moved with the mouse button still pressed.


mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Move the original button to the ending position of the movingButton, make the button visible again, and get rid of the movingButton.

Note that if the button is moved off of the canvas, a new canvas size is not recomputed. This could be done in a similar manner to the canvas size re-computation at the end of the DrawingCanvas.paintComponent method. Alternatively, the button could be kept from moving off of the visible canvas altogether by adding appropriate checking code within the mouseDragged method.