Suppose you have an app that displays user graphic (some kind of image) then you want to allow the user to draw some lines on this image. I have the following questions regarding such situation:
How would you accomplish that? How would you get pixel coordinates for the image from the user drag events? How would you update the image in real time?
I will give you an example of the exact opposite [erasing the Image on JavaFX] which I suppose will be enough as a starter point for you:
Download it on gist
this Canvas tutorial by Oracle shows exactly what you want to accomplish in the "Interacting with the User" section.
It shows how you can add an EventHandler to the Canvas to handle MouseEvent such as
MouseEvent.MOUSE_DRAGGED
. The GraphicsContext is then used to get the x and y coordinates and draw on the canvas.In order to use the Canvas outside the main Application class, you'd declare the Canvas in your .fxml file as such:
Then, on your MyController class: