首页 > 解决方案 > 单击它后,我想删除该圆圈。但是,每当我单击它时,什么都没有发生

问题描述

所以这是我创建工作正常的圆圈的课程。

public class CircleAnimations {

    private ArrayList<Circle> circles; // the circles to animate
    private int size; // canvas width and height (will be square)
    private Random rng; // use to make random numbers

    /** create a drawing pane of a particular size */
    public CircleAnimations(int s) {
        circles = new ArrayList<>();
        size = s;
        rng = new Random();

//don't mess with this
        StdDraw.setCanvasSize(size, size); // set up drawing canvas
        StdDraw.setXscale(0, size); // <0, 0> is bottom left. <size-1, size-1> is top right
        StdDraw.setYscale(0, size);
    }

    public void drawCircle() {
        for (int i = 0; i < circles.size(); i++) {
            circles.get(i).draw();
        }
    }

    public void addCircle() {
        circles.add(new Circle(rng.nextInt(size - 1), rng.nextInt(size - 1), rng.nextInt(75),
                new Color(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255))));
        circles.add(new Circle(rng.nextInt(size - 1), rng.nextInt(size - 1), rng.nextInt(75),
                new Color(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255))));
        circles.add(new Circle(rng.nextInt(size - 1), rng.nextInt(size - 1), rng.nextInt(75),
                new Color(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255))));
        drawCircle();
    }

    public void removeClicked() {
        addCircle();
        while (circles.size() > 0) {
            for (int i = circles.size() - 1; i > 0; i--) {
                double mouseXPos = StdDraw.mouseX();
                double mouseYPos = StdDraw.mouseY();
                if (StdDraw.isMousePressed()) {
                    if (mouseXPos < circles.get(i).getX() + circles.get(i).getRadius()
                            || mouseXPos > circles.get(i).getX() - circles.get(i).getRadius()) {
                        if (mouseYPos < circles.get(i).getY() + circles.get(i).getRadius()
                                || mouseYPos > circles.get(i).getX() - circles.get(i).getRadius()) {
                            circles.remove(i);
                            drawCircle();
                        }
                    }
                }

            }

        }

    }
}

由于某种原因,删除被点击的圆圈的方法不起作用。

标签: java

解决方案


推荐阅读