JavaFX example to apply multiple transformations on a node

JavaFXObject Oriented ProgrammingProgramming

Transformation refers to a change on the node is in the XY plane. JavaFX supports four basic transforms namely −

  • Scale − Increase or decrease the size.

  • Rotate − Movement of the coordinates of a node around a fixed point with an angle.

  • Translate − Movement of the node in the XY plane.

  • Shear − Displacement of an object in a fixed direction such that its shape is slanted.

Every node in JavaFX contains an observable list to hold all the transforms to be applied on a node. You can get this list using the getTransforms() method. You can also add multiple transforms to a node.

Example

The following JavaFX example demonstrates the addition of multiple transforms to a node. It contains a 2D geometric shape and, three sliders, representing scale, rotate, and translate transforms.

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Scale;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
public class MultipleTransformations extends Application {
   public void start(Stage stage) {
      //Creating a rectangle
      Rectangle rect = new Rectangle(300, 100, 75, 75);
      rect.setFill(Color.BLUEVIOLET);
      rect.setStrokeWidth(5.0);
      rect.setStroke(Color.BROWN);
      //Creating Rotate transformation
      Slider slider1 = new Slider(0, 360, 0);
      Rotate rotate = new Rotate();
      //Setting pivot points for the rotation
      rotate.setPivotX(rect.getX());
      rotate.setPivotY(rect.getY());
      //Setting slider for rotation
      slider1.setShowTickLabels(true);
      slider1.setShowTickMarks(true);
      slider1.setMajorTickUnit(90);
      slider1.setBlockIncrement(10);
      slider1.setOrientation(Orientation.VERTICAL);
      slider1.valueProperty().addListener(new ChangeListener<Number>() {
         public void changed(ObservableValue <?extends Number>observable, Number oldValue, Number newValue){
            //Setting the angle for the rotation
            rotate.setAngle((double) newValue);
         }
      });
      //Creating the translation transformation
      Translate translate = new Translate();
      //Setting the slider for the translation
      Slider slider2 = new Slider(0, 200, 0);
      slider2.setOrientation(Orientation.VERTICAL);
      slider2.setShowTickLabels(true);
      slider2.setShowTickMarks(true);
      slider2.setMajorTickUnit(50);
      slider2.setBlockIncrement(50);
      slider2.valueProperty().addListener(new ChangeListener<Number>() {
         public void changed(ObservableValue <?extends Number>observable, Number oldValue, Number newValue){
            translate.setX(25);
            translate.setY((double) newValue);
            translate.setZ(100);
         }
      });
      //Creating the Scale transformation
      Scale scale = new Scale();
      scale.setPivotX(rect.getX());
      scale.setPivotY(rect.getY());
      //Setting the slider for the horizontal translation
      Slider slider3 = new Slider(0, 2.5, 1);
      slider3.setShowTickLabels(true);
      slider3.setShowTickMarks(true);
      slider3.setMajorTickUnit(0.5);
      slider3.setBlockIncrement(0.1);
      slider3.valueProperty().addListener(new ChangeListener<Number>() {
         public void changed(ObservableValue <?extends Number>observable, Number oldValue, Number newValue){
            scale.setX((double) newValue);
            scale.setY((double) newValue);
         }
      });
      //Adding all the transformations to the node
      rect.getTransforms().addAll(translate, rotate, scale);
      //Creating the pane
      BorderPane pane = new BorderPane();
      pane.setRight(new VBox(new Label("Rotate"), slider1));
      pane.setCenter(rect);
      pane.setBottom(new VBox(new Label("Translate (Y axis)"), slider2));
      pane.setLeft(new VBox(new Label("Scale"), slider3));
      //Preparing the scene
      Scene scene = new Scene(pane, 595, 300);
      stage.setTitle("Multiple Transformations");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

raja
Published on 19-May-2020 11:18:14
Advertisements