mouseevent - JavaFX Circle object not registering mouse events properly -


i want user able drag-move circles around pane. circles dont seem register (almost) no mouse events (as defined in end). have same exact code empty pane works fine. if change

circle1.setonmousedragged 

to

paneforcircles.setonmousedragged 

it works fine not want because need manipulate both circles. ideas ? appreciate if tell me how hide part of circle overlaps adjacent elements if center close pane border.

import javafx.application.application; import javafx.geometry.insets; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.contentdisplay; import javafx.scene.control.label; import javafx.scene.control.textfield; import javafx.scene.layout.hbox; import javafx.scene.layout.pane; import javafx.scene.layout.vbox; import javafx.scene.paint.color; import javafx.scene.shape.circle;  import javafx.stage.stage;   public class ex168 extends application { @override public void start(stage primarystage) throws exception {     circle circle1 = new circle(30);     circle circle2 = new circle(35);     circle1.setcenterx(100);     circle1.setcentery(100);     circle2.setcenterx(150);     circle2.setcentery(120);     circle1.setstroke(color.black);     circle1.setfill(null);     circle2.setstroke(color.black);     circle2.setfill(null);      vbox vboxforscene = new vbox(5);     vboxforscene.setpadding(new insets(5));     vboxforscene.setalignment(pos.top_center);      pane paneforcircles = new pane();     paneforcircles.setstyle("-fx-border-color: black");     vboxforscene.heightproperty().addlistener(ov -> paneforcircles.setprefheight(vboxforscene.heightproperty().divide(1.2).doublevalue()));     paneforcircles.setprefheight(300);     hbox hboxforfields = new hbox(5);     hboxforfields.setalignment(pos.center);     hboxforfields.setspacing(5);      // vbofforleftfields     vbox vboxforleftfields = new vbox(5);     vboxforleftfields.setalignment(pos.center_left);     label lblcircle1 = new label("enter circle 1 info");     lblcircle1.setalignment(pos.top_left);     textfield tfcircle1centerx = new textfield();     tfcircle1centerx.textproperty().bind(circle1.centerxproperty().asstring());     textfield tfcircle1centery = new textfield();     tfcircle1centery.textproperty().bind(circle1.centeryproperty().asstring());     textfield tfcircle1radius = new textfield();     tfcircle1radius.textproperty().bind(circle1.radiusproperty().asstring());     tfcircle1centerx.setprefcolumncount(5);     tfcircle1radius.setprefcolumncount(5);     tfcircle1centery.setprefcolumncount(5);     label lblcenterx = new label("center x:", tfcircle1centerx);     label lblcentery = new label("center x:", tfcircle1centery);     label lblcircle1radius= new label("radius:   ", tfcircle1radius);     lblcenterx.setcontentdisplay(contentdisplay.right);     lblcentery.setcontentdisplay(contentdisplay.right);     lblcircle1radius.setcontentdisplay(contentdisplay.right);      //vboxforrightfields     vbox vboxforrightfields = new vbox(5);     label lblcircle2 = new label("enter circle 2 info");     textfield tfcircle2centerx = new textfield();      textfield tfcircle2centery = new textfield();     textfield tfcircle2radius = new textfield();     tfcircle2centerx.setprefcolumncount(5);     tfcircle2centerx.textproperty().bind(circle2.centerxproperty().asstring());     tfcircle2radius.setprefcolumncount(5);     tfcircle2radius.textproperty().bind(circle2.radiusproperty().asstring());     tfcircle2centery.setprefcolumncount(5);     tfcircle2centery.textproperty().bind(circle2.centeryproperty().asstring());     label lblcenter2x = new label("center x:", tfcircle2centerx);     label lblcenter2y = new label("center x:", tfcircle2centery);     label lblcircle2radius= new label("radius:   ", tfcircle2radius);     lblcenter2x.setcontentdisplay(contentdisplay.right);     lblcenter2y.setcontentdisplay(contentdisplay.right);     lblcircle2radius.setcontentdisplay(contentdisplay.right);     vboxforrightfields.getchildren().addall(lblcircle2, lblcenter2x, lblcenter2y, lblcircle2radius);      vboxforleftfields.getchildren().addall(lblcircle1, lblcenterx, lblcentery, lblcircle1radius);     hboxforfields.getchildren().addall(vboxforleftfields, vboxforrightfields);     label lblresult = new label("do 2 circles intersect?");     button btredrawcircles = new button("redraw circles");     vboxforscene.getchildren().addall(lblresult, paneforcircles, hboxforfields, btredrawcircles);      circle1.setonmousedragged(e -> {         system.out.println(e.getx());         circle1.setcenterx(e.getx());         circle1.setcentery(e.gety());     });      circle2.setonmousedragged(e -> {          circle2.setcenterx(e.getx());         circle2.setcentery(e.gety());     });      paneforcircles.getchildren().addall(circle1, circle2);     scene scene = new scene(vboxforscene);     primarystage.setscene(scene);     primarystage.setminheight(400);     primarystage.setminwidth(340);     primarystage.setalwaysontop(true);     primarystage.show();     circle1.requestfocus(); } 

}

this code on other hand, supposed same thing, works perfectly

public class circledraggingsample extends application {     @override     public void start(stage primarystage) throws exception {     final double radius=10;     pane pane = new pane();     pane.setprefheight(300);     pane.setprefwidth(300);     circle circle1 = new circle(radius);     circle1.setcenterx(30);     circle1.setcentery(30);      circle circle2 = new circle(radius);     circle2.setcenterx(100);     circle2.setcentery(100);      line line = new line();      line.endxproperty().bind(circle2.centerxproperty());     line.endyproperty().bind(circle2.centeryproperty());      line.startxproperty().bind(circle1.centerxproperty());     line.startyproperty().bind(circle1.centeryproperty());      pane.getchildren().addall(circle1, circle2, line);      circle2.setonmousedragged(e -> {         circle2.setcenterx(e.getx());         circle2.setcentery(e.gety());     });      circle1.setonmousedragged(e -> {         circle1.setcenterx(e.getx());         circle1.setcentery(e.gety());     });      scene scene = new scene(pane);     primarystage.setscene(scene);     primarystage.show(); } } 

even though have posted example, i'd rather show way mine how done in general. there several ways, 1 works:

public class dragnodes extends application {      public static list<circle> circles = new arraylist<circle>();      public static void main(string[] args) {         launch(args);     }      @override     public void start(stage primarystage) {          group root = new group();          circle circle1 = new circle( 100, 100, 50);         circle1.setstroke(color.green);         circle1.setfill(color.green.derivecolor(1, 1, 1, 0.3));          circle circle2 = new circle( 200, 200, 50);         circle2.setstroke(color.blue);         circle2.setfill(color.blue.derivecolor(1, 1, 1, 0.3));          line line = new line();         line.setstrokewidth(20);          // binding         line.startxproperty().bind(circle1.centerxproperty());         line.startyproperty().bind(circle1.centeryproperty());         line.endxproperty().bind(circle2.centerxproperty());         line.endyproperty().bind(circle2.centeryproperty());          mousegestures mg = new mousegestures();         mg.makedraggable( circle1);         mg.makedraggable( circle2);         mg.makedraggable( line);          root.getchildren().addall(circle1, circle2, line);          primarystage.setscene(new scene(root, 1024, 768));         primarystage.show();     }        public static class mousegestures {          class dragcontext {             double x;             double y;         }          dragcontext dragcontext = new dragcontext();          public void makedraggable( node node) {             node.setonmousepressed( onmousepressedeventhandler);             node.setonmousedragged( onmousedraggedeventhandler);             node.setonmousereleased(onmousereleasedeventhandler);         }          eventhandler<mouseevent> onmousepressedeventhandler = new eventhandler<mouseevent>() {              @override             public void handle(mouseevent event) {                  if( event.getsource() instanceof circle) {                      circle circle = ((circle) (event.getsource()));                      dragcontext.x = circle.getcenterx() - event.getscenex();                     dragcontext.y = circle.getcentery() - event.getsceney();                  } else {                      node node = ((node) (event.getsource()));                      dragcontext.x = node.gettranslatex() - event.getscenex();                     dragcontext.y = node.gettranslatey() - event.getsceney();                  }             }         };          eventhandler<mouseevent> onmousedraggedeventhandler = new eventhandler<mouseevent>() {              @override             public void handle(mouseevent event) {                  if( event.getsource() instanceof circle) {                      circle circle = ((circle) (event.getsource()));                      circle.setcenterx( dragcontext.x + event.getscenex());                     circle.setcentery( dragcontext.y + event.getsceney());                  } else {                      node node = ((node) (event.getsource()));                      node.settranslatex( dragcontext.x + event.getscenex());                     node.settranslatey( dragcontext.y + event.getsceney());                  }              }         };          eventhandler<mouseevent> onmousereleasedeventhandler = new eventhandler<mouseevent>() {              @override             public void handle(mouseevent event) {              }         };      }  } 

it shows how drag circles , bind node (the line) gets modified when drag circles. can drag line separately node , handled differently.

in case still got problems let me know.


as general note, it's advisable add node in order understand events happen:

 node.addeventfilter(event.any, e -> system.out.println( e)); 

and check console output while on screen.


regarding main problem: mustn't set fill null. in case click event won't registered. should use color.transparent instead. can verify event difference above mentioned method.


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -