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).

!7

sclass CirclesAndLines {
  new L<Circle> circles;
  new L<Line> lines;

  Circle addCircle(S imageID, double x, double y) {
    ret addAndReturn(circles, nu(Circle, +x, +y, img := loadImage2(imageID)));
  }
  
  Line addLine(Circle a, Circle b) {
    ret addAndReturn(lines, nu(Line, +a, +b));
  }
  
  BufferedImage makeImage(int w, int h) {
    BufferedImage bg = renderTiledBackground(#1007195, w, h);
    for (Line l : lines)
      drawThoughtLine(bg, l.a.img, iround(l.a.x*w), iround(l.a.y*h),
        l.b.img, iround(l.b.x*w), iround(l.b.y*h), Color.white);
    for (Circle c : circles)
      drawThoughtCircle(bg, c.img, iround(c.x*w), iround(c.y*h));
    ret bg;
  }
}

sclass Circle {
  BufferedImage img;
  double x, y;
  
  Pt pt(ImageSurface is) {
    ret new Pt(iround(x*is.getWidth()), iround(y*is.getHeight()));
  }
  
  bool contains(ImageSurface is, Pt p) {
    ret pointDistance(p, pt(is)) <= thoughtCircleSize(img)/2+1;
  }
}

sclass Line {
  Circle a, b;
}

p-pretty /* although that's debatable given the detailed person */ {
  final new CirclesAndLines cal;
  Circle a = cal.addCircle(#1007291, 1/3.0, 0.5);
  Circle b = cal.addCircle(#1007292, 2/3.0, 0.5);
  cal.addLine(a, b);
  fO makeImg = func(int w, int h) { cal.makeImage(w, h) };
  final Canvas canvas = jcanvas(makeImg);
  disableImageSurfaceSelector(canvas);
  new CircleDragger(canvas, r { updateCanvas(canvas, makeImg) }, cal.circles);
  showFrame(canvas);
}

sclass CircleDragger extends MouseAdapter {
  ImageSurface is;
  L<Circle> circles;
  O update;
  int dx, dy;
  Circle circle;

  *(ImageSurface *is, O *update, L<Circle> *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<Circle> best;
    for (Circle c : circles)
      if (c.contains(is, p))
        best.put(c, pointDistance(p, c.pt(is)));
    ret best.get();
  }
}

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: 480 / 612
Version history: 16 change(s)
Referenced in: #1007303 - CirclesAndLines [include]
#1007304 - Draggable Circles [shortened]