无法从Java中的ArrayList中删除绘制的对象

时间:2019-04-12 00:42:10

标签: java arrays user-interface arraylist draw

在“撤消和清除”操作中,我试图删除arrayList中的最后一项(撤消),并删除arraysLists中的所有项目(清除)

我有2个数组列表,矩形和圆形。

当我运行程序时,我可以绘制对象没有问题,但是当我单击“撤消”或“清除”时,什么也没发生(注释掉的清除是我要运行的代码,将背景颜色更改器放在那里作为懒惰的出路)< / p>

不确定我做错了什么,并且希望获得一些见识

这是一个家庭作业项目!

粘贴了相关代码-Circle和Rectangle类仅具有构造函数和draw函数

GraphicsContext gc, transgc;

// Array list of shapes 


ArrayList<Circle> circleList = new ArrayList<Circle>();

ArrayList<Rectangle> rectangleList = new ArrayList<Rectangle>();


// Boolean value if you are drawing a circle
private void drawCircles (ActionEvent e) {
    isRectangle = false; 
}

// Boolean value if you are drawing a Rectangle
private void drawRectangles (ActionEvent e) {
    isRectangle = true; 
}



// Clear
private void clear (ActionEvent e){

  circleList.removeAll(circleList);        
  rectangleList.removeAll(rectangleList);

}

private void undo (ActionEvent e){
    if (isRectangle == false && circleList.size() > 0)
        circleList.remove(circleList.size() - 1);
    else if (isRectangle == true && rectangleList.size()>0)
        rectangleList.remove(rectangleList.size() - 1);
}

// Draws a shape when mouse is pressed 
private void pressHandler(MouseEvent me) {
    int s1;
    int s2;

    if (me.getButton().equals(MouseButton.PRIMARY)) {
        if (isRectangle == false){
        circleList.add(new Circle(me.getX(), me.getY(), s1, s2, color));
        for (Circle g : circleList) {
            g.drawCircle(gc);
        }
    }

        else{
            rectangleList.add(new Rectangle(me.getX(), me.getY(), s1, 
            s2,color));
            for (Rectangle g : rectangleList) {
                g.drawRectangle(gc);
        }
        }
}



private void dragHandler(MouseEvent me) {

    if (isRectangle == false){
        circleList.add(new Circle(me.getX(), me.getY(), s1, s2,color));
        for (Circle g : circleList) {
            g.drawCircle(gc);
        }
    }

    else{
        rectangleList.add(new Rectangle(me.getX(), me.getY(), s1, 
        s2,color));
            for (Rectangle g : rectangleList) {
                g.drawRectangle(gc);
            }
    }
}

可以绘制形状,但是在绘制之后不能将其从画布中删除

0 个答案:

没有答案