我把一个aintComponent()方法放在JPanel. 此方法使用Graphics.drawLine().
我放了JButton一个监听器用于放大这棵树。最后我使用了这个repaint()方法,我的树被重新粉刷得更大了,所以没有问题。
// Add Listeners on Zooming buttons
bPlus.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
Node.setZoom(1);
repaint();
}
});
但是,我使用 aMouseMotionListener在其 JPanel 中移动这棵树。然后当我打电话时repaint(),它不会删除以前打印的树,所以我最终得到了重复的树。
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if(mouseHolded) {
x += e.getX() - mouseX;
mouseX = e.getX();
y += e.getY() - mouseY;
mouseY = e.getY();
repaint(); // <===
}
}
}
);
我究竟做错了什么 ?当我使用repaint()缩放时,以前的树会被删除,为什么当我移动树时它不会这样做?