您好我有一个应用程序,我在触发鼠标右键时单击鼠标左键绘制东西它需要进入 ArrayList 并创建一个新对象。当我再次开始单击鼠标左键时,它必须继续使用新对象继续显示另一个对象..我的问题是旧对象消失了。谁能看看我忘记了什么?
这是代码
Drawing draw; // class definition
private ArrayList<Drawing> tekening = new ArrayList<Drawing>();
public DrawingPanel() {
setBackground(Color.WHITE); // zorg voor een witte achtergrond.
this.addMouseListener(this); // control de mouselistener
draw = new Drawing(color);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw.draw(g);
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
Point k = new Point(e.getX(), e.getY());
draw.addPoint(k);
System.out.println("punt gezet op " + k);
}
if(e.getButton() == MouseEvent.BUTTON3) {
tekening.add(draw);
color = new Color(r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0, r.nextInt(255 - 0 + 1) + 0);
//System.out.println("new color " + color);
draw.setColor(color);
draw = new Drawing(color);
}
}
我忘记了我的paintcomponent中的foreach循环。
for(Drawing draw : tekening) {
draw.draw(g);
}