0

I have a JPanel that has a button which when pressed is to create a new circle. However, that doesn't happen. A new circle doesn't appear and when I resize a window the existing circle breaks, the clone of the button appears on the other side of the panel and everything freezes.

class Panel extends JPanel  {

    private JButton button;
    private Ellipse2D.Double[] circles;
    Integer count;

    public Panel()  {
            setup();
    }

    private void setup()  {
            count=new Integer(0);
            circles=new Ellipse2D.Double[10];
            button=new JButton(count.toString());
            button.addActionListener(new ActionListener() {
               @Override
               public void actionPerformed(ActionEvent e) {
                           circles[count]=new 
                              Ellipse2D.Double(10, 10, 100, 100);
                           count++;
                           button.setText(count.toString());
                    }
                });

               add(button);
        }


    public void paintComponent(Graphics g)  {
            super.paintComponent(g);
            paintStuff(g);
        }

    private void paintStuff(Graphics g)  {
            Graphics2D g2=(Graphics2D) g;
            g2.setPaint(Color.RED);
            Ellipse2D.Double circle2=new Ellipse2D.Double(getWidth()/2-
                    getHeight()/4,
                    getHeight()/4, getHeight()/2, getHeight()/2);
            g2.draw(circle2);

            if (count!=0)  {
                for (Ellipse2D.Double circle: circles)  {
                        g2.draw(circle);
                }
            }
    }
}

public class Frame extends JFrame  {
     private Panel panel;
     public Frame()  {
        panel=new Panel();
        add(panel);
     }

     public static void main(String[] args)  {
          Frame frame=new Frame();
     }
}

What is wrong and what should I do to fix it?

parsecer
  • 4,758
  • 13
  • 71
  • 140
  • 1
    *"A new circle doesn't appear"* - Call `repaint` to trigger a new paint cycle – MadProgrammer Apr 30 '17 at 23:11
  • @MadProgrammer Where should I add it? I tried inside the `for` loop and the new circle appears only after the for loop is done (after the 10th press). Also, could you explain why the window freezes? It was giving me a `too much output to process` error after resizing.. – parsecer Apr 30 '17 at 23:15
  • 1
    *"Where should I add it?"* - After you've made any change which would need the UI to be updated - like when you create a new circle – MadProgrammer Apr 30 '17 at 23:16
  • 1
    You're getting NPE in the `paintStuff` method because of the `for (Ellipse2D.Double circle : circles) {` - most of the elements are `null`, you need to either use something like `for (int index = 0; index < count; index++) {...}` or check each element and make sure it's not `null` before updating – MadProgrammer Apr 30 '17 at 23:17
  • @MadProgrammer, Oh, I see. I thought it wouldn't go beyond the existing elements, including the uninitialized. Thank you. – parsecer Apr 30 '17 at 23:19

0 Answers0