×

JavaFX Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All methods needed for this purpose are present in the javafx.scene.transform.Translate class.

Properties and corresponding methods to create Translate Transformation :

1. X      

This is a double type of property. It is used to represent the x distance by which the object is to be translated and the setX( Double value ) method is used to set it.

2. Y

This is double type property. It is used to represent the y distance by which the object is to be translated and the setY( Double value ) method is used to set it.

3. Z

This is double type property. It is used to represent the z distance by which the object is to be translated and the setZ( Double value ) method is used to set it.

JavaFX Transformation – Translate Transformation

Example:

import javafx.application.Application;  
import javafx.scene.Group;  
import javafx.scene.Scene;  
import javafx.scene.paint.Color;  
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.transform.Translate;  
import javafx.stage.Stage;  




public class TransformUI extends Application{  
@Override  
public void start(Stage primaryStage) throws Exception {  
    
Text text = new Text();  
     text.setText(" Text Without Translate Effect ");  
     text.setX(40);  
     text.setY(40);  
     text.setFont(Font.font("Times New Roman",FontWeight.BLACK,FontPosture.ITALIC,30));  
     text.setFill(Color.RED);  
     text.setStroke(Color.BROWN);  
     text.setUnderline(true);  
     
     Text text2 = new Text();  
     text2.setText(" Text With Translate Effect ");  
     text2.setX(20);  
     text2.setY(20);  
     text2.setFont(Font.font("Times New Roman",FontWeight.BLACK,FontPosture.ITALIC,30));  
     text2.setFill(Color.RED);  
     text2.setStroke(Color.BROWN);  
     text2.setUnderline(true);  
      
    Translate translate = new Translate();  
      
    translate.setX(140);  
    translate.setY(200);  
    translate.setZ(200); 
      
    text2.getTransforms().addAll(translate);  
      
    Group root = new Group();  
    root.getChildren().addAll(text,text2);  
    Scene scene = new Scene(root,600,400);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle("JavaFX Translation Example");  
    primaryStage.show();  
}  


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

Output:

JavaFX Effect – Transformation

Explanation:

In order to create translate transformation in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.transform.Translate, javafx.scene.paint.Color, javafx.scene.text.Font, javafx.scene.text.FontPosture, javafx.scene.text.FontWeight, javafx.scene.text.Text, javafx.stage.Stage.

Then we have created one class named TransformUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold text with and without the translate transformation, a Group object is created which is then passed to the Scene class object.

The text is created using the constructor and various properties are set using setters. The translate transformation constructor is created and then using the setters various properties are set.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX Translation example”. Also, it displays the text with and without the translate transformation.

JavaFX Transformation – Translate Transformation on Rectangle:

Example:

import javafx.application.Application;  
import javafx.scene.Group;  
import javafx.scene.Scene;  
import javafx.scene.paint.Color;  
import javafx.scene.shape.Rectangle;  
import javafx.scene.transform.Translate;  
import javafx.stage.Stage;  




public class TransformUI extends Application{  
@Override  
public void start(Stage primaryStage) throws Exception {  
    
    Rectangle rect1 = new Rectangle(40,40,220,180);  
    Rectangle rect2 = new Rectangle(40,40,220,180);  
      
    rect1.setFill(Color.RED);  
    rect1.setStroke(Color.BLACK);  
    rect1.setStrokeWidth(5);  
    rect2.setFill(Color.BLUE);  
    rect2.setStroke(Color.BLACK);  
    rect2.setStrokeWidth(5);  
      
    Translate translate = new Translate();  
      
    translate.setX(240);  


   
      
    rect2.getTransforms().addAll(translate);  
      
    Group root = new Group();  
    root.getChildren().addAll(rect1,rect2);  
    Scene scene = new Scene(root,600,400);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle("JavaFX Translation Example");  
    primaryStage.show();  
}  


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

Output:

JavaFX Effect – Transformation

Explanation:

In order to create translate transformation 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.stage.Stage. 

Then we have created one class named TransformUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a rectangle with and without the translate transformation, a Group object is created which is then passed to the Scene class object.

The rectangle is created using the constructor and various properties are set using setters. The translate transformation constructor is created and then using the setters various properties are set.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX Translation example”. Also, it displays the rectangle with and without the translate transformation.


Related Topics

JavaFX Box

In the JavaFX application, in order to draw a Box, the Box class is used. The Box allows us to form 3D shape having rectangular faces. All methods needed for...

4 minutes read.

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Pause Transition

In the JavaFX application, in order to generate the pause transition, we have to apply pause transition on any particular shape. It has the ability to take a particular pause...

4 minutes read.

JavaFX Slider

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range...

4 minutes read.

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

3 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All...

4 minutes read.

JavaFX Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 minutes read.

JavaFX Drop Shadow

In the JavaFX application, in order to apply various Drop Shadow effect, we use this class. It has the ability to create the drop shadow of the given object. This...

4 minutes read.

JavaFX Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

4 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Sphere

In the JavaFX application, in order to draw a Sphere, the Sphere class is used. The Sphere allows us to form a 3D shape having circular faces. All methods needed...

4 minutes read.

JavaFX Fade Transition

In the JavaFX application, in order to generate the fade transition, we have to apply fade transition on any particular shape. It has the ability to show the fading colors...

4 minutes read.

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 minutes read.

JavaFX HyperLink

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

4 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 minutes read.

JavaFX - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life...

4 minutes read.

JavaFX Path Transition

In the JavaFX application, in order to generate the path transition, we have to apply path transition on any particular shape. It has the ability to traverse the given shape...

3 minutes read.