在Java中从ArrayList中删除绘制对象时遇到问题
在撤消和清除操作中,我试图删除arrayList中的最后一项(撤消),并删除arrayList中的所有项(清除) 我有两个数组列表,矩形和圆形 当我运行程序时,我可以绘制对象,没有问题,但是当我单击“撤消”或“清除”时,什么也没有发生(注释掉的“清除”是我想要运行的代码,在那里放置一个背景颜色更改器作为延迟方式) 我真的不确定我做错了什么,希望有一些见解 这是一个家庭作业项目 粘贴的相关代码-Circle和Rectangle类只有构造函数和draw函数在Java中从ArrayList中删除绘制对象时遇到问题,java,arrays,user-interface,arraylist,draw,Java,Arrays,User Interface,Arraylist,Draw,在撤消和清除操作中,我试图删除arrayList中的最后一项(撤消),并删除arrayList中的所有项(清除) 我有两个数组列表,矩形和圆形 当我运行程序时,我可以绘制对象,没有问题,但是当我单击“撤消”或“清除”时,什么也没有发生(注释掉的“清除”是我想要运行的代码,在那里放置一个背景颜色更改器作为延迟方式) 我真的不确定我做错了什么,希望有一些见解 这是一个家庭作业项目 粘贴的相关代码-Circle和Rectangle类只有构造函数和draw函数 GraphicsContext gc, t
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);
}
}
}
GraphicsContext-gc,transgc;
//形状数组列表
ArrayList circleList=新的ArrayList();
ArrayList矩形列表=新建ArrayList();
//如果正在绘制圆,则为布尔值
私人活动(ActionEvent e){
isRectangle=false;
}
//如果正在绘制矩形,则为布尔值
私有void Draw矩形(ActionEvent e){
isRectangle=真;
}
//清楚的
私人无效清除(ActionEvent e){
圆环列表。移除所有(圆环列表);
removeAll(矩形列表);
}
私有无效撤消(ActionEvent e){
如果(isRectangle==false&&circleList.size()>0)
circleList.remove(circleList.size()-1);
else if(isRectangle==true&&rectangleList.size()>0)
rectangleList.remove(rectangleList.size()-1);
}
//按下鼠标时绘制形状
私有void pressHandler(MouseEvent me){
int s1;
int s2;
if(me.getButton().equals(MouseButton.PRIMARY)){
如果(isRectangle==false){
添加(新的圆(me.getX(),me.getY(),s1,s2,color));
用于(圆圈g:圆圈列表){
g、 绘图圈(gc);
}
}
否则{
添加(新矩形(me.getX(),me.getY(),s1,
s2,颜色);
用于(矩形g:矩形列表){
g、 绘制矩形图(gc);
}
}
}
私人真空吸尘器(MouseEvent me){
如果(isRectangle==false){
添加(新的圆(me.getX(),me.getY(),s1,s2,color));
用于(圆圈g:圆圈列表){
g、 绘图圈(gc);
}
}
否则{
添加(新矩形(me.getX(),me.getY(),s1,
s2,颜色);
用于(矩形g:矩形列表){
g、 绘制矩形图(gc);
}
}
}
可以绘制形状,但绘制后无法将其从画布中移除