Handling the Mouse


Each figure is defined by a "mouse pressed" action, which sets a point named start, and a "mouse released" action, which sets a point named end. When the mouse is released, any figure Painter is capable of drawing can be drawnincluding a line, rectangle, ellipse, and so onbecause start and end define the region in which the figure should be drawn.

When the user presses the mouse, the mouse position is recorded in the start point, and a flag indicating the state of the mouse, mouseUp, is set to false. While the user drags the mouse, Painter will draw the current graphics figure, expanding or reducing it to follow the mouse movements interactively. To translate between window coordinates (used when the mouse is being dragged) and coordinates inside the drawing area (represented by the box in Figure 4.1, which is used when the user releases the mouse button and the image is finalized), the offset of the drawing area will be subtracted from the mouse location in the final figure. Because you don't want that subtraction to be performed as the mouse is dragged, but rather only when the mouse button is released, the code sets a flag named adjusted to true (that is, the image is already adjusted to use window coordinates) and repaints the image. Here's what that looks like in the mousePressed method:

 public void mousePressed(MouseEvent e) {     mouseUp = false;     start = new Point(e.getX(), e.getY());     adjusted=true;     repaint(); } 

When the mouse goes up, the new position is recorded as the end point, mouseUp is set to true, the flag dragging is set to false, and adjusted is set to false (meaning that the offset of the upper left of the drawing area should be subtracted from all drawing operations now that the code will be drawing in the Image object instead of the main window). Then the window, and the image in it, is redrawn to reflect the final figure. Here's what that looks like in the mouseReleased method:

 public void mouseReleased(MouseEvent e) {     if(rounded || line || ellipse || rectangle || draw || text){         end = new Point(e.getX(), e.getY());         mouseUp = true;         dragging = false;         adjusted=false;         repaint();     } } 

If the user drags the mouse, the dragging flag is set to TRue and the image is redrawn to reflect the new position of the mouse. You can also check to be sure that the mouse is inside the drawing area before redrawing the window; here's what that looks like in the mouseDragged method:

 public void mouseDragged(MouseEvent e) {     dragging = true;         .         .         .     if(new Rectangle(offsetX, offsetY,         imageWidth, imageHeight).contains(start.x, start.y)){         end = new Point(e.getX(), e.getY());         repaint();     } } 

In fact, the user can also draw freehand while dragging the mouse. If he's doing that, the code should store all the mouse locations as the mouse is dragged. Painter simply stores those points in an array named dots, shown here:

 public void mouseDragged(MouseEvent e) {     dragging = true;     if(new Rectangle(offsetX, offsetY,         imageWidth, imageHeight).contains(e.getX(), e.getY())){         if(draw){             dot[dots] = new Point(e.getX(), e.getY());             dots++;         }     }     if(new Rectangle(offsetX, offsetY,         imageWidth, imageHeight).contains(start.x, start.y)){         end = new Point(e.getX(), e.getY());         repaint();     } } 

That takes care of tracking the mouse and responding to mouse actions. Now that the start and end points have been determined, specifying the locations at which the user has pressed and released the mouse, respectively, it's time to draw some graphics.



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net