如何从圈子列表中选择圈子以从JFrame中删除?

时间:2018-12-08 14:16:16

标签: java swing jframe graphics2d mouselistener

我有一个JFrame,里面有随机的圆圈(它们是随机生成的)。我想通过鼠标单击从Jframe中删除选定的圈子,并根据删除结果增加分数。。有什么想法吗?

“我有一个计时器,可以在屏幕上创建一个新的圆圈。 我也使用列表来存储创建的圈子。 (指公共圈子课程) 我还具有缩小功能,可以通过减小半径来及时缩小圆。”

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 个答案:

答案 0 :(得分:2)

不要创建自己的Circle类。相反,您可以使用Ellipse2D.Double(...)类。

Ellipse2D类实现Shape接口。 Shape接口实现contains(...)方法。因此,您可以遍历列表中的所有对象,并检查Shape是否包含鼠标指针。

因此,我将把“ Circle”类更改为“ ShapeInfo”类。此类将包含两个属性:

  1. 形状
  2. 颜色

因此您的基本代码将是:

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

将来,您甚至可以将Rectangle形状或任何其他想要的形状添加到列表中。

有关此概念的更多常规信息,请参见Playing With Shapes

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

然后只需调用repaint()。这是调用以确保正确重涂组件的正确方法。