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

101
LINES

< > BotCompany Repo | #1007298 // Draggable Circles [WORKS]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 3874K of libraries. Click here for Pure Java version (4342L/30K/113K).

1  
!7
2  
3  
sclass CirclesAndLines {
4  
  new L<Circle> circles;
5  
  new L<Line> lines;
6  
7  
  Circle addCircle(S imageID, double x, double y) {
8  
    ret addAndReturn(circles, nu(Circle, +x, +y, img := loadImage2(imageID)));
9  
  }
10  
  
11  
  Line addLine(Circle a, Circle b) {
12  
    ret addAndReturn(lines, nu(Line, +a, +b));
13  
  }
14  
  
15  
  BufferedImage makeImage(int w, int h) {
16  
    BufferedImage bg = renderTiledBackground(#1007195, w, h);
17  
    for (Line l : lines)
18  
      drawThoughtLine(bg, l.a.img, iround(l.a.x*w), iround(l.a.y*h),
19  
        l.b.img, iround(l.b.x*w), iround(l.b.y*h), Color.white);
20  
    for (Circle c : circles)
21  
      drawThoughtCircle(bg, c.img, iround(c.x*w), iround(c.y*h));
22  
    ret bg;
23  
  }
24  
}
25  
26  
sclass Circle {
27  
  BufferedImage img;
28  
  double x, y;
29  
  
30  
  Pt pt(ImageSurface is) {
31  
    ret new Pt(iround(x*is.getWidth()), iround(y*is.getHeight()));
32  
  }
33  
  
34  
  bool contains(ImageSurface is, Pt p) {
35  
    ret pointDistance(p, pt(is)) <= thoughtCircleSize(img)/2+1;
36  
  }
37  
}
38  
39  
sclass Line {
40  
  Circle a, b;
41  
}
42  
43  
p-pretty /* although that's debatable given the detailed person */ {
44  
  final new CirclesAndLines cal;
45  
  Circle a = cal.addCircle(#1007291, 1/3.0, 0.5);
46  
  Circle b = cal.addCircle(#1007292, 2/3.0, 0.5);
47  
  cal.addLine(a, b);
48  
  fO makeImg = func(int w, int h) { cal.makeImage(w, h) };
49  
  final Canvas canvas = jcanvas(makeImg);
50  
  disableImageSurfaceSelector(canvas);
51  
  new CircleDragger(canvas, r { updateCanvas(canvas, makeImg) }, cal.circles);
52  
  showFrame(canvas);
53  
}
54  
55  
sclass CircleDragger extends MouseAdapter {
56  
  ImageSurface is;
57  
  L<Circle> circles;
58  
  O update;
59  
  int dx, dy;
60  
  Circle circle;
61  
62  
  *(ImageSurface *is, O *update, L<Circle> *circles) {
63  
    if (containsInstance(is.tools, CircleDragger)) ret;
64  
    is.tools.add(this);
65  
    is.addMouseListener(this);
66  
    is.addMouseMotionListener(this);
67  
  }
68  
69  
  public void mousePressed(MouseEvent e) {
70  
    if (e.getButton() == MouseEvent.BUTTON1) {
71  
      Pt p = is.pointFromEvent(e);
72  
      circle = findCircle(p);
73  
      if (circle != null) {
74  
        dx = p.x-iround(circle.x*is.getWidth());
75  
        dy = p.y-iround(circle.y*is.getHeight());
76  
      }
77  
    }
78  
  }
79  
80  
  public void mouseDragged(MouseEvent e) {
81  
    if (circle != null) {
82  
      Pt p = is.pointFromEvent(e);
83  
      circle.x = (p.x-dx)/(double) is.getWidth();
84  
      circle.y = (p.y-dy)/(double) is.getHeight();
85  
      callF(update);
86  
    }
87  
  }
88  
89  
  public void mouseReleased(MouseEvent e) {
90  
    mouseDragged(e);
91  
    circle = null;
92  
  }
93  
  
94  
  Circle findCircle(Pt p) {
95  
    new Lowest<Circle> best;
96  
    for (Circle c : circles)
97  
      if (c.contains(is, p))
98  
        best.put(c, pointDistance(p, c.pt(is)));
99  
    ret best.get();
100  
  }
101  
}

Author comment

Began life as a copy of #1007296

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, meaugxdlecrj, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1007298
Snippet name: Draggable Circles [WORKS]
Eternal ID of this version: #1007298/17
Text MD5: 6902d92c74c068b8aafbc59dbae58a95
Transpilation MD5: fac6f6529648293e7495eb8d975bacd3
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-18 17:58:42
Source code size: 2702 bytes / 101 lines
Pitched / IR pitched: No / No
Views / Downloads: 481 / 613
Version history: 16 change(s)
Referenced in: [show references]