Looking for simple rectangle drawing source code

R

Ramon

I need to write a very simple "visual" widget drawing application.

The app user will select an item from a palette and then
by click and drag, create a rectangle.

This must be the most common (after a simple click) operation
in all the GUI environments.

Anyway, I am looking for the code that implements the
reactangle drawing/erasing as the mouse is dragged around.
Any pointers??

TIA,

-Ramon
 
K

klynn47

You might look at MouseMotionListener and MouseListener. The method for
drawing a rectangle is simple. The method is drawRect and accepts 4
parameters. The first two are the x and y coordinates of the upper left
corner of the rectangle. The third is the width, and the fourth is the
height.
 
A

Andrei Kouznetsov

I need to write a very simple "visual" widget drawing application.
The app user will select an item from a palette and then
by click and drag, create a rectangle.

This must be the most common (after a simple click) operation
in all the GUI environments.

Anyway, I am looking for the code that implements the
reactangle drawing/erasing as the mouse is dragged around.
Any pointers??

it is very easy...

while mouse dragging you make following:

first getGraphics from your Component or Canvas or whatever.
then you set XOR painting mode on that Graphics
then if there exist saved(!) rectangle you draw it.
then you draw current rectangle.
then you save your current rectangle.
and dispose Graphics

thats it!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top