JavaFX

JavaFX 2D Shape Example

Photo of Andreas PomarolliAndreas PomarolliOctober 21st, 2016Last Updated: April 24th, 2019
0 1,594 15 minutes read

This is a JavaFX 2DShape Example. Any shape that can be drawn in a two-dimensional plane is called a 2D shape. JavaFX offers variety nodes to draw different types of shapes (lines, circles, rectangles, etc.). You can add shapes to a Scene Graph. All shape classes are in thejavafx.scene.shape package.

AShape has a size and a position, which are defined by their properties. For example, the width and height properties define the size of aRectangle. Theradius property defines the size of aCircle. Thex andy properties define the position of the upper-left corner of aRectangle, thecenterX andcenterY properties define the center of a circle, etc.

Shapes are not resized by their parents during layout. The size of a shape changes only when its size-related properties are changed. Shapes have an interior and a stroke. The properties for defining the interior and stroke of a shape are declared in theShape class.

 
Thefill property specifies the color to fill the interior of the shape. The default fill isColor.BLACK. Thestroke property specifies the color for the outline stroke, which is null by default, except forLine,Polyline, andPath, which haveColor.BLACK as the default stroke.

ThestrokeWidth property specifies thewidth of the outline, which is 1.0px by default.

TheShape class contains asmooth property, which is true by default. Its true value indicates that an antialiasing hint should be used to render the shape. If it is set to false, the antialiasing hint will not be used, which may result in the edges of shapes being not crisp.

The following table shows an overview of the whole article:

The following examples use Java SE 7 and JavaFX 2.2.

1. Drawing Lines

1.1 The Code

Fx2DShapeExample1.java

