我目前对为什么我的矩形没有正确擦除有疑问。出于某种原因,当我开始拖动时,我的矩形已经丢失了一大块:
@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 坐标有关,但我仍然无法找出问题所在。