JavaFX Tutorial - JavaFX Transformations








JavaFX transformations related classes are located in the javafx.scene.transform package and are subclasses of the Transform class.

A transformation is about how to change the coordinate system. The following types of transformations are supported in JavaFX:

  • Translation
  • Rotation
  • Scaling
  • Shearing

JavaFX Transformations can operate on three coordinates, thus allow us to create three-dimensional 3D objects and effects.

JavaFX implements transformation constructors with the x-axis and y-axis along with the x, y, and z axes.

To create a two-dimensional 2D effect, specify only the x and y coordinates. To create a 3D effect, specify all three coordinates.





Translation

The translation transformation shifts a node from one place to another along one of the axes relative to its initial position. The initial position of the xylophone bar is defined by x, y, and z coordinates.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/*from   w  w w  .j a v  a 2  s  . c  o  m*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Text Fonts");

    Group rectangleGroup = new Group();
    Scene scene = new Scene(rectangleGroup, 550, 250);

    Rectangle rect = new Rectangle();
    rect.setWidth(100);
    rect.setHeight(100);
    rect.setTranslateX( 135);
    rect.setTranslateY(11.0);
    
    rectangleGroup.getChildren().add(rect);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null




Rotation

The rotation transformation moves the node around a specified pivot point.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
//from   w ww .ja v a  2  s .com
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Text Fonts");

    Group rectangleGroup = new Group();
    Scene scene = new Scene(rectangleGroup, 550, 250);

    Rectangle rect = new Rectangle();
    rect.setWidth(100);
    rect.setHeight(100);

    rect.setRotate(10);
    rectangleGroup.getChildren().add(rect);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null

Scaling

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/*from   www.  j  av a 2s .  co m*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Text Fonts");

    Group rectangleGroup = new Group();
    Scene scene = new Scene(rectangleGroup, 550, 250);

    Rectangle rect = new Rectangle();
    rect.setWidth(100);
    rect.setHeight(100);

    rect.setScaleY(2);
    rectangleGroup.getChildren().add(rect);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null

Shearing

To shear, use the Shear class or the shear method of the Transform class.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Shear;
import javafx.stage.Stage;
/*from  w ww  .  j  a v a 2s  .c  o  m*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Text Fonts");

    Group rectangleGroup = new Group();
    Scene scene = new Scene(rectangleGroup, 550, 250);

    Rectangle rect = new Rectangle();
    rect.setWidth(100);
    rect.setHeight(100);

    Shear sh = new Shear();
    sh.setY(0.4);
    rect.getTransforms().add(sh);
    
    rectangleGroup.getChildren().add(rect);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null