sclass Base { S text; } sclass CirclesAndLines extends Base { new L circles; new L lines; Circle addCircle(S imageID, double x, double y) { ret addCircle(imageID, x, y, ""); } Circle addCircle(S imageID, double x, double y, S text) { ret addAndReturn(circles, nu(Circle, +x, +y, +text, img := loadImage2(imageID))); } Line addLine(Circle a, Circle b) { Line line = findWhere(Line, +a, +b); if (line == null) lines.add(line = nu(Line, +a, +b)); ret line; } Line addArrow(Circle a, Circle b) { ret addArrow(a, b, ""); } Line addArrow(Circle a, Circle b, S text) { Line line = findWhere(Line, +a, +b); if (line == null) ret addAndReturn(lines, nu(Arrow, +a, +b, +text)); line.text += " / " + text; ret line; } BufferedImage makeImage(int w, int h) { BufferedImage bg = renderTiledBackground(#1007195, w, h); for (Line l : lines) { Pt a = l.a.pt(w, h), b = l.b.pt(w, h); if (l << Arrow) drawThoughtArrow(bg, l.a.img, a.x, a.y, l.b.img, b.x, b.y, l.color); else drawThoughtLine(bg, l.a.img, a.x, a.y, l.b.img, b.x, b.y, l.color); if (nempty(l.text)) drawThoughtLineText(bg, l.a.img, a.x, a.y, l.b.img, b.x, b.y, l.text, l.color); } for (Circle c : circles) { Pt p = c.pt(w, h); drawThoughtCircle(bg, c.img, p.x, p.y); if (nempty(c.text)) drawThoughtCircleText(bg, c.img, p, c.text); } ret bg; } Canvas showAsFrame() { fO makeImg = func(int w, int h) { makeImage(w, h) }; final ImageSurface canvas = jcanvas(makeImg); disableImageSurfaceSelector(canvas); new CircleDragger(canvas, r { updateCanvas(canvas, makeImg) }, circles); showFrame(canvas); ret canvas; } } sclass Circle extends Base { BufferedImage img; double x, y; Pt pt(ImageSurface is) { ret pt(is.getWidth(), is.getHeight()); } Pt pt(int w, int h) { ret new Pt(iround(x*w), iround(y*h)); } bool contains(ImageSurface is, Pt p) { ret pointDistance(p, pt(is)) <= thoughtCircleSize(img)/2+1; } } sclass Line { Circle a, b; Color color = Color.white; Line setColor(Color color) { this.color = color; this; } } Line > Arrow {} sclass CircleDragger extends MouseAdapter { ImageSurface is; L circles; O update; int dx, dy; Circle circle; *(ImageSurface *is, O *update, L *circles) { if (containsInstance(is.tools, CircleDragger)) ret; is.tools.add(this); is.addMouseListener(this); is.addMouseMotionListener(this); } public void mousePressed(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) { Pt p = is.pointFromEvent(e); circle = findCircle(p); if (circle != null) { dx = p.x-iround(circle.x*is.getWidth()); dy = p.y-iround(circle.y*is.getHeight()); } } } public void mouseDragged(MouseEvent e) { if (circle != null) { Pt p = is.pointFromEvent(e); circle.x = (p.x-dx)/(double) is.getWidth(); circle.y = (p.y-dy)/(double) is.getHeight(); callF(update); } } public void mouseReleased(MouseEvent e) { mouseDragged(e); circle = null; } Circle findCircle(Pt p) { new Lowest best; for (Circle c : circles) if (c.contains(is, p)) best.put(c, pointDistance(p, c.pt(is))); ret best.get(); } Circle findCircle(S text) { for (Circle c : circles) if (eqic(c.text, text)) ret c; null; } }