現在、長方形が適切に消去されない理由に問題があります。何らかの理由で、ドラッグを開始すると、長方形のチャンクが既に欠落しています。
@Override
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
// initialize lastX, lastY
lastX = startX;
lastY = startY;
}
@Override
public void mouseDragged(MouseEvent e) {
// Implement rubber-band cursor
Graphics g = drawingPanel.getGraphics();
//g.setColor(Color.black);
g.setXORMode(drawingPanel.getBackground());
// REDRAW the line that was drawn
// most recently during this drag
// XOR mode means that yellow pixels turn black
// essentially erasing the existing line
if(drawMode == Mode.LINE) {
g.drawLine(startX, startY, lastX, lastY);
g.drawLine(startX, startY, e.getX(), e.getY());
}
// draw line to current mouse position
// XOR mode: yellow pixels become black
// black pixels, like those from existing lines, temporarily become
// yellow
else if(drawMode == Mode.RECTANGLE) {
g.drawRect(startX, startY, lastX, lastY);
g.drawRect(startX, startY, e.getX(), e.getY());
}
lastX = e.getX();
lastY = e.getY();
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
if(drawMode == Mode.LINE) {
allShapes.add(new Line(startX, startY, arg0.getX(), arg0.getY()));
}else if(drawMode == Mode.RECTANGLE){
allShapes.add(new Rectangle(startX, startY, arg0.getX(), arg0.getY()));
}
}
startX と startY の座標に関係しているように感じますが、まだ問題がわかりません。