JavaFX - 2D Shapes Quad Curve


Advertisements


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

Quadcurve

In JavaFX, a QuadCurve 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 QuadCurve, 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, as follows −

QuadCurve quadcurve = new QuadCurve(startX, startY, controlX, controlY, endX, endY);

Or, by using their respective setter methods as follow −

setStartX(value); 
setStartY(value); 
setControlX(value); 
setControlY(value); 
setEndX(value); 
setEndY(value); 

Steps to Draw Quadcurve

To Draw a Bezier Quadrilateral Curve in JavaFX, follow the steps given below.

Step 1: Creating a Class

Create a Java class and inherit the Application class of the package javafx.application. Then you can implement the start() method of this class as follows.

public class ClassName extends Application { 
   @Override     
   public void start(Stage primaryStage) throws Exception {     
   }    
}

Step 2: Creating a QuadCurve

You can create a QuadCurve in JavaFX by instantiating the class named QuadCurve which belongs to a package javafx.scene.shape. You can then instantiate this class as shown in the following code block.

//Creating an object of the class QuadCurve 
QuadCurve quadcurve = new QuadCurve();

Step 3: Setting Properties to the QuadCurve

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);

Step 4: Creating a Group Object

In the start() method, create a group object by instantiating the class named Group, which belongs to the package javafx.scene.

Pass the QuadCurve (node) object created in the previous step as a parameter to the constructor of the Group class, in order to add it to the group as follows −

Group root = new Group(quadcurve);

Step 5: Creating a Scene Object

Create a Scene by instantiating the class named Scene which belongs to the package javafx.scene. To this class pass the Group object (root) created in the previous step.

In addition to the root object, you can also pass two double parameters representing height and width of the screen along with the object of the Group class as follows.

Scene scene = new Scene(group ,600, 300);

Step 6: Setting the Title of the Stage

You can set the title to the stage using the setTitle() method of the Stage class. The primaryStage is a Stage object which is passed to the start method of the scene class, as a parameter.

Using the primaryStage object, set the title of the scene as Sample Application as follows.

primaryStage.setTitle("Sample Application");

Step 7: Adding Scene to the Stage

You can add a Scene object to the stage using the method setScene() of the class named Stage. Add the Scene object prepared in the previous steps using this method as follows.

primaryStage.setScene(scene);

Step 8: Displaying the Contents of the Stage

Display the contents of the scene using the method named show() of the Stage class as follows.

primaryStage.show();

Step 9: Launching the Application

Launch the JavaFX application by calling the static method launch() of the Application class from the main method as follows.

public static void main(String args[]){   
   launch(args);      
} 

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 QuadCurveExample.java 
java QuadCurveExample

On executing, the above program generates a JavaFX window displaying a Bezier quadrilateral curve as shown in the following screenshot.

Drawing Quadcurve
javafx_2d_shapes.htm

Advertisements