I'm developing a JavaFX application. I have a canvas and I want to know how can I draw an rectangle or other geometric shape with mouse, like in Windows paint app. Does anybody knows?
Asked
Active
Viewed 4,409 times
3
-
2You can easily do this with MOUSE_PRESSED and MOUSE_RELEASED and then following the x and y coordinates and their changes to create your geometric shapes: http://docs.oracle.com/javafx/2/api/javafx/scene/input/MouseEvent.html This link shows you how to draw rects, for example: http://docs.oracle.com/javafx/2/api/javafx/scene/shape/Rectangle.html This is a tutorial on how to use events in JavaFX: http://docs.oracle.com/javafx/2/events/jfxpub-events.htm – Nico Apr 13 '13 at 16:15
-
And if I want to draw a polygon? I'm using a canvas. I need to use canvas.getGraphicsContext2D().fillPolygon(...);. I want all to be in real time. For example if I draw an circle, when I click the circle must grow while I drag the mouse. – John Smith Apr 13 '13 at 16:30
-
1http://docs.oracle.com/javafx/2/api/javafx/scene/shape/Polygon.html Shows you how to draw Polygons. As for your mouse click and growing in real time. I get it. Hence I gave you the documentation specifically for MOUSE_PRESSED and MOUSE_RELEASED. While the mouse is pressed, you keep feeding the x and y coordinates to your shape and when the mouse is released you let go. I am not aware of convas.getGraphicsContext etc. So you will have to sift through the docs. – Nico Apr 13 '13 at 16:33