Friday, 15 May 2015

java - How to get coordinates for the center of Path in javafx? -



java - How to get coordinates for the center of Path in javafx? -

i've got path element, manually drawn , saved node. cannot figure how center coordinates entire node, if relocate path using relocate() method , later decide set was, not homecoming it's initial position. have tried using getlayoutx() , getlayouty() methods, moves node farther original position.

code utilize relocate

class patheventhandler implements eventhandler<mouseevent>{ //element number in paths public int n; public patheventhandler(int n){ this.n=n; } @override public void handle(mouseevent me) { if (me.geteventtype() == mouseevent.mouse_entered) { paths.get(n).seteffect(new dropshadow(20, color.black)); } if (me.geteventtype() == mouseevent.mouse_exited) { paths.get(n).seteffect(null); } if (i == 2) { if (me.geteventtype() == mouseevent.mouse_dragged) { listx.add(paths.get(n).getlayoutx()); listy.add(paths.get(n).getlayouty()); paths.get(n).relocate(me.getscenex(), me.getsceney()); cachepath.add(paths.get(n)); cachetype.add("relocate"); } if (me.geteventtype() == mouseevent.mouse_clicked) { if (me.getbutton() == mousebutton.secondary) { root.getchildren().remove(paths.get(n)); cachepath.add(paths.get(n)); cachetype.add("remove"); } } } } }

based on previous question, , you've barely described in one, i've tried figure out trying accomplish.

maybe it's far there, short functional code should plenty understand how deal path creation (as first question) , path motion (for actual question), within same mouse listener.

private path path; private double x1, y1; @override public void start(stage primarystage) { anchorpane root = new anchorpane(); root.addeventhandler(mouseevent.any, e -> { if(e.gettarget() instanceof path){ // select existing path path path1 = (path)e.gettarget(); if (e.geteventtype() == mouseevent.mouse_entered_target) { path1.seteffect(new dropshadow(20, color.black)); } else if (e.geteventtype() == mouseevent.mouse_exited_target) { path1.seteffect(null); } else if (e.geteventtype() == mouseevent.mouse_pressed) { x1=e.getx(); y1=e.gety(); } else if (e.geteventtype() == mouseevent.mouse_dragged) { // traslate path path1.settranslatex(e.getx()-x1+path1.gettranslatex()); path1.settranslatey(e.gety()-y1+path1.gettranslatey()); x1=e.getx(); y1=e.gety(); } else if (e.getbutton()==mousebutton.secondary) { // right-click on path move original position path1.settranslatex(0); path1.settranslatey(0); } } else { // generate new path if (e.geteventtype() == mouseevent.mouse_pressed) { path = new path(); path.setstroke(color.black); path.setstrokewidth(10); path.getelements().add(new moveto(e.getx(), e.gety())); root.getchildren().add(path); } else if (e.geteventtype() == mouseevent.mouse_dragged || e.geteventtype() == mouseevent.mouse_released) { path.getelements().add(new lineto(e.getx(), e.gety())); } } }); scene scene = new scene(root, 600, 400); primarystage.setscene(scene); primarystage.show(); }

java javafx

No comments:

Post a Comment