graphics - Implement Undo&Redo Java Blackboard Paint -
i've got blackboard, can draw dragging mouse. i'm trying implement undo & redo delete/restore last coordinates drawn. undo & redo works, blackboard doesn't refresh automatically, , have drag mouse again.
here code (too long):
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 mouse events class mlistener implements mouselistener { // required interface, 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{ (pointerdraw p : points){ g2.drawline(p.getstartxpoint(), p.getstartypoint(), p.getstartxpoint(), p.getstartypoint()); } _flagundo = false; } } // shortcuts: ctrl-z , 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; (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(); } } }
Comments
Post a Comment