Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaFX Drawing Shapes via Mouse #1

Open
anm100 opened this issue Mar 12, 2017 · 0 comments
Open

JavaFX Drawing Shapes via Mouse #1

anm100 opened this issue Mar 12, 2017 · 0 comments

Comments

@anm100
Copy link
Owner

anm100 commented Mar 12, 2017

http://stackoverflow.com/questions/35963341/javafx-drawing-shapes-via-mouse

Is there any easy way to draw a some shape by moving mouse?
To be more specific, I'm doing a screenshot program, so I want to select display the area somehow. I see it like holding mouse key on point A and moving it to point B in realtime, drawing the rectangle between point A and current point.

Answer:
I am using a Scene object called scene in this case. This or something like it should probably do what you want it, perhaps you have to switch between dragBox.setTranslate and dragBox.setWidth / .setHeight in the cases where you're dragging up or left instead of down and right.

Rectangle dragBox = new Rectangle(0, 0, 0, 0);
dragBox.setVisible(false);
scene.addEventFilter(MouseEvent.ANY, new EventHandler() {
@OverRide
public void handle(MouseEvent mouseEvent) {
if(mouseEvent.getEventType() == MouseEvent.MOUSE_CLICKED){
dragBox.setVisible(true);
dragBox.setTranslateX(mouseEvent.getX());
dragBox.setTranslateY(mouseEvent.getY());
}
if(mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED && dragBox.isVisible()){
dragBox.setWidth(mouseEvent.getX() - dragBox.getTranslateX());
dragBox.setHeight(mouseEvent.getY() - dragBox.getTranslateY());
}
if(mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED)
dragBox.setVisible(false);
}
});
You also have to make sure to add the dragBox to the Pane that is being showed in the scene, or else the dragBox won't be visible at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant