java - JavaFX increasing a circle's radius by clicking on it -
i'm trying create javafx program creates circle when click on screen. there can many circles @ once. can't think of solution how increase circle's radius when click on again.
public class controller implements initializable { @fxml private button reset; @fxml private anchorpane anchor; @fxml private borderpane border; circle circle = new circle(); int radius = 20; public void initialize (url location, resourcebundle resources) { anchor.setonmouseclicked(event -> { border.getchildren().add(circle = new circle()); circle.setcenterx(event.getx()); circle.setcentery(event.gety()); circle.setradius(radius); }); reset.setonaction(event -> { border.getchildren().clear(); }); circle.setonmouseclicked(event -> { circle.setradius(radius * 1.5); }); } }
the field declare circle
never added scene graph. never appears , mouseclicked
handler never invoked.
on other hand, circles add scene graph have no mouse clicked handler associated them. need register handler when create them:
anchor.setonmouseclicked(event -> { circle circle = new circle(); border.getchildren().add(circle); circle.setcenterx(event.getx()); circle.setcentery(event.gety()); circle.setradius(radius); circle.setonmouseclicked(e -> { circle.setradius(circle.getradius() * 1.5); // prevent event propagating pane: e.consume(); }); });
and rid of circle
instance field , handler associate entirely.
Comments
Post a Comment