我有一個使用自定義JPanel繪製的Java繪畫程序。當點擊JPanel畫一個小橢圓形(或者如果你願意的話),每當你點擊另一個地方時橢圓消失。座標也得到更新,但橢圓形不留,它移動到任何地方的用戶點擊一個... ... 這裏有一個自定義的JPanel代碼:爲什麼這個Java繪畫程序不能繪製多個橢圓形?
int xCord, yCord;
public class PaintPanel extends JPanel implements MouseListener {
// default serial whatever...
private static final long serialVersionUID = -6514297510194472060L;
// initial values
int xCord = -10;
int yCord = -10;
public PaintPanel() {
addMouseListener(this);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(ProgramUI.currentColor);
g.fillOval(xCord, yCord, 8, 8);
repaint();
}
@Override
public void mouseClicked(MouseEvent m) {
}
@Override
public void mouseEntered(MouseEvent m) {
}
@Override
public void mouseExited(MouseEvent m) {
}
@Override
public void mousePressed(MouseEvent m) {
if (paintPanel.contains(m.getPoint())) {
xCord = m.getX();
yCord = m.getY();
System.out.println("x: " + xCord + " y: " + yCord);
}
}
@Override
public void mouseReleased(MouseEvent m) {
}
}
我需要鼠標的召開,不斷作畫一個橢圓直到放開鼠標按鈕。這裏唯一的問題是鼠標橢圓更新,但不保存它的原始位置。我該如何解決?