Java Reference
In-Depth Information
Example 13−4: ScribbleDragAndDrop.java (continued)
int numScribbles = scribbles.size();
for(int i = 0; i < numScribbles; i++) {
Scribble s = (Scribble)scribbles.get(i);
g2.draw(s);
// Draw the scribble
}
}
public void setDragMode(boolean dragMode) {
this.dragMode = dragMode;
}
public boolean getDragMode() { return dragMode; }
/**
* This method, and the following four methods are from the MouseListener
* interface. If we're in drawing mode, this method handles mouse down
* events and starts a new scribble.
**/
public void mousePressed(MouseEvent e) {
if (dragMode) return;
currentScribble = new Scribble();
scribbles.add(currentScribble);
currentScribble.moveto(e.getX(), e.getY());
}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
/**
* This method and mouseMoved() below are from the MouseMotionListener
* interface. If we're in drawing mode, this method adds a new point
* to the current scribble and requests a redraw
**/
public void mouseDragged(MouseEvent e) {
if (dragMode) return;
currentScribble.lineto(e.getX(), e.getY());
repaint();
}
public void mouseMoved(MouseEvent e) {}
/**
* This method implements the DragGestureListener interface. It will be
* invoked when the DragGestureRecognizer thinks that the user has
* initiated a drag. If we're not in drawing mode, then this method will
* try to figure out which Scribble object is being dragged, and will
* initiate a drag on that object.
**/
public void dragGestureRecognized(DragGestureEvent e) {
// Don't drag if we're not in drag mode
if (!dragMode) return;
// Figure out where the drag started
MouseEvent inputEvent = (MouseEvent) e.getTriggerEvent();
int x = inputEvent.getX();
int y = inputEvent.getY();
// Figure out which scribble was clicked on, if any by creating a
// small rectangle around the point and testing for intersection.
Search WWH ::




Custom Search