I have a JFrame that has randomly circles in it(They are generated randomly). I want to remove the selected circles by mouse click from Jframe and increase the score according to that removing.. Any idea?
"I have a timer that can able to create a new circle on the screen. Also i have used List to store created circles. (Referring Public Circle class) I have a shrinking feature as well for shrinking circles in time by reducing radius."
private static final int WIDTH = 700;
private static final int HEIGHT = 700;
public int x, y;
private static final int D_W = 500;
private static final int D_H = 500;
private List<Circle> circles;
Random random = new Random();
public int randRadius;
public int delay = 50;
public static int Life=10;
public static int Score=0;
private List<Circle> circles;
public static int Score=0;
public GameModel() {
setSize(WIDTH,HEIGHT); //setting the Frame width and height
circles = new ArrayList<>();
Timer timer = new Timer(250, new ActionListener() {
//timer for creating a new ball on JFrame
@Override
public void actionPerformed(ActionEvent e) {
int randX = random.nextInt(D_W); //or whatever the width of your panel is
int randY = random.nextInt(D_H); //or whatever the height of your panel is
randRadius = random.nextInt(101) + 50; //radius
Color color = Color.BLUE;
Circle circle = new Circle(randRadius, color, randX, randY);
circles.add(circle);
update(); //it is simply repaint();
}
});
timer.start();
}
@Override
protected void paintComponent(Graphics g) { //draw the circle randomly
super.paintComponent(g);
for (Circle circle : circles) {
circle.drawCircle(g);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(D_W, D_H);
}
public class Circle { //class for shrinking the balls in time
public int radiuss, x, y;
Color color;
public Circle(int radius, Color color, int x, int y) {
this.radiuss = radius;
this.color = color;
this.x = x;
this.y = y;
ActionListener counter = new ActionListener(){
public void actionPerformed(ActionEvent evt){
update();
radiuss--;
}};
new Timer (delay, counter).start();
}
public void drawCircle(Graphics g) {
g.setColor(color);
g.fillOval(x, y, radiuss * 2, radiuss * 2);
}
}
private class ClickCircle extends MouseAdapter {
public void mousePressed(MouseEvent e) {
selected = null;
...
if (selected != null) {
Score++;
System.out.println("Score" + Score);
}
}
}