Skip to content

Press JButton to draw a ball, the ball is not visible

So, I want to draw a ball whenever the user presses JButton. My problem is that the ball is not visible after I call revalidate() and repaint(). Am I forgetting something? Here is my code, I have another class for the queue and stack that’s why I extended Queueue. My buttons are visible and I know they work when I press them, it’s jst that the ball doesn’t show up on the screen. Earlier I tried to have my void paintComponent in my ActionListener but it would not work. I then wanted to just call the method but because of the Graphics g parameter it would not work as well. So I saw similar issue where someone suggested to use boolean

public class Main extends Queueue {

static boolean clicked = false;

public void paintComponent(Graphics g) {
    if(clicked) {
        
            g.setColor(Color.BLACK);
            g.fillOval(60, 60, 15, 15);
        
    
    }

    }

public static void main (String[] args) {
    Queueue qq = new Queueue();
    JFrame f = new JFrame();
    JPanel p = new JPanel();
    JButton b1 = new JButton("Queue");
    JButton b2 = new JButton("Stack");
    JButton b3 = new JButton("Ball");
    f.setSize(700, 500);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    p.setBackground(Color.RED);
    p.add(b1);
    p.add(b2);
    p.add(b3);
    f.add(p);
    f.setVisible(true);
    
    b1.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            qq.Q();
            
        }
        
    });
    b2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            qq.S();
            
        }
        
    });

 b3.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            clicked = true;
            f.revalidate();
            f.repaint();
        
            
        }
        
    });

Answer

You had some syntax errors which I corrected. I also trimmed it down to more clearly demonstrate the procedure. Here is a working version that just paints the ball.

public class Main extends JPanel {
    
    static boolean clicked = false;
    
    public static void main(String[] args) {
        
        JFrame f = new JFrame();
        Main m = new Main();
        JButton b3 = new JButton("Ball");
        f.setSize(700, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        f.add(m);
        m.add(b3);
        f.setVisible(true);
        
        b3.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                
                clicked = true;
                f.repaint();
                
            }
        });
        
    }
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (clicked) {
            
            g.setColor(Color.BLACK);
            g.fillOval(60, 60, 15, 15);
            
        }
    }
}
  • Your class should extend JPanel
  • Add it to the JFrame.
  • Add the JButton to Main instance
  • and in paintComponent call super.paintComponent(g) first.

Check out The Java Tutorials for more information on how to paint.

In response to your comments, the following should work. The main issue is that you need to have the paintComponent inside a JPanel, not as a method in Queueue.

public class Main extends Queueue {
    
    static boolean clicked = false;
    
    public static void main(String[] args) {
        
        JFrame f = new JFrame();
        Main m = new Main();
        JPanel panel = new JPanel() {
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (clicked) {
                
                g.setColor(Color.BLACK);
                g.fillOval(60, 60, 15, 15);
                
            }
        }
        };
        JButton b3 = new JButton("Ball");
        f.setSize(700, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        f.add(panel);
        panel.add(b3);
        f.setVisible(true);
        
        b3.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                
                clicked = true;
                f.repaint();
                
            }
        });
        
    }
    
}