JavaFX Transformation
In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes the form of the given shape by either translation, scaling, rotation, or shearing. All methods needed for this purpose are present in the javafx.scene.transform package.
Parent class of Transformation: Transform
The class Transfrom in javafx.scene.transform package is the parent class of all transformation classes. It changes the graphics of the object by applying various rules on the object. Also, we can apply multiple transformations to the same object.
Methods of the Transform class:
1. clone()
This method is used to return the deep copy of the transform.
2. createInverse()
This method is used to return the inverse transform of the given transform.
3. determinant()
This method is used to calculate the determinant of the transformation matrix.
4. isIdentity()
This method is used to retrieve the Boolean value for the property of the identity.
5. isType2D()
This method is used to retrieve the Boolean value for the property of the 2D type.
6. transform( double x, double y )
This method is used to transform the given point by a specified parameter.
7. translate( double x, double y)
This method is used to translate the given point by specified parameters.
8. scale( double x, double y)
This method is used to scale the given point by specified parameters.
9. rotate( double angle, double pivotx, double pivoty)
This method is used to rotate the given object around the pivot point by the specified angle.
Various transformations available in the JavaFX:
1. Scaling
This type of transformation is used in the JavaFX to change the actual size of the object. It alters the size of the given node, the javafx.scene.transform.Scale class is used for applying scaling transformation on the object.
2. Rotation
This type of transformation is used in the JavaFX to rotate the given object. It is used to rotate the given node by specified angle, the javafx.scene.transform.The rotation class is used for applying rotation transformation on the object.
3. Translation
This type of transformation is used in the JavaFX to translate the given object. It is used to change the position of the given node by a specified parameter, the javafx.scene.transform.Translate class is used for applying translation transformation on the object.
4. Multiple Transformation
This type of transformation is used in the JavaFX to apply multiple transformations to the given object. It is used to apply multiple transformations on the same object to show various effects.
5. Shearing
This type of transformation is used in the JavaFX to shear the given object. It is used to change the slope of the given node by a specified parameter, the javafx.scene.transform.Shear class is used for applying shearing transformation on the object.
Steps to apply Transformation Effect on the given node:
- Importing required libraries:
In order to apply transformation on objects in JavaFX, we have to import all the required libraries such as
- javafx.application.Application
- javafx.scene.Group
- javafx.scene.Scene
- javafx.scene.paint.Color
- javafx.scene.shape.Rectangle
- javafx.scene.transform.Translate
- javafx.scene.transform.Scale
- javafx.scene.transform.Rotate
- javafx.scene.transform.Shear
- javafx.stage.Stage
- Instantiating respective transformation class using the constructor:
In this step in order to apply a transformation on various objects in javafx, a constructor of either scaling, rotation, translation, or shearing is created. Then object from instantiating is used for the further process.
Example:
Translate translate = new Translate()
Scale scale = new Scale()
Rotate rotate = new Rotate()
Shear shear = new Shear()
- Setting the various properties for applying the transformation on the given object:
In order to apply a transformation on various objects in javafx, we have to apply various setters properties on the object of the constructor. This property gives us various transformation.
Example:
scale.setX(double xvalue);
scale.setY(double yvalue);
scale.setPivotX(double pivotxvalue);
scale.setPivotY(double pivotyvalue);
- Apply the transformation on the given node:
After setting all the properties on the given node, we can apply the transformation on the given node.