Results 1 to 4 of 4
- 11-15-2010, 04:28 PM #1
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
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:
Now I got to do this exercise which saysJava 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 */ }I tried my best to do this exercise but I couldn't.Can anybody give me ideas please how to do this?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.
- 11-15-2010, 05:21 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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?
- 11-15-2010, 05:54 PM #3
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
I try that Tolls,thank you for your help mate.
- 11-16-2010, 08:46 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
Wrong with Rectangle res = new Rectangle(0,0,0,0);???
By jiapei100 in forum AWT / SwingReplies: 3Last Post: 09-25-2010, 03:39 PM -
Zoom on graphic
By onephenom in forum Java 2DReplies: 1Last Post: 03-24-2010, 08:52 PM -
Java ACM graphic program help
By jimmy-lin in forum New To JavaReplies: 0Last Post: 11-16-2009, 04:40 AM -
Graphic library
By xeannart in forum Advanced JavaReplies: 6Last Post: 07-14-2008, 02:36 AM -
How do insert a Graphic
By carl in forum New To JavaReplies: 1Last Post: 08-01-2007, 05:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks