”;
Mathematically, a quadratic curve is one that is described by a quadratic function like − y = ax2 + bx + c.
In computer graphics Bezier curves are used. These are parametric curves which appear reasonably smooth at all scales. These Bezier curves are drawn based on points on an XY plane.
A quadratic curve is a Bezier parametric curve in the XY plane which is a curve of degree 2. It is drawn using three points: start point, end point and control point as shown in the following diagram
Quad Curve in JavaFX
In JavaFX, a Quad Curve is represented by a class named QuadCurve. This class belongs to the package javafx.scene.shape.
By instantiating this class, you can create a QuadCurve node in JavaFX.
This class has 6 properties of the double datatype namely −
-
startX − The x coordinate of the starting point of the curve.
-
startY − The y coordinate of the starting point of the curve.
-
controlX − The x coordinate of the control point of the curve.
-
controlY − The y coordinate of the control point of the curve.
-
endX − The x coordinate of the end point of the curve.
-
endY − The y coordinate of the end point of the curve.
To draw a Quad Curve, you need to pass values to these properties. This can be done either by passing them to the constructor of this class, in the same order, at the time of instantiation; or by using appropriate setter methods.
Steps to Draw Quad Curve
To Draw a Bezier Quadrilateral Curve in JavaFX, follow the steps given below.
Step 1: Creating a Quad Curve
You can create a Quad Curve in JavaFX by instantiating the class named QuadCurve which belongs to a package javafx.scene.shape. You can then instantiate this class in start() method of Application class as shown in the following code block.
public class ClassName extends Application { public void start(Stage primaryStage) throws Exception { //Creating an object of the class QuadCurve QuadCurve quadcurve = new QuadCurve(); } }
Step 2: Setting Properties to the Quad Curve
Specify the x, y coordinates of the three points: start point, end point and control points, of the required curve, using their respective setter methods as shown in the following code block.
//Adding properties to the Quad Curve quadCurve.setStartX(100.0); quadCurve.setStartY(220.0f); quadCurve.setEndX(500.0f); quadCurve.setEndY(220.0f); quadCurve.setControlX(250.0f); quadCurve.setControlY(0.0f);
Or, by using their respective setter methods as follows −
QuadCurve quadcurve = new QuadCurve(startX, startY, controlX, controlY, endX, endY);
Step 3: Adding Quad Curve Object to Group
In the start() method, instantiate a Group class by passing the previously created QuadCurve object as a parameter value to its constructor −
Group root = new Group(quadcurve);
Step 4: Launching Application
Once the 2D object is created, follow the given steps below to launch the application properly −
-
Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters.
-
Then, set the title to the stage using the setTitle() method of the Stage class.
-
Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
-
Display the contents of the scene using the method named show().
-
Lastly, the application is launched with the help of the launch() method.
Example
Following is a program which generates a quadrilateral curve using JavaFX. Save this code in a file with the name QuadCurveExample.java.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.QuadCurve; public class QuadCurveExample extends Application { @Override public void start(Stage stage) { //Creating a QuadCurve QuadCurve quadCurve = new QuadCurve(); //Adding properties to the Quad Curve quadCurve.setStartX(100.0); quadCurve.setStartY(220.0f); quadCurve.setEndX(500.0f); quadCurve.setEndY(220.0f); quadCurve.setControlX(250.0f); quadCurve.setControlY(0.0f); //Creating a Group object Group root = new Group(quadCurve); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle("Drawing a Quad curve"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } }
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls QuadCurveExample.java java --module-path %PATH_TO_FX% --add-modules javafx.controls QuadCurveExample
Output
On executing, the above program generates a JavaFX window displaying a Bezier quadrilateral curve as shown in the following screenshot.
Example
Now, you can also draw this quadrilateral curve by applying any effect, say bloom effect, as shown in the example below. Save the code in a file named QuadCurveEffect.java.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.Bloom; import javafx.stage.Stage; import javafx.scene.paint.Color; import javafx.scene.shape.QuadCurve; public class QuadCurveEffect extends Application { @Override public void start(Stage stage) { //Creating a QuadCurve QuadCurve quadCurve = new QuadCurve(); //Adding properties to the Quad Curve quadCurve.setStartX(100.0); quadCurve.setStartY(220.0f); quadCurve.setEndX(300.0f); quadCurve.setEndY(220.0f); quadCurve.setControlX(250.0f); quadCurve.setControlY(0.0f); quadCurve.setFill(Color.RED); //Instantiating the Bloom class Bloom bloom = new Bloom(); //setting threshold for bloom bloom.setThreshold(0.1); //Applying bloom effect to quadCurve quadCurve.setEffect(bloom); //Creating a Group object Group root = new Group(quadCurve); //Creating a scene object Scene scene = new Scene(root, 400, 300); //Setting title to the Stage stage.setTitle("Drawing a Quad curve"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } }
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls QuadCurveEffect.java java --module-path %PATH_TO_FX% --add-modules javafx.controls QuadCurveEffect
Output
On executing, the above program generates a JavaFX window displaying a Bezier quadrilateral curve as shown in the following screenshot.
”;