JavaFX has other built-in shapes, such as
Arc
Circle
CubicCurve
Ellipse
Line
Path
Polygon
Polyline
QuadCurve
Rectangle
SVGPath
Text
The following code shows how to create a Path.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
//from w w w . j a v a 2 s. c o m
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
[Link]("Checkbox Sample");
[Link](230);
[Link](120);
Path path = new Path();
[Link]().add(new MoveTo(0.0f, 50.0f));
[Link]().add(new LineTo(100.0f, 100.0f));
VBox vbox = new VBox();
[Link]().addAll(path);
[Link](5);
HBox root = new HBox();
[Link]().add(vbox);
[Link](40);
[Link](new Insets(20, 10, 10, 20));
((Group) [Link]()).getChildren().add(root);
[Link](scene);
[Link]();
}
}
Path elements actually extend from the [Link] class,
which is used only in the context of a Path object.
So you won't be able to instantiate a LineTo class to be put in the scene graph.
The classes with To as a suffix are path elements, not Shape nodes.
For example, the MoveTo and LineTo object instances are Path elements
added to a Path object, not shapes that can be added to the scene.
The code above generates the following result.
Example
The following code shows how to add QuadCurveTo to a Path.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
//from w w w .j a va2s .co m
public class Main extends Application {
@Override
public void start(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root, 300, 150);
[Link](scene);
[Link]("");
Path path = new Path();
MoveTo moveTo = new MoveTo();
[Link](0.0f);
[Link](50.0f);
QuadCurveTo quadTo = new QuadCurveTo();
[Link](25.0f);
[Link](0.0f);
[Link](50.0f);
[Link](50.0f);
[Link]().add(moveTo);
[Link]().add(quadTo);
[Link]().add(path);
[Link](root);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
The code above generates the following result.
Example 2
Using Path, MoveTo and CubicCurveTo to create curve
/*w w w . j a va 2 s.c o m*/
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
[Link]("ComboBoxSample");
Scene scene = new Scene(new Group(), 450, 250);
Path path = new Path();
MoveTo moveTo = new MoveTo();
[Link](0.0f);
[Link](0.0f);
CubicCurveTo cubicTo = new CubicCurveTo();
cubicTo.setControlX1(0.0f);
cubicTo.setControlY1(0.0f);
cubicTo.setControlX2(100.0f);
cubicTo.setControlY2(100.0f);
[Link](100.0f);
[Link](50.0f);
[Link]().add(moveTo);
[Link]().add(cubicTo);
Group root = (Group) [Link]();
[Link]().add(path);
[Link](scene);
[Link]();
}
}
The code above generates the following result.
Example 3
Subtract two shapes to create a Path
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
public class Main extends Application {
public static void main(String[] args) {
[Link](args);//from w w w . ja v a 2 s. co m
}
@Override
public void start(Stage primaryStage) {
[Link]("Shapes");
Group root = new Group();
Scene scene = new Scene(root, 300, 300, [Link]);
Ellipse bigCircle = [Link]()
.centerX(100)
.centerY(100)
.radiusX(50)
.radiusY(75/2)
.strokeWidth(3)
.stroke([Link])
.fill([Link])
.build();
Ellipse smallCircle = [Link]()
.centerX(100)
.centerY(100)
.radiusX(35/2)
.radiusY(25/2)
.build();
Shape shape = [Link](bigCircle, smallCircle);
[Link](1);
[Link]([Link]);
[Link]([Link](255, 200, 0));
[Link]().add(shape);
[Link](scene);
[Link]();
}
}
The code above generates the following result.
Example 4
Using VLineTo to create vertical line
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/* ww w. j a va2s. co m*/
public class Main extends Application {
@Override
public void start(final Stage stage) {
[Link]("HTML");
[Link](500);
[Link](500);
Scene scene = new Scene(new Group());
VBox root = new VBox();
Path path = new Path();
[Link]().add(new MoveTo(50.0f, 0.0f));
[Link]().add(new VLineTo(50.0f));
[Link]().addAll(path);
[Link](root);
[Link](scene);
[Link]();
}
public static void main(String[] args) {
launch(args);
}
}
The code above generates the following result.