Results 1 to 6 of 6
  1. #1
    nnur is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Button click for drawing geometry shape

    I'm really new to Java. I'm building a Swing-bases application. The main modul is about drawing shapes in canvas. I've read some tutorial & samples before, but still get error in first code: my button can't draw geometric shapes. The complete post is here Button click.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hello, welcome to the forum, and thanks for creating a new thread. If you don't get decent help soon, one way to increase your chances is to post your formatted code here with code tags. This will make your code much more readable, and if it's easier to read, more will take the time to read it (makes sense, right?).

    One problem that your code suggests (I'm not sure since the code seems incomplete), is that you're giving your class a Graphics field, g, and then using it to draw, and drawing in Swing just isn't done this way. For one, Graphics objects do not persist but instead get destroyed each time the component is repainted, so if you do ever initialize this object sometime (I don't see that you do) you may be trying to draw with a dead Graphics object. Instead, you'll want to set your boolean to true and then call repaint() on the drawing JPanel. Then in the JPanel's paintComponent method (or myPaint), use the boolean to decide whether or not to draw the oval (Edit: as is shown in my posts in the previous thread where you initially posted this question). I suggest that you go through the Sun Swing and 2D Graphics tutorials as they'll show you most of the gory details.

    Also, if you want to learn Swing coding and are using NetBeans to generate Swing code for you, you are far better off ditching this feature of NetBeans and using the tutorials as a guide, learn to create your own Swing code. You won't regret this decision.

    Much luck and again welcome!
    Last edited by Fubarable; 05-14-2010 at 06:54 AM.

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Oh, wellcome :)

    So you are writing the code like a

    Java Code:
    public class ToolbarKanvas extends javax.swing.JFrame {
    
    boolean pilihOval = false;
    Graphics g;
    
    JPanel KanvasDasar = new JPanel()
    {
    @Override
    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    myPaint(g);
    }
    };
    
    /** Creates new form ToolbarKanvas */
    public ToolbarKanvas() 
    {
    initComponents();
    this.getContentPane().add(KanvasDasar, BorderLayout.CENTER);
    this.pack();
    }
    
    // untuk menggambar geometri
    private void myPaint(Graphics g)
    {
    if (pilihOval)
    {
    g.drawOval(5,5,40,40);
    }
    }
    
    //panggil u/ gambar.
    private void bOvalMouseClicked(java.awt.event.MouseEvent evt) 
    { this.pilihOval = true;
    myPaint(g);
    }

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Emm...

    I think your code doesn't do things with canvas as you may describe us :(

    Watch the code

    Java Code:
    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    myPaint(g);
    }
    };
    ... it shows you try to paint right on your JFrame :)

    So I think you should better modify it first something like ... Just to refresh my fingers...

    Java Code:
    JPanel canvasp=new JPanel()
    {
    public void paintComponent(Graphics g)
    {
     if (pilihOval)
    {
    g.drawOval(5,5,40,40);
    }
    
    }
    
    public void update(){}
    };
    and then...

    Java Code:
    private void bOvalMouseClicked(java.awt.event.MouseEvent evt) 
    { this.pilihOval = true;
      canvasp.repaint();
    }
    Last edited by Webuser; 05-15-2010 at 06:45 AM.
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  5. #5
    nnur is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Sorry for late response. It's weekend (it means: family-time, not working time :)
    Thank you Fubarable. Simply replace with repaint(), and everything's OK. To make a good graduate projects, I'm sure I need to learn a lot as I'm a newbie, but I believe there's nothing that impossible :). And surely, I'll ask to seniors here when I'm getting stuck:)

    About ur suggestion: NetBeans or own-code; some friends suggest me that NetBeans is the best tool for newbie though it generates many many automatic line-code. But actually I need a time to map between the tutorial's logic and the NetBeans' simplicity. The example: Java Tutorial (from Sun'w website) introduce interface ActionListener on using button, but in NetBeans we simply right-click the button and choose the method.

    Hey, Webuser. I've override the method paintComponent in JPanel, not in JFrame :) But thanks for ur help. I think your solution has the same logic with Fubarable's.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by nnur View Post
    Thank you Fubarable. Simply replace with repaint(), and everything's OK.
    Wonderful!

    To make a good graduate projects, I'm sure I need to learn a lot as I'm a newbie, but I believe there's nothing that impossible :). And surely, I'll ask to seniors here when I'm getting stuck:)
    We look forward to your questions, and as you get comfortable, your answers for others' questions.

    About ur suggestion: NetBeans or own-code; some friends suggest me that NetBeans is the best tool for newbie though it generates many many automatic line-code. But actually I need a time to map between the tutorial's logic and the NetBeans' simplicity. The example: Java Tutorial (from Sun'w website) introduce interface ActionListener on using button, but in NetBeans we simply right-click the button and choose the method.
    As long as your actively studying what is going on and how to change behavior, it sounds like you're on the right track.

    Hey, Webuser. I've override the method paintComponent in JPanel, not in JFrame :) But thanks for ur help. I think your solution has the same logic with Fubarable's.
    Indeed it is.

    Best of luck!

Similar Threads

  1. How can I display on Button click?
    By ntagrafix in forum New To Java
    Replies: 3
    Last Post: 11-04-2009, 12:05 AM
  2. How we can open a file with a click in button
    By kostinio in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2009, 04:46 PM
  3. Drawing a shape on an image
    By Yoruichi in forum Java 2D
    Replies: 2
    Last Post: 03-29-2009, 02:49 PM
  4. deselecting a button after the click.
    By ramsrocker in forum New To Java
    Replies: 10
    Last Post: 02-15-2009, 06:52 AM
  5. Shape drawing applet not working...
    By evapisces in forum Java Applets
    Replies: 0
    Last Post: 11-17-2008, 11:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •