File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Press a button and draw a shape Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Press a button and draw a shape" Watch "Press a button and draw a shape" New topic
Author

Press a button and draw a shape

Abder-Rahman Ali
Ranch Hand

Joined: May 22, 2008
Posts: 138
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
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  72
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

    Joined: May 22, 2008
    Posts: 138
    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

    Joined: Jun 09, 2003
    Posts: 4632
    > 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

    Joined: Jul 26, 2007
    Posts: 10535
        
        9

    Thanks Michael.

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


    [How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
    pete stein
    Bartender

    Joined: Feb 23, 2007
    Posts: 1561
    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

    Joined: May 22, 2008
    Posts: 138
    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

    Joined: Jun 09, 2003
    Posts: 4632
    > 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

    Joined: Jul 26, 2007
    Posts: 10535
        
        9

    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Press a button and draw a shape