0

A while ago I wrote an application in Java using Swing for the GUI development. As I want to learn more about JavaFX I'm trying to convert it but I get stuck on one piece of my application I can't seem to find the right equivalent for in JavaFX. In Swing I was able to make a JPanel, create an image, and then rescale the image based on the size of the JPanel using the component events on the panel. Then I'd draw stuff on the image and draw that on the graphics of the JPanel. Also I'd use mouse events on the JPanel to implement zooming and panning on the underlying image.

I'm not sure whether this was good practise in the first place, but it enabled me to draw anything, from pixels to lines to circles to images, on the JPanel, essentially creating sort of a rendered viewport for my application.

What would be the best way to go about this in JavaFX? I've looked at tons of tutorials and youtube videos, but they all seem to focus just on normal GUI controls like buttons, and when I look at game dev for example they make a lot of use of standard JavaFX Nodes to quickly generate moving entities in their scene. I just need sort of a canvas to render stuff on as I please, but if I add something like a default Canvas or Region in the Scene Builder for my FXML file I don't know how to start drawing on that. I'm completely lost in the jungle of tutorials and documentation on the internet.

Does anyone know of any good resource for this kind of application cause I just don't know where to start.

Erik Stens
  • 1,779
  • 6
  • 25
  • 40
  • Basically instead of drawing on a JPanel, you want to draw on a Canvas object. Add the canvas object onto a Pane. Then, you get the GraphicsContext from the canvas object, then draw on there. – Steven Nov 12 '17 at 23:56
  • Some related examples are seen [here](https://stackoverflow.com/q/31909941/230513) and [here](https://stackoverflow.com/q/37931670/230513). – trashgod Nov 13 '17 at 00:00
  • Or you can create a `Pane` and add circles, lines, images, etc to it. – James_D Nov 13 '17 at 00:21

0 Answers0