This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Draw rectangle by dragging mouse

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Marshal
Posts: 80071
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80071
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80071
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I hadn't noticed we were using SWT. Sorry.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic