Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2332
  • Last Modified:

How to draw rectangle using JAVA

I want to draw rectangle in java.
This rectangle should be defined by the end points of one of the diagonals.
I need to draw rectangle like this:
                        If I click "Draw Rectangle" button and click first point on the screen, the rectangle formed by clicked point and the current mouse location is then displayed in red until the second mouse click. After that the rectangle is displayed in the standard color.

Could you please tell me how to draw the rectangle using above information in java?
Once I click first mouse click, rectangle formed by mouse click and moving rectangle with mouse until click the second mouse click then the rectangle formed completely.
0
easy_life
Asked:
easy_life
1 Solution
 
objectsCommented:
0
 
HylkeVdSCommented:
And finally, to draw rectangles:
http://java.sun.com/docs/books/tutorial/2d/geometry/primitives.html
Scroll down to the Rectangle bit. You'll need one corner, and the height and width of the rectangle.

So to sum it up:
 * Make a MouseListener (that extends MouseAdapter) to listen for the clicks
 * Make a MouseMotionListener (that extends MouseMotionAdapter) to follow the mouse after the first click.
 * Store the resulting coordinates.
 * Draw the rectangle using those coordinates (in the paint method)

The 2D graphics trail is very helpful if you want to learn more about graphics:
http://java.sun.com/docs/books/tutorial/2d/TOC.html
0
 
easy_lifeAuthor Commented:
Thank you for your help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now