简体   繁体   English

绘制形状时在JPanel中为我的画布清除按钮

[英]Clear button for my canvas in JPanel when drawing shapes

I am drawing shapes and the shapes store into an arraylist. 我正在绘制形状并将形状存储到arraylist中。 When I hit clear, all the shapes clear out of the arraylist except for the last one I have drawn. 当我点击清除时,除我绘制的最后一个形状外,所有形状均从数组列表中清除。

I even did 我什至做到了

Arraylist list = new Arraylist();
list.clear();
JOptionPane.showMessageDialog(null, "Size is: " + list.size());

just to prove the size was 0 and a similar test to make sure the last added object was added into the array list. 只是为了证明大小为0,并进行类似的测试以确保最后添加的对象已添加到数组列表中。 What could be the possible reasons for this? 这可能是什么原因?

call the repaint() method in the event handler of the clear button. 在清除按钮的事件处理程序中调用repaint()方法。

list.clear();
repaint();

it will refresh your canvas. 它将刷新您的画布。

Arraylist list = new Arraylist();
list.clear();

Well, that doesn't do anything because all did was create a local variable which is empty to begin with and then clear the empty List. 嗯,这没有任何作用,因为所有操作都是创建一个局部变量,该变量以空开头,然后清除空的List。 Your painting code will still be referencing the instance variable from your class. 您的绘画代码仍将引用类中的实例变量。 Your code should just be: 您的代码应为:

//Arraylist list = new Arraylist();
list.clear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM