你好我有一個應用程序,我在繪製東西時點擊鼠標左鍵,當觸發右鍵時它需要進入ArrayList並創建一個新的對象。當我再次開始點擊鼠標時,它必須繼續保持新的對象不斷顯示另一個對象..我的問題是,舊的消失。任何人都可以看看我忘了什麼?把對象放在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);
}
我看到'addPoint()'的問題......你能發佈這個方法的代碼嗎? – Vikram