aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Draw rectangle by dragging mouse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Draw rectangle by dragging mouse" Watch "Draw rectangle by dragging mouse" New topic
Author

Draw rectangle by dragging mouse

Dheeraj Kasar
Greenhorn

Joined: Jul 14, 2010
Posts: 2
Hi all,
I want to draw a rectangle by dragging mouse using SWT. I am getting co-ordinates of starting point and endpoint but rectangle is not drawn.
Please help me about this.
Thanks in advance...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
Welcome to the Ranch

Can you print out the coordinates? Presumably yes.
How are you doing the drawing? Which Graphics object are you using?
Dheeraj Kasar
Greenhorn

Joined: Jul 14, 2010
Posts: 2
Here..I am posting my code...I have modified it. It is now displaying rectangle but during dragging mouse it is showing all mouse moves.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
How are you calling the makeRectangle method? Are you calling it from paintComponent? What's GC? Is it some sort of Graphics object? How did you write your PaintListener class?

Last time I had that exercise I created a Shape interfaceand a Rectangle class which implemented that interface and I put the object into a List and drew all the elements in that List in the paintComponent method. Of course, there is a Graphics object implicitly passed to the paintComponent method, ready for you to use. What about calling repaint() on the display from your mouse listener methods?

Please note the code button makes your posts look much better. I have edited your post, since you are new, to show it to full effect.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Campbell Ritchie wrote:How are you calling the makeRectangle method? Are you calling it from paintComponent? What's GC? Is it some sort of Graphics object? How did you write your PaintListener class?
...


I'm not sure if these concerns are relevant in GUI programs that use the SWT library, but I must plead ignorance since I don't use this library.

Pete
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
Thank you. I hadn't noticed we were using SWT. Sorry.
 
Consider Paul's rocket mass heater.
 
subject: Draw rectangle by dragging mouse