• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Press a button and draw a shape

 
Abder-Rahman Ali
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code shown below, which will be able to draw a shape once clicking on a button. For example, if you press the "Draw Rectangle" button a rectangle should be drawn and so forth.

I have reached to the point where to determine the button pressed, but UNABLE to add the drawing functionality after that. How can I draw a shape after pressing a button?



Thanks.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Several points, in no particular order.
  • Your class should not extend Thread. Even if it did background processing, extending Thread is almost always the wrong approach. See ExtendingThreadVsImplementingRunnable for more information.
  • This being Swing code, you should not override the paint method; override paintComponent instead. You may want to work through http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html.
  • You should get in the habit of using the @Override annotation for overridden methods.
  • The java.awt.Graphics class has lots of methods that can be used to do the drawing. Such an object is available in the paintComponent method, which is where all drawing should occur - and not in the event handlers.
  • So you need data structures that store all shapes to be drawn - those data structures would be populated in the events handlers, and then used in the paintComponent method. At the end of the event handler, you would call the repaint() method to tell Swing that it needs to repaint the window.
  •  
    Abder-Rahman Ali
    Ranch Hand
    Posts: 138
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you so much for your reply.

    I got what you mean and they are really valuable.

    But, can you just give me a hint on how to work in your last point?

    So you need data structures that store all shapes to be drawn - those data structures would be populated in the events handlers, and then used in the paintComponent method. At the end of the event handler, you would call the repaint() method to tell Swing that it needs to repaint the window.

    Thanks.
     
    Michael Dunn
    Ranch Hand
    Posts: 4632
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    > But, can you just give me a hint on how to work in your last point?

    if you spent more time reading than cross-posting, you might find out the answer for yourself

    http://forums.sun.com/thread.jspa?threadID=5431808&tstart=0

    http://www.java-forums.org/awt-swing/26603-draw-shapes-pressing-button.html

    yes, and we've heard it all before - you want an answer NOW!
     
    Maneesh Godbole
    Saloon Keeper
    Posts: 11072
    13
    Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
     
    pete stein
    Bartender
    Posts: 1561
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Maneesh Godbole wrote:Thanks Michael.

    @Abder,
    Please read http://faq.javaranch.com/java/BeForthrightWhenCrossPostingToOtherSites


    I have asked him to read and acknowledge this article in a different forum (java-forums) where I am moderator, but he has yet to comply. A shame.
     
    Abder-Rahman Ali
    Ranch Hand
    Posts: 138
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm following all the answers, and I really don't know what is the issue posting on all forums.

    I don't think that I should comply with things that actually don't make sense.

    Is there a problem asking more than one person on different forums? If you can help, I would appreciate it, if you cannot, you shouldn't write things beyond the subject.

    I really wish to see something related to Java here...

    Think BIGGER.
     
    Michael Dunn
    Ranch Hand
    Posts: 4632
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    > I'm following all the answers, and I really don't know what is the issue posting on all forums.

    you're not too bright - you only have to read the names of those castigating your actions to realize that
    this and future problems/posts are likely to be totally ignored by those with the better solutions

    > I don't think that I should comply with things that actually don't make sense.

    do whatever you like, just don't expect much future help in any of the forums where you regard the contributors as slaves to your needs

    > Is there a problem asking more than one person on different forums?

    common courtesy - perhaps where you come from, treating people as insignificant is acceptable

    > If you can help, I would appreciate it, if you cannot, you shouldn't write things beyond the subject.

    your problem is so simple that when/if you find the answer, you'll give yourself a triple upper-cut

    > I really wish to see something related to Java here...

    really? then either
    1) buy the site, or
    2) comply with common 'niceties' of forums

    > Think BIGGER

    nah, you're definitely a Mickey Rooney
     
    Maneesh Godbole
    Saloon Keeper
    Posts: 11072
    13
    Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Abder-Rahman Ali wrote:...
    I don't think that I should comply with things that actually don't make sense...
    Is there a problem asking more than one person on different forums?


    Looks like you did not find time to go through the link you were provided, here as well as in the other forum. If you had, you would have understood what is the "problem" . Here is the link again. If I were you, I would go through it.
    http://faq.javaranch.com/java/BeForthrightWhenCrossPostingToOtherSites

    I feel enough has been said about the cross posting policy. If anyone feels need for more discussion, they can open a new thread in the Ranch Office
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic