Results 1 to 11 of 11
  1. #1
    jujubinaj's Avatar
    jujubinaj is offline Member
    Join Date
    Dec 2010
    Location
    under the sun :)
    Posts
    9
    Rep Power
    0

    Question Why is java paintComponents() not working?

    okay, here's my code. . .

    Java Code:
    public class DrawingPanel extends JPanel {
    
        ClickButton click;    
    
        public DrawingPanel() {
    
            this.setOpaque(true);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            final Graphics2D g2 = (Graphics2D) g;
    
            Ellipse2D oval = new Ellipse2D.Double(5.0, 40.0,
                                                  25.0, 25.0);
            g2.draw(oval);
    
            
            g2.setColor(Color.white);
            g2.fill(oval);
    
    
            click = new ClickButton();
            click.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    g2.draw(anotherCircle());
                    repaint();
                }
            });        
                    
        }
    
        public Ellipse2D anotherCircle() {
            Ellipse2D oval = new Ellipse2D.Double(15.0, 40.0,
                                                  25.0, 25.0);
    
            return oval;
        }
    
    }
    i'm really stuck on this one. what i want is that when i click the button, another circle would be painted but it doesn't work that way. only one circle is shown right after i run this program, and no more. help. :(

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

    Default

    You would never want to create components within the paintComponent method as you do not have absolute control of when or if that method is called, and the method should be as bare and as fast as possible so as not to slow down the drawing of your GUI.

    So create your JButton in the constructor and add it to your JPanel. If you want to be able to display one more circle, then give your class a boolean variable and have that variable set to true on button press and then call repaint(). In the paintComponent method, use an if block that tests this boolean variable, and if true, draw your ellipse.

    If you want draw many random ellipses, then have an ArrayList of Ellipse2D objects or something like that, and on button press, add a new Ellipse2D.Double to the ArrayList. Then iterate through the list in the paintComponent method, drawing each one as you get to it.

  3. #3
    jujubinaj's Avatar
    jujubinaj is offline Member
    Join Date
    Dec 2010
    Location
    under the sun :)
    Posts
    9
    Rep Power
    0

    Default

    okay. i'll try. & thank you for the suggestion! :D

  4. #4
    jujubinaj's Avatar
    jujubinaj is offline Member
    Join Date
    Dec 2010
    Location
    under the sun :)
    Posts
    9
    Rep Power
    0

    Default

    here's what i did. i created a jbutton so when a user clicks the button, the painting would be done.

    Here's my main class:

    Java Code:
    public class MyFrame extends JFrame {
    
        ClickMe click;
        PaintPanel painting;
    
        public static void main(String[] args) {
            MyFrame frame = new MyFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
        public MyFrame() {
            setTitle("State Machine Simulator");
            setSize(700, 425);
            setLayout(null);
            setLocationRelativeTo(null);
            setResizable(false);
    
            JPanel panel = new JPanel();
            panel.setBounds(5, 40, 530, 323);
            panel.setBorder(BorderFactory.createLineBorder(Color.black));
            panel.setBackground(Color.white);
    
            add(panel);
    
            click = new ClickMe();
            click.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    painting = new PaintPanel();
                    add(painting);
                    painting.repaint();       //is this repaint() only?         
                }
            });
    
            add(click.clickMe());
    
            
        }
    
    }
    And here's my painting class:

    Java Code:
    public class PaintPanel extends JPanel {
    
        @Override
        public void paintComponent(Graphics g) {
            this.setOpaque(false);
            super.paintComponent(g);
    
            Graphics2D g2 = (Graphics2D) g;
            Ellipse2D oval = new Ellipse2D.Double(5.0, 40.0,
                                                  25.0, 25.0);
            g2.draw(oval);
            g2.setColor(Color.white);
            g2.fill(oval);
        }
    
    }
    i didn't declare anymore a boolean variable to check if the button is pressed, coz i believe that the button's actionPerformed would do the task. but when i click it, there's no painting shown. is there something that i missed? :confused:

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

    Default

    Quote Originally Posted by jujubinaj View Post
    here's what i did. i created a jbutton so when a user clicks the button, the painting would be done.
    i didn't declare anymore a boolean variable to check if the button is pressed, coz i believe that the button's actionPerformed would do the task. but when i click it, there's no painting shown. is there something that i missed? :confused:
    Well, you didn't take our advice about the boolean for one. The drawing JPanel should be added on GUI creation and should be visible, and it should show an oval when the boolean is true.

    But to fully help you, we need to know your complete program requirements. What are you trying to do exactly? Show a single oval when a button is pressed and do nothing if it is pressed again? Make it disappear when the button is pressed again? Show a random oval when button is pressed and keep showing them with each button press? The more details you provide the greater we can know what you have right and what you have wrong in your code.

  6. #6
    jujubinaj's Avatar
    jujubinaj is offline Member
    Join Date
    Dec 2010
    Location
    under the sun :)
    Posts
    9
    Rep Power
    0

    Default

    i'm trying to paint a single oval after the button is clicked. [this is for practice only, so when i already know how to use paint, i'm going to give the user the right to create lots of ovals in different locations that he wants, like when he clicks the button clickMe, he still has to click a certain location where he wants to put the ovals].

  7. #7
    jujubinaj's Avatar
    jujubinaj is offline Member
    Join Date
    Dec 2010
    Location
    under the sun :)
    Posts
    9
    Rep Power
    0

    Default

    And also. why does paintComponent doesn't work if one still has to click a button. but if it is run directly, it works? :confused:

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

    Default

    You can find lots of examples of drawing shapes in this forum. For instance in this thread I've posted JDrawRect2 for drawing rectangles on a JPanel through a MouseAdapter (combined MouseListener and MouseMotionListener).

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

    Default

    Quote Originally Posted by jujubinaj View Post
    And also. why does paintComponent doesn't work if one still has to click a button. but if it is run directly, it works? :confused:
    paintComponent does work, but you have to code it a bit differently than you're used to.

    Here's another example that is similar to your code above and that uses booleans:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.Ellipse2D;
    import javax.swing.*;
    
    public class DrawOvalOnButtonClick {
       private JPanel mainPanel = new JPanel();
       private PaintPanel paintPanel = new PaintPanel();
       private JToggleButton drawOvalButton = new JToggleButton("Draw Oval");
    
       public DrawOvalOnButtonClick() {
          drawOvalButton.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                paintPanel.setDrawOval(drawOvalButton.isSelected());
                paintPanel.repaint();
             }
          });
          JPanel btnPanel = new JPanel();
          btnPanel.add(drawOvalButton);
    
          paintPanel.setPreferredSize(new Dimension(400, 400));
    
          mainPanel.setLayout(new BorderLayout());
          mainPanel.add(paintPanel, BorderLayout.CENTER);
          mainPanel.add(btnPanel, BorderLayout.SOUTH);
       }
    
       public JPanel getMainPanel() {
          return mainPanel;
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("DrawOvalOnButtonClick");
          frame.getContentPane().add(new DrawOvalOnButtonClick().getMainPanel());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class PaintPanel extends JPanel {
       private Ellipse2D oval = new Ellipse2D.Double(50, 50, 300, 300);
       private boolean drawOval = false;
    
       @Override
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
    
          if (drawOval) {
             Graphics2D g2 = (Graphics2D) g;
             g2.setColor(Color.white);
             g2.fill(oval);
             g2.setColor(Color.black);
             g2.draw(oval);
          }
       }
    
       public boolean isDrawOval() {
          return drawOval;
       }
    
       public void setDrawOval(boolean drawOval) {
          this.drawOval = drawOval;
       }
    
    }

  10. #10
    jujubinaj's Avatar
    jujubinaj is offline Member
    Join Date
    Dec 2010
    Location
    under the sun :)
    Posts
    9
    Rep Power
    0

    Default

    the code you shared works great!!!! :D

    but i just have a question, is there a way that when i toggle off the button, the oval would remain on the panel? :o

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

    Default

    Quote Originally Posted by jujubinaj View Post
    the code you shared works great!!!! :D

    but i just have a question, is there a way that when i toggle off the button, the oval would remain on the panel? :o
    Surely you can figure this out yourself.

Similar Threads

  1. paintComponent vs paintComponents
    By alacn in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 04:33 AM
  2. Working with Alice and Java
    By jaicea in forum New To Java
    Replies: 2
    Last Post: 12-06-2009, 06:57 AM
  3. I am working on java!
    By pawankumarom in forum New To Java
    Replies: 33
    Last Post: 03-06-2009, 12:24 PM
  4. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  5. working with mp3 in java
    By po0oker in forum Advanced Java
    Replies: 1
    Last Post: 10-30-2007, 09:20 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
  •