Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

51
LINES

< > BotCompany Repo | #1034389 // ImageSurfaceScribbleTool

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (16961L/106K).

1  
sclass ImageSurfaceScribbleTool extends ImageSurfaceMouseHandler {
2  
  settable BasicStroke stroke = new BasicStroke(5);
3  
  settable Color color = Color.black;
4  
  settable bool enabled;
5  
  Pt lastPt;
6  
  event painted;
7  
  event scribbleDone(Scribble scribble);
8  
  
9  
  Scribble scribble;
10  
11  
  *() {}
12  
  *(ImageSurface is) { this.register(is); }
13  
14  
  public void mousePressed(MouseEvent evt) {
15  
    if (evt.getButton() != MouseEvent.BUTTON1) ret;
16  
    if (!enabled) ret;
17  
    lastPt = getPt(evt);
18  
    scribble = new Scribble(color, stroke.getLineWidth(), new L);
19  
    drawPoint(lastPt);
20  
  }
21  
22  
  public void mouseReleased(MouseEvent e) {
23  
    mouseDragged(e);
24  
    lastPt = null;
25  
    scribbleDone(scribble);
26  
    scribble = null;
27  
  }
28  
  
29  
  public void mouseDragged(MouseEvent e) {
30  
    if (lastPt == null) ret;
31  
    Pt p = getPt(e);
32  
    if (!eq(p, lastPt))
33  
      drawLine(lastPt, p);
34  
    lastPt = p;
35  
    is.repaint(); // That's gonna be slow...
36  
  }
37  
  
38  
  void drawPoint(Pt p) {
39  
    drawLine(p, p);
40  
  }
41  
  
42  
  void drawLine(Pt a, Pt b) {
43  
    scribble?.addPoint(b);
44  
    Graphics2D g = getImageSurface().getImage().createGraphics();
45  
    g.setStroke(stroke);
46  
    g.setColor(color);
47  
    g.drawLine(a.x, a.y, b.x, b.y);
48  
    g.dispose();
49  
    painted();
50  
  }
51  
}

Author comment

Began life as a copy of #1005517

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034389
Snippet name: ImageSurfaceScribbleTool
Eternal ID of this version: #1034389/16
Text MD5: d3b52a9fe34458bad66eb578caa09533
Transpilation MD5: d8088786d26cb54f3fc7f07c3fdcbf45
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-15 22:28:45
Source code size: 1269 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 191 / 340
Version history: 15 change(s)
Referenced in: [show references]