Okay so I am creating a program where the user can click anywhere on a JPanel and a circle will appear. I can run the program with no errors however no circles are appearing when I click on the JPanel. I believe my paint components and draw methods are a little messy.
Main class:
Bubble Class:
Main class:
public class Soap extends JFrame {
private JButton setSize = new JButton("SET RADIUS");
private JButton green = new JButton("GREEN");
private JButton red = new JButton("RED");
private JLabel info = new JLabel("New Radius: ");
private JTextField newR = new JTextField(8);
public ArrayList<MakeBubbles> bub = new ArrayList<MakeBubbles>();
private JPanel canvas = new JPanel();
Soap(){
setLayout(new BorderLayout());
JPanel main = new JPanel(new FlowLayout(FlowLayout.CENTER));
main.add(green);
main.add(red);
main.add(info);
main.add(newR);
main.add(setSize);
add(main, BorderLayout.SOUTH);
canvas.setBackground(Color.WHITE);
add(canvas, BorderLayout.CENTER);
canvas.addMouseListener(clicker);
}
protected void paintComponent(Graphics g){
super.paintComponents(g);
for (MakeBubbles mb : bub){
mb.draw(g);
}
}
MouseListener clicker = new MouseAdapter(){
public void mouseClicked(MouseEvent e){
MakeBubbles b = new MakeBubbles((int)e.getX(), (int)e.getY(), 79, Color.CYAN);
bub.add(B)/>;
repaint();
}
};
public static void main(String[] args){
Soap s1 = new Soap();
s1.setVisible(true);
s1.setSize(500,300);
s1.setResizable(true);
s1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Bubble Class:
public class MakeBubbles {
int x = 0, y = 0, radius = 30;
Color c = Color.CYAN;
public MakeBubbles(int x, int y, int radius, Color c){
this.x = x;
this.y = y;
this.radius = radius;
this.c = c;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public void setRadius(int newR){
this.radius = newR;
}
public int getRadius(){
return this.radius;
}
public Color getColor(){
return this.c;
}
public void draw(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setColor(c);
g2.fillOval(x,y,radius,radius);
}
}