import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.HBox;import javafx.scene.paint.Color;import javafx.scene.shape.Line;import javafx.stage.Stage;public class Fx2DShapeExample1 extends Application{public static void main(String[] args){Application.launch(args);}@Overridepublic void start(Stage stage){// Create the LinesLine line1 = new Line();Line line2 = new Line(0, 0, 50, 0);line2.setStrokeWidth(1.0);Line line3 = new Line(0, 50, 50, 0);line3.setStrokeWidth(2.0);line3.setStroke(Color.RED);Line line4 = new Line(0, 0, 50, 50);line4.setStrokeWidth(5.0);line4.setStroke(Color.BLUE);// Create the HBoxHBox root = new HBox();// Add the Children to the HBoxroot.getChildren().addAll(line1, line2, line3, line4);// Set Spacing of the HBoxroot.setSpacing(10);// Set the Style of the HBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A JavaFX Line Example");// Display the Stagestage.show();}}

An instance of theLine class represents a lineNode. ALine has no interior. By default, itsfill property is set to null. Setting fill has no effects. The default stroke isColor.BLACK and the defaultstrokeWidth is 1.0. TheLine class contains four double properties.

  • startX
  • startY
  • endX
  • endY

TheLine represents a line segment between (startX, startY) and (endX, endY) points. TheLine class has a no-args constructor, which defaults all its four properties to zero resulting in a line from (0, 0) to (0, 0), which represents a point. Another constructor takes values forstartX,startY,endX, andendY. After you create aLine, you can change its location and length by changing any of the four properties.

The following code snippet creates aLine with stroke width 1.0:

Line line2 = new Line(0, 0, 50, 0);line2.setStrokeWidth(1.0);

1.2 The GUI

The firstLine will appear as a point. The following Figure shows the lines.

A JavaFX Line Example
A JavaFX Line Example

2. Drawing Rectangles

2.1 The Code

Fx2DShapeExample2.java

import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Rectangle;import javafx.stage.Stage;public class Fx2DShapeExample2 extends Application{public static void main(String[] args){Application.launch(args);}@Overridepublic void start(Stage stage){// Create the RectanglesRectangle rectangle1 = new Rectangle(100, 50, Color.LIGHTGRAY);Rectangle rectangle2 = new Rectangle(120, 20, 100, 50);rectangle2.setFill(Color.WHITE);rectangle2.setStroke(Color.BLACK);rectangle2.setArcWidth(10);rectangle2.setArcHeight(10);// Create the PanePane root = new Pane();// Add the Children to the Paneroot.getChildren().addAll(rectangle1, rectangle2);// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A JavaFX Rectangle Example");// Display the Stagestage.show();}}

An instance of theRectangle class represents a rectangleNode. The class uses six properties to define the rectangle.

  • x
  • y
  • width
  • height
  • arcWidth
  • arcHeight

Thex andy properties are the x and y coordinates of the upper-left corner of the rectangle in the local coordinate system of the node. Thewidth andheight properties are the width and height of the rectangle, respectively. Specify the same width and height to draw a square.

By default, the corners of a rectangle are sharp. ARectangle can have rounded corners by specifying thearcWidth andarcHeight properties. You can think of one of the quadrants of an ellipse positioned at the four corners to make them round. ThearcWidth andarcHeight properties are the horizontal and vertical diameters of the ellipse. By default, their values are zero, which makes a rectangle have sharp corners.

TheRectangle class contains several constructors. They take various properties as arguments. The default values forx,y,width,height,arcWidth, andarcHeight properties are zero. The constructors are:

  • Rectangle()
  • Rectangle(double width, double height)
  • Rectangle(double x, double y, double width, double height)
  • Rectangle(double width, double height, Paint fill)

The following code sbippet creates aRectangle with a width of 100px and a heigth of 50 px.

// Create the RectanglesRectangle rectangle1 = new Rectangle(100, 50, Color.LIGHTGRAY);

You will not see effects of specifying the values for thex andy properties for aRectangle when you add it to most of the layout panes as they place their children at (0, 0). APane uses these properties.

2.2 The GUI

The following image shows Rectangles inside aPane, which uses thex andy properties to position them.

A JavaFX Rectangle Example
A JavaFX Rectangle Example

3. Drawing Circles

3.1 The Code

Fx2DShapeExample3.java

import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.HBox;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.stage.Stage;public class Fx2DShapeExample3 extends Application{public static void main(String[] args){Application.launch(args);}@Overridepublic void start(Stage stage){// Create the CirclesCircle circle1 = new Circle(0, 0, 40);circle1.setFill(Color.LIGHTGRAY);Circle circle2 = new Circle(10, 10, 40, Color.YELLOW);circle2.setStroke(Color.BLACK);circle2.setStrokeWidth(2.0);// Create the HBoxHBox root = new HBox();// Add the children to the HBoxroot.getChildren().addAll(circle1, circle2);// Set Spacing of the HBoxroot.setSpacing(10);// Set Style for the HBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A JavaFX Circle Example");// Display the Stagestage.show();}}

An instance of theCircle class represents a circle node. The class uses three properties to define the circle.

  • centerX
  • centerY
  • radius

ThecenterX andcenterY properties are the x and y coordinates of the center of the circle in the local coordinate system of the node. Theradius property is the radius of the circle. The default values for these properties are zero.

TheCircle class contains several constructors.

  • Circle()
  • Circle(double radius)
  • Circle(double centerX, double centerY, double radius)
  • Circle(double centerX, double centerY, double radius, Paint fill)
  • Circle(double radius, Paint fill)

The following snippet of code creates a ligthgrayCircle:

Circle circle1 = new Circle(0, 0, 40);circle1.setFill(Color.LIGHTGRAY);

3.2 The GUI

The above program adds two circles to anHBox. The following figure shows the two circles:

A JavaFX Circle Example
A JavaFX Circle Example

4. Drawing Ellipses

4.1 The Code

Fx2DShapeExample4.java

import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.HBox;import javafx.scene.paint.Color;import javafx.scene.shape.Ellipse;import javafx.stage.Stage;public class Fx2DShapeExample4 extends Application{public static void main(String[] args){Application.launch(args);}@Overridepublic void start(Stage stage){// Create the ellipsesEllipse ellipse1 = new Ellipse(50, 30);ellipse1.setFill(Color.LIGHTGRAY);Ellipse ellipse2 = new Ellipse(60, 30);ellipse2.setFill(Color.YELLOW);ellipse2.setStroke(Color.BLACK);ellipse2.setStrokeWidth(2.0);Ellipse ellipse3 = new Ellipse(30, 30);ellipse3.setFill(Color.YELLOW);ellipse3.setStroke(Color.BLACK);ellipse3.setStrokeWidth(2.0);// Create the HBoxHBox root = new HBox();// Add the children to the HBoxroot.getChildren().addAll(ellipse1, ellipse2, ellipse3);// Set Spacing of the HBoxroot.setSpacing(10);// Set the Style of the HBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A JavaFX Ellipses Example");// Display the Stagestage.show();}}

An instance of theEllipse class represents an ellipse node. The class uses four properties to define the ellipse.

  • centerX
  • centerY
  • radiusX
  • radiusY

ThecenterX andcenterY properties are thex andy coordinates of the center of the circle in the local coordinate system of the node. TheradiusX andradiusY are the radii of the ellipse in the horizontal and vertical directions. The default values for these properties are zero. A circle is a special case of an ellipse whenradiusX andradiusY are the same.

TheEllipse class contains several constructors.

  • Ellipse()
  • Ellipse(double radiusX, double radiusY)
  • Ellipse(double centerX, double centerY, double radiusX, double radiusY)

The following code snippet create a ligthgrayEllipse:

Ellipse ellipse1 = new Ellipse(50, 30);ellipse1.setFill(Color.LIGHTGRAY);

4.2 The GUI

The above program creates three instances of theEllipse class. The third instance draws a circle as the program sets the same value for the ccode>radiusX andradiusY properties. The followng image shows the three ellipses.

A JavaFX Ellipses Example
A JavaFX Ellipses Example

5. Drawing Polygons

5.1 The Code

Fx2DShapeExample5.java

import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.HBox;import javafx.scene.paint.Color;import javafx.scene.shape.Polygon;import javafx.stage.Stage;public class Fx2DShapeExample5 extends Application{public static void main(String[] args){Application.launch(args);}@Overridepublic void start(Stage stage){// Create the TrianglePolygon triangle = new Polygon();triangle.getPoints().addAll(50.0, 0.0,0.0, 50.0,100.0, 50.0);triangle.setFill(Color.WHITE);triangle.setStroke(Color.RED);// Create the ParallelogramPolygon parallelogram = new Polygon();parallelogram.getPoints().addAll(30.0, 0.0,130.0, 0.0,100.00, 50.0,0.0, 50.0);parallelogram.setFill(Color.YELLOW);parallelogram.setStroke(Color.BLACK);// Create the HexagonPolygon hexagon = new Polygon(100.0, 0.0,120.0, 20.0,120.0,40.0,100.0, 60.0,80.0,40.0,80.0, 20.0);hexagon.setFill(Color.WHITE);hexagon.setStroke(Color.BLACK);// Create the HBoxHBox root = new HBox();// Add the Children to the HBoxroot.getChildren().addAll(triangle, parallelogram, hexagon);// Set Spacing of the HBoxroot.setSpacing(10);// Set the Styleroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A JavaFX Polygon Example");// Display the Stagestage.show();}}

An instance of thePolygon class represents a polygon node. The class does not define any public properties. It lets you draw a polygon using an array of (x, y) coordinates defining the vertices of the polygon. Using thePolygon class, you can draw any type of geometric shape that is created using connected lines (triangles, pentagons, hexagons, parallelograms, etc.).

ThePolygon class contains two constructors.

  • Polygon()
  • Polygon(double… points)

The no-args constructor creates an empty polygon. You need add the (x, y) coordinates of the vertices of the shape. ThePolygon will draw a line from the first vertex to the second vertex, from the second to the third, and so on. Finally, the shape is closed by drawing a line from the last vertex to the first vertex.

ThePolygon class stores the coordinates of the vertices in anObservableList<Double>. You can get the reference of the observable list using thegetPoints() method. Notice that it stores the coordinates in a list ofDouble, which is simply a number. It is your job to pass the numbers in pairs, so they can be used as (x, y) coordinates of vertices. If you pass an odd number of numbers, no shape is created.

The following snippet of code creates two triangles—one passes the coordinates of the vertices in the constructor and another adds them to the observable list later. Both triangles are geometrically the same.

// Create an empty triangle and add vertices laterPolygon triangle1 = new Polygon();triangle1.getPoints().addAll(50.0, 0.0,0.0, 100.0,100.0, 100.0);// Create a triangle with verticesPolygon triangle2 = new Polygon(50.0, 0.0,0.0, 100.0,100.0, 100.0);

5.2 The GUI

The above program creates a triangle, a parallelogram, and a hexagon using thePolygon class as shown in the following Figure.

A JavaFX Polygon Example
A JavaFX Polygon Example

6. Drawing Polylines

6.1 The Code

Fx2DShapeExample6.java

import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.HBox;import javafx.scene.paint.Color;import javafx.scene.shape.Polyline;import javafx.stage.Stage;public class Fx2DShapeExample6 extends Application{public static void main(String[] args){Application.launch(args);}@Overridepublic void start(Stage stage){// Create the TrianglePolyline triangle = new Polyline();triangle.getPoints().addAll(50.0, 0.0,0.0, 50.0,100.0, 50.0, 50.0, 0.0);triangle.setFill(Color.WHITE);triangle.setStroke(Color.BLUE);// Create an open ParallelogramPolyline parallelogram = new Polyline();parallelogram.getPoints().addAll(30.0, 0.0, 130.0, 0.0,100.00, 50.0,0.0, 50.0);parallelogram.setFill(Color.YELLOW);parallelogram.setStroke(Color.BLACK);// Create a HexagonPolyline hexagon = new Polyline(100.0, 0.0,120.0, 20.0,120.0, 40.0,100.0, 60.0,80.0, 40.0, 80.0, 20.0,100.0, 0.0);hexagon.setFill(Color.WHITE);hexagon.setStroke(Color.BLACK);// Create the HBoxHBox root = new HBox();// Add the children to the HBoxroot.getChildren().addAll(triangle, parallelogram, hexagon);// Set Spacing of the HBoxroot.setSpacing(10);// Set the Styleroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A JavaFX Polyline Example");// Display the Stagestage.show();}}

APolyline is similar to aPolygon, except that it does not draw a line between the last and first points. That is, a polyline is an open polygon. However, the fill color is used to fill the entire shape as if the shape was closed.

Want to master JavaFX ?
Subscribe to our newsletter and download theJavaFXProgramming Cookbookright now!
In order to get you prepared for your JavaFX development needs, we have compiled numerous recipes to help you kick-start your projects. Besides reading them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

An instance of thePolyline class represents a polyline node. The class does not define any public properties. It lets you draw a polyline using an array of (x, y) coordinates defining the vertices of the polyline. Using thePolyline class, you can draw any type of geometric shape that is created using connected lines (triangles, pentagons, hexagons, parallelograms, etc.).

ThePolyline class contains two constructors.

  • Polyline()
  • Polyline(double… points)

The no-args constructor creates an empty polyline. You need to add (x, y) coordinates of the vertices of the shape. The polygon will draw a line from the first vertex to the second vertex, from the second to the third, and so on. Unlike aPolygon, the shape is not closed automatically. If you want to close the shape, you need to add the coordinates of the first vertex as the last pair of numbers. If you want to add coordinates of vertices later, add them to theObservableList<Double> returned by thegetPoints() method of thePolyline class.

The following snippet of code creates two triangles with the same geometrical properties using different methods. Notice that the first and the last pairs of numbers are the same in order to close the triangle.

Polyline triangle = new Polyline();triangle.getPoints().addAll(50.0, 0.0,0.0, 50.0,100.0, 50.0, 50.0, 0.0);triangle.setFill(Color.WHITE);triangle.setStroke(Color.BLUE);

6.2 The GUI

The above program creates a triangle, an open parallelogram, and a hexagon using the
Polyline class as shown in the following Figure.

A JavaFX Polyline Example
A JavaFX Polyline Example

7. Drawing Arcs

7.1 The Code

Fx2DShapeExample7.java

import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.HBox;import javafx.scene.paint.Color;import javafx.scene.shape.Arc;import javafx.scene.shape.ArcType;import javafx.stage.Stage;public class Fx2DShapeExample7 extends Application{public static void main(String[] args){Application.launch(args);}@Overridepublic void start(Stage stage){// Create an open arc with a fillArc arc1 = new Arc(0, 0, 50, 100, 0, 90);arc1.setFill(Color.LIGHTGRAY);// Create an open arc with no fill and a strokeArc arc2 = new Arc(0, 0, 50, 100, 0, 90);arc2.setFill(Color.TRANSPARENT);arc2.setStroke(Color.BLACK);// Create a chord arc with no fill and a strokeArc arc3 = new Arc(0, 0, 50, 100, 0, 90);arc3.setFill(Color.TRANSPARENT);arc3.setStroke(Color.BLACK);arc3.setType(ArcType.CHORD);// Create a round arc with no fill and a strokeArc arc4 = new Arc(0, 0, 50, 100, 0, 90);arc4.setFill(Color.TRANSPARENT);arc4.setStroke(Color.BLACK);arc4.setType(ArcType.ROUND);// Create a round arc with a gray fill and a strokeArc arc5 = new Arc(0, 0, 50, 100, 0, 90);arc5.setFill(Color.GRAY);arc5.setStroke(Color.BLACK);arc5.setType(ArcType.ROUND);// Create the HBoxHBox root = new HBox();// Add the children to the HBoxroot.getChildren().addAll(arc1, arc2, arc3, arc4, arc5);// Set Spacing of the HBoxroot.setSpacing(10);// Set the Style of the HBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A JavaFX Arc Example");// Display the Stagestage.show();}}

An instance of theArc class represents a sector of an ellipse. The class uses seven properties to define the ellipse.

  • centerX
  • centerY
  • radiusX
  • radiusY
  • startAngle
  • length
  • type

The first four properties define an ellipse. The last three properties define a sector of the ellipse that is theArc node. ThestartAngle property specifies the start angle of the section in degrees measured counterclockwise from the positive x-axis. It defines the beginning of the arc. The length is an angle in degrees measured counterclockwise from the start angle to define the end of the sector. If the length property is set to 360, the Arc is a full ellipse.

The type property specifies the way theArc is closed. It is one of the constants,OPEN,CHORD, andROUND, defined in theArcType enum.

  • The ArcType.OPEN does not close the arc.
  • The ArcType.CHORD closes the arc by joining the starting and ending points by a straight line.
  • The ArcType.ROUND closes the arc by joining the starting and ending point to the center of the ellipse.

TheArc class contains two constructors:

  • Arc()
  • Arc(double centerX, double centerY, double radiusX, double radiusY, double startAngle, double length)

The following code snippet creates an ligthgrayArc:

// Create an open arc with a fillArc arc1 = new Arc(0, 0, 50, 100, 0, 90);arc1.setFill(Color.LIGHTGRAY);

7.2 The GUI

The above program shows how to createArc nodes. The resulting window is shown in the following GUI:

A JavaFX Arc Example
A JavaFX Arc Example

8. Drawing Quadratic Curves

8.1 The Code

Fx2DShapeExample8.java

import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.HBox;import javafx.scene.paint.Color;import javafx.scene.shape.QuadCurve;import javafx.stage.Stage;public class Fx2DShapeExample8 extends Application{public static void main(String[] args){Application.launch(args);}@Overridepublic void start(Stage stage){// Create the QuadCurvesQuadCurve quadcurce1 = new QuadCurve(0, 100, 20, 0, 150, 100);quadcurce1.setFill(Color.TRANSPARENT);quadcurce1.setStroke(Color.BLACK);QuadCurve quadcurce2 = new QuadCurve(0, 100, 20, 0, 150, 100);quadcurce2.setFill(Color.LIGHTGRAY);// Create the HBoxHBox root = new HBox();// Add the children to the HBoxroot.getChildren().addAll(quadcurce1, quadcurce2);// Set Spacing of the HBoxroot.setSpacing(10);// Set the Style of the HBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A JavaFX QuadCurve Example");// Display the Stagestage.show();}}

Bezier curves are used in computer graphics to draw smooth curves. An instance of theQuadCurve class represents a quadratic Bezier curve segment intersecting two specified points using a specified Bezier control point. TheQuadCurve class contains six properties to specify the three points.

  • startX
  • startY
  • controlX
  • controlY
  • endX
  • endY

TheQuadCurve class contains two constructors.

  • QuadCurve()
  • QuadCurve(double startX, double startY, double controlX, double controlY, double endX, double endY)

The following code snippet creates aQuadCurve:

// Create the QuadCurvesQuadCurve quadcurce1 = new QuadCurve(0, 100, 20, 0, 150, 100);quadcurce1.setFill(Color.TRANSPARENT);quadcurce1.setStroke(Color.BLACK);

8.2 The GUI

The following GUI shows the result of the above class:

A JavaFX QuadCurve Example
A JavaFX QuadCurve Example

9. Drawing Cubic Curves

9.1 The Code

Fx2DShapeExample9.java

import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.layout.HBox;import javafx.scene.paint.Color;import javafx.scene.shape.CubicCurve;import javafx.stage.Stage;public class Fx2DShapeExample9 extends Application{public static void main(String[] args){Application.launch(args);}@Overridepublic void start(Stage stage){// Create the CubicCurvesCubicCurve cubiccurve1 = new CubicCurve(0, 50, 20, 0, 50, 80, 50, 0);cubiccurve1.setFill(Color.TRANSPARENT);cubiccurve1.setStroke(Color.BLACK);CubicCurve cubiccurve2 = new CubicCurve(0, 50, 20, 0, 50, 80, 50, 0);cubiccurve2.setFill(Color.LIGHTGRAY);// Create the HBoxHBox root = new HBox();// Add the Children to the HBoxroot.getChildren().addAll(cubiccurve1, cubiccurve2);// Set Spacing of the HBoxroot.setSpacing(10);// Set the Style of the HBoxroot.setStyle("-fx-padding: 10;" +"-fx-border-style: solid inside;" +"-fx-border-width: 2;" +"-fx-border-insets: 5;" +"-fx-border-radius: 5;" +"-fx-border-color: blue;");// Create the SceneScene scene = new Scene(root);// Add the Scene to the Stagestage.setScene(scene);// Set the Title of the Stagestage.setTitle("A JavaFX CubicCurve Example");// Display the Stagestage.show();}}

An instance of theCubicCurve class represents a cubic Bezier curve segment intersecting two specified points using two specified Bezier control points. TheCubicCurve class contains eight properties to specify the four points.

  • startX
  • startY
  • controlX1
  • controlY1
  • controlX2
  • controlY2
  • endX
  • endY

TheCubicCurve class contains two constructors.

  • CubicCurve()
  • CubicCurve(double startX, double startY, double controlX1, double controlY1, double controlX2, double controlY2, double endX, double endY)

The following snippet of code creates a transparentCubicCurve:

// Create the CubicCurvesCubicCurve cubiccurve1 = new CubicCurve(0, 50, 20, 0, 50, 80, 50, 0);cubiccurve1.setFill(Color.TRANSPARENT);cubiccurve1.setStroke(Color.BLACK);

9.2 The GUI

The following image shows the result of the above example:

A JavaFX CubicCurve Example
A JavaFX CubicCurve Example

10. Download

This was an example ofjavafx.scene.shape

Download
You can download the full source code of this example here:JavaFx2DShapeExample.zip
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Andreas PomarolliAndreas PomarolliOctober 21st, 2016Last Updated: April 24th, 2019
0 1,594 15 minutes read
Photo of Andreas Pomarolli

Andreas Pomarolli

Andreas has graduated from Computer Science and Bioinformatics at the University of Linz. During his studies he has been involved with a large number of research projects ranging from software engineering to data engineering and at least web engineering. His scientific focus includes the areas of software engineering, data engineering, web engineering and project management. He currently works as a software engineer in the IT sector where he is mainly involved with projects based on Java, Databases and Web Technologies.

Related Articles

The JavaFX Print API

July 8th, 2016

JavaFX WebView Example

October 13th, 2016

JavaFX Canvas Example

May 18th, 2016

JavaFX CSS Tutorial

May 5th, 2016

The JavaFX Media API

August 8th, 2016
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.