我有一块黑板,我可以在那里画拖鼠。 我正在尝试实现撤销和放大重做以删除/恢复绘制的最后一个坐标。 撤消&重做有效,但我的黑板不会自动刷新,我不得不再次拖动鼠标。
这是我的代码(太长了):
public class ClientBlackboard extends JPanel implements KeyListener {
[...]
class mMotionListener implements MouseMotionListener {
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
points.add(new PointerDraw(x, y));
_changed = true;
try {
drawCurrentShape(_bufImage.createGraphics());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
repaint();
}
public void mouseMoved(MouseEvent e) { }
}
// subscribe to mouse events
class mListener implements MouseListener {
// Required by interface, but not used
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
if (!_changed)
System.out.println("Error");
else
System.out.println("Changes saved");
}
}
// draw
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (_bufImage == null){
int w = this.getWidth();
int h = this.getHeight();
_bufImage = (BufferedImage) this.createImage(w, h);
Graphics2D gc = _bufImage.createGraphics();
gc.setColor(COLOR_BACKGROUND);
gc.fillRect(0, 0, w, h);
}
g2.drawImage(_bufImage, null, 0, 0);
test = g2;
}
private void drawCurrentShape(Graphics2D g2) throws Exception{
g2.setColor(COLOR_FOREGROUND);
if (!_flagUndo)
g2.drawLine(x, y, x, y);
else{
for (PointerDraw p : points){
g2.drawLine(p.getStartXPoint(), p.getStartYPoint(), p.getStartXPoint(), p.getStartYPoint());
}
_flagUndo = false;
}
}
// Shortcuts: ctrl-z and ctrl-y
public void keyReleased(KeyEvent e) { }
public void keyPressed(KeyEvent e) {
if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_Z){
try {
PointerDraw point = Connection.remote.readDrawFromDB(_counter++);
System.out.println("COUNTER: " + _counter);
System.out.println("UNDO: " + "X: " + point.getStartXPoint() + " Y: " + point.getStartYPoint());
points.removeLast();
getAllList();
_bufImage = null;
_flagUndo = true;
repaint();
test.drawImage(_bufImage, null, 0, 0);
} catch (NullPointerException exc){
System.out.println("You can't undo again");
_counter--;
} catch (Exception exc) {
// TODO Auto-generated catch block
exc.printStackTrace();
}
}
if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_Y){
try {
if (_counter > 0){
PointerDraw point = Connection.remote.readDrawFromDB(_counter--);
System.out.println("COUNTER: " + _counter);
System.out.println("REDO: X: " + point.getStartXPoint() + " Y: " + point.getStartYPoint());
points.add(new PointerDraw(point.getStartXPoint(), point.getStartYPoint()));
getAllList();
_bufImage = null;
_flagUndo = true;
for (PointerDraw p : points){
test.drawLine(p.getStartXPoint(), p.getStartYPoint(), p.getStartXPoint(), p.getStartYPoint());
}
repaint();
}
} catch(NullPointerException exc){
System.out.println("You can't redo again");
_counter++;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}