graphics - Java custom Path2D -
i have created custom path2d class draw h-shaped "calliper" on screen, project doing. want drag , resize calliper on screen. have managed path2d set can draw calliper, , code looks this:
declaration , constructor:
public class calliper extends path2d.double { // x , y coordinates of 6 points on calliper double cx1, cx2, cx3, cx4, cx5, cx6; double cy1, cy2, cy3, cy4, cy5, cy6; // width , height double cwidth; double cheight; public calliper(double x, double y, double w, double h) { cwidth = w; cheight = h; cx1 = x; cy1 = y; cx2 = x; cy2 = y + (h/2); cx3 = x; cy3 = y + h; cx4 = x + w; cy4 = y; cx5 = cx4; cy5 = cy4 + (h /2); cx6 = cx4; cy6 = cy4 + h; build(); }
build() method (used draw path) , setcalliper() method, used redefine coordinates, or width, height:
private void build() { // draw path calliper moveto(cx1, cy1); lineto(cx2, cy2); lineto(cx3, cy3); moveto(cx2, cy2); lineto(cx5, cy5); moveto(cx4, cy4); lineto(cx6, cy6); } public void setcalliper(double x, double y, double w, double h) { // rebuild calliper using different x,y coordinates, or // different width/height cwidth = w; cheight = h; cx1 = x; cy1 = y; cx2 = x; cy2 = y + (h/2); cx3 = x; cy3 = y + h; cx4 = x + w; cy4 = y; cx5 = cx4; cy5 = cy4 + (h /2); cx6 = cx4; cy6 = cy4 + h; build(); }
i have created class draw calliper on screen, do, if try drag calliper around screen, doesn't erase original shape drag, long trail of shapes left behind. thought had omitted super.paintcomponent(g)
paintcomponent(graphics g)
method, in there code still not work.
my drag method looks this:
@override public void mousedragged(mouseevent ev) { double mx = ev.getx(); double = ev.gety(); if (dragging) { calx = mx - offsetx; caly = - offsety; cal = setcalliper(calx, caly, calw, calh); repaint(); } }
if change line cal = setcalliper(calx, caly, calw, calh);
above read cal = new calliper(calx, caly, calw, calh);
works, have been told shouldn't way.
any ideas why doesn't work expected?
the setcalliper()
directly calls build method, method appends new points previous points added path2d
- each time mousedragged
called more points added path. try calling reset()
before calling build()
(or call reset in build method before moveto/lineto calls).
Comments
Post a Comment