Drawing Path JavaFX


Question

I have a Path in JavaFX that has a set of MoveTo. I am adding the Path to a Pane. Since a Path is a Node, it should draw itself automatically, right? The Pane says it has the Path as a child, but it is not drawing itself. What am I missing here?

1
1
5/1/2014 10:07:38 PM

Accepted Answer

The Path consists of the collection of PathElements. MoveTo is one of them but it moves the starting point of current path, you can make an analogy with mouse cursor like on drawing applications. First you move the mouse cursor to desired place and start drawing by mouse clicking or dragging. To start drawing with Path, use other path elements, for instance LineTo.

Path path = new Path();

// First move to starting point
MoveTo moveTo = new MoveTo();
moveTo.setX(100.0f);
moveTo.setY(50.0f);

// Then start drawing a line
LineTo lineTo = new LineTo();
lineTo.setX(75.0f);
lineTo.setY(255.0f);

path.getElements().add(moveTo);
path.getElements().add(lineTo);

For some interesting demo see Draw a semi ring - JavaFX.

5
5/23/2017 12:24:08 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon