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?