java - 单击它后,我想删除该圆圈。但是,每当我单击它时,什么都没有发生
问题描述
所以这是我创建工作正常的圆圈的课程。
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();
}
}
}
}
}
}
}
由于某种原因,删除被点击的圆圈的方法不起作用。
解决方案
推荐阅读
- python - 了解如何使用 Google OR 工具设置软约束
- python - Redshift UDF 中 scikit learn 包的导入失败
- r - 跨两个嵌套列表列应用数学函数
- typo3 - 将cropVariants 与 FilesProcessor 一起使用
- linux - 为什么Linux系统时间每6个月加减一小时?
- mysql - 如何在 MySQL 查询中调用模型方法?
- sql - 如何组合来自多个来源的时间线/重叠
- c - 如何实现像 memcpy() 这样的快速复制功能?
- reactjs - 如何修复我在反应组件中遇到的语法错误?
- python - 使用 Django 导出查询集数据,无需重新查询数据