0
votes

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);
                    }
                }

            }
1
1) For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! - Andrew Thompson

1 Answers

2
votes

Don't create your own Circle class. Instead you can use the Ellipse2D.Double(...) class.

The Ellipse2D class implements the Shape interface. The Shape interface implements a contains(...) method. So you can iterate through all the Objects in your List and check if the Shape contains the mouse point.

So I would call change you "Circle" class to be a "ShapeInfo" class. This class would contain two properties:

  1. the Shape
  2. the Color

So your basic code would be:

//Circle circle = new Circle(randRadius, color, randX, randY);
//circles.add(circle);
Shape shape = new Ellipse2D.Double(...);
ShapeInfo info = new ShapeInfo(shape, color);
shapes.add( info );

In the future you could even add Rectangle shapes or any other Shape that you want to your List.

See Playing With Shapes for more general info on this concept.

update(); //it is simply repaint();

Then just invoke repaint(). That is the proper method to invoke to make sure the component is repainted properly.