sclass ImageSurfaceScribbleTool extends ImageSurfaceMouseHandler { settable BasicStroke stroke = new BasicStroke(5); settable Color color = Color.black; settable bool enabled; Pt lastPt; event painted; event scribbleDone(Scribble scribble); Scribble scribble; *() {} *(ImageSurface is) { this.register(is); } public void mousePressed(MouseEvent evt) { if (evt.getButton() != MouseEvent.BUTTON1) ret; if (!enabled) ret; lastPt = getPt(evt); scribble = new Scribble(color, stroke.getLineWidth(), new L); drawPoint(lastPt); } public void mouseReleased(MouseEvent e) { mouseDragged(e); lastPt = null; scribbleDone(scribble); scribble = null; } public void mouseDragged(MouseEvent e) { if (lastPt == null) ret; Pt p = getPt(e); if (!eq(p, lastPt)) drawLine(lastPt, p); lastPt = p; is.repaint(); // That's gonna be slow... } void drawPoint(Pt p) { drawLine(p, p); } void drawLine(Pt a, Pt b) { scribble?.addPoint(b); Graphics2D g = getImageSurface().getImage().createGraphics(); g.setStroke(stroke); g.setColor(color); g.drawLine(a.x, a.y, b.x, b.y); g.dispose(); painted(); } }