Table of contents
1.
Introduction
2.
JavaFX Cubic Curve
2.1.
Constructor for the class:
2.2.
Other Methods of JavaFX
3.
Program Code to Create a Cubic Curve
3.1.
Code:
3.1.1.
Output:
4.
Program Code to Create a Quad Curve and set a fill for Quad Curve
4.1.
Code:
4.1.1.
Output
4.2.
Note:
5.
Frequently Asked Questions
5.1.
Explain some features of JavaFX?
5.2.
Why should we use JavaFX?
5.3.
Does JavaFX 2 Support JavaFX Script?
6.
Conclusion
Last Updated: Mar 27, 2024

JavaFX Cubic Curve

Author Akash Nagpal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaFX is a Java framework for developing desktop and rich Internet applications (RIA). JavaFX provides a comprehensive set of graphics and media APIs and uses the contemporary Graphical Processing Unit via hardware-accelerated graphics. JavaFX also has APIs that allow developers to integrate visual animation with user interface control. JavaFX applications may run on various platforms, including the Web, Mobile, and Desktop. In this blog, we will learn about the JavaFX Cubic Curve and its methods with the help of examples that are useful in making 2-D shapes.

JavaFX Cubic Curve

A CubicCurve is characterised by a third-degree polynomial function of two variables, which can be represented as :

Bezier curves are commonly utilised in computer graphics. They are parametric curves that seem smooth at all scales. Connecting points on the XY plane create these curves. 

CubicCurve is a JavaFX component. A cubic Bézier parametric curve segment in (x, y) coordinate space is defined by the CubiCurve class. The Cubic curve travels through the starting and finishing positions and the two control points. Bézier control points are used to specify the control points.

Constructor for the class:

Let’s learn about the constructors of the Cubic curve class from JavaFX.

  • CubicCurve(): It returns a new cubic curve object.
  • CubicCurve(double startX, double startY, double controlX1, double controlY1, double controlX2, double controlY2, double endX, double endY): This method produces a cubic curve instance using the supplied beginning and ending points as well as two control points.

Other Methods of JavaFX

Following are some other useful methods of Cubic Class that can be used for making various 2-D shapes.

Program Code to Create a Cubic Curve

This software generates a CubicCurve with the name cubic curve ( two control point s, the start point and end point, are passed as arguments). The CubicCurve will be generated within a scene, which will then be housed within a stage. The function setTitle() is used to give the stage a title. The cubic curve is then connected to the group. The group is involved in the scene. The show() function is then used to display the final findings.

Code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.CubicCurve;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
public class cubic_curve_0 extends Application {
  
    // launching the application
    public void start(Stage stage)
    {
        // setting the title for the stage
        stage.setTitle("creating cubic_curve");
  
        // creating a cubic curve
        CubicCurve cubic_curve = new CubicCurve(10.0f, 10.0f, 200.0f, 140.0f, 120.0f, 240.0f, 160.0f, 70.0f);
  
        // creating a Group
        Group group = new Group(cubic_curve);
  
        // translating the cubic-curve to a position
        cubic_curve.setTranslateX(100);
        cubic_curve.setTranslateY(100);
  
        // creating a scene
        Scene scene = new Scene(group, 500, 300);
  
        stage.setScene(scene);
  
        stage.show();
    }
  
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}

Output:

Code output

Program Code to Create a Quad Curve and set a fill for Quad Curve

This program generates a cubic curve (two control points, start point and end point, are set using setControlX1(), setControlY1(), setControlX2(), setControlY2(), setStartX(), setStartY(), setEndX(), and setEndY() methods. The CubicCurve will be produced within a scene, housed within a stage. The function setTitle() is used to give the stage a title. The cubic curve is then connected to the group. The group is involved in the scene. The show() function is then used to display the final findings. Set the fill for the cubic curve using the function setFill().

Code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.CubicCurve;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.paint.Color;
public class cubic_curve_1 extends Application {
  
    // launching the application
    public void start(Stage stage)
    {
        // setting title for the stage
        stage.setTitle("creating cubic_curve");
  
        // creating a cubic_curve
        CubicCurve cubic_curve = new CubicCurve();
  
 
        cubic_curve.setStartX(10.0f);
        cubic_curve.setStartY(10.0f);
  
        // setting control coordinates
        cubic_curve.setControlX1(200.0f);
        cubic_curve.setControlY1(140.0f);
        cubic_curve.setControlX2(120.0f);
        cubic_curve.setControlY2(240.0f);
  
        // setting end coordinates
        cubic_curve.setEndX(160.0f);
        cubic_curve.setEndY(70.0f);
  
        // creating a Group
        Group group = new Group(cubic_curve);
  
        // translating the cubic_curve to a position
        cubic_curve.setTranslateX(100);
        cubic_curve.setTranslateY(100);
  
        // setting fill for the cubic curve
        cubic_curve.setFill(Color.BLUE);
  
        // creating a scene
        Scene scene = new Scene(group, 500, 300);
  
        // set the scene
        stage.setScene(scene);
  
        stage.show();
    }
  
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}

Output

Code output

Note:

  • Save the above given codes as <class_name>.java.
  • compile and execute the above-stored java file from the command prompt using these commands:
    javac <class-name>.java
    java <class_name>

Frequently Asked Questions

Explain some features of JavaFX?

Developers may use this API to include vector graphics, animation, audio, and video Web elements into a rich, interactive, and immersive application. Java technology is extended by allowing the usage of any Java library within a JavaFX application.

Why should we use JavaFX?

JavaFX provides a comprehensive set of graphics and media APIs and uses the contemporary Graphical Processing Unit via hardware-accelerated graphics. JavaFX also has APIs that allow developers to integrate visual animation with user interface control.

Does JavaFX 2 Support JavaFX Script?

JavaFX Script is no longer supported as of JavaFX 2.0. Other scripting languages that run on the JVM, such as Groovy or Scala, can be used instead.

Conclusion

This article extensively discussed the JavaFX Cubic Curve and its methods with the help of examples that are useful in making 2-D shapes. In this article, we have discussed JavaFX Cubic Curve in detail. We hope this blog has helped you enhance your knowledge regarding JavaFX 2-D Shapes. 

To learn more, check out our JavaFX ArcJavaFX Circle, and JavaFX Parallel Transition articles. Practice makes a man perfect. To practice and improve yourself for the interviews, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow. Happy Coding!

Thank you image

Thank You Image

Live masterclass