Results 1 to 4 of 4
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default Drag a rectangle (graphic program)

    Hi frens,
    This program called DrawRectange is from the book "Art and science of Java" which lets you draw rectangle on canvas by pressing mouse and dragging.The code for this program is as follows:
    Java Code:
    /*
    * File: DrawRectangle.java
    * ------------------------
    * This program allows users to create rectangles on the canvas
    * by clicking and dragging with the mouse.
    */
    import java.awt.event.*;
    import acm.graphics.*;
    import acm.program.*;
    /** This class allows users to drag rectangles on the canvas */
    public class DrawRectangle extends GraphicsProgram {
    /** Runs the program */
    public void run() {
    addMouseListeners();
    }
    /** Called on mouse press to record the starting coordinates */
    public void mousePressed(MouseEvent e) {
    startX = e.getX();
    startY = e.getY();
    currentRect = new GRect(startX, startY, 0, 0);
    currentRect.setFilled(true);
    add(currentRect);
    }
    /** Called on mouse drag to reshape the current rectangle */
    public void mouseDragged(MouseEvent e) {
    double x = Math.min(e.getX(), startX);
    double y = Math.min(e.getY(), startY);
    double width = Math.abs(e.getX() - startX);
    double height = Math.abs(e.getY() - startY);
    currentRect.setBounds(x, y, width, height);
    }
    /* Private state */
    private GRect currentRect; /* The current rectangle */
    private double startX; /* The initial mouse X position */
    private double startY; /* The initial mouse Y position */
    }
    Now I got to do this exercise which says
    Java Code:
    Modify the DrawRectangle program ,so that clicking the mouse
    inside an existing rectangle allows you to drag that rectangle to a new position on the
    canvas.
    I tried my best to do this exercise but I couldn't.Can anybody give me ideas please how to do this?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    You know where on the screen the mouse was clicked (it's there in the code you posted).
    You already have the current rectangle stored (I presume that's what currentRect is).
    SO you should be able to determine if those coordinates for the mouse click are within the rectangle.

    Can you achieve that bit?

  3. #3
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    I try that Tolls,thank you for your help mate.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    How you get it to move after you've clicked on it I can't say accurately because I don't know acm.

Similar Threads

  1. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 03:39 PM
  2. Zoom on graphic
    By onephenom in forum Java 2D
    Replies: 1
    Last Post: 03-24-2010, 08:52 PM
  3. Java ACM graphic program help
    By jimmy-lin in forum New To Java
    Replies: 0
    Last Post: 11-16-2009, 04:40 AM
  4. Graphic library
    By xeannart in forum Advanced Java
    Replies: 6
    Last Post: 07-14-2008, 02:36 AM
  5. How do insert a Graphic
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 05:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •