- Mastering JavaFX 10
- Sergey Grinev
- 388字
- 2021-06-25 21:21:57
Transformations
JavaFX API supports basic transformations for every Node (and Shape, which extends Node).
Three basic transformations can be used through Node methods:
- setRotate (double angle): Rotates around the center of the Node
- setTranslateX (double pixels), setTranslateY (double pixels): Shifts the Node by a set amount of pixels
- setScaleX (double scale), setScaleY (double scale): Increases (or decreases) the Node by multiplying its horizontal or vertical dimensions by scale
For more complex transformations, the Transform class can be used. It allows us to work precisely with every parameter of the transformation. For example, you can concatenate two transformations into one combined and use two different nodes to save.
Note that through Transform, there are usually more options available. For example, the setRotate() method always uses the center of the shape as a pivot point, whereas for the rotate transformation you can set a deliberate pivot point inside the shape:
Rotate rotateTransform = new Rotate();
rotateTransform.setAngle(45);
rotateTransform.setPivotX(10);
rotateTransform.setPivotY(10);
node.getTransforms().add(rotateTransform);
The following demo shows rotate, translate, scale, and shear transforms. Additionally, there is a fifth transformation that is a combination of shear and rotate. On the following figure, the black border is an original rectangle and the gray shape is the transformed one:

The code for the preceding figure is a bit long because of the double rectangle functionality; the actual transformations are at the end. Take a look at the following code snippet:
// chapter2/other/Transformations.java
public class Transformations extends Application {
// service method to make similar rectangles
private Rectangle addRect() {
// here we create two rectangles:
// one for transformation
Rectangle rect = new Rectangle(20, 100, Color.DARKGRAY);
// and another to demonstrate original
// untransformed rectangle bounds
Rectangle rectOrig = new Rectangle(20, 100);
rectOrig.setFill(Color.TRANSPARENT);
rectOrig.setStroke(Color.BLACK);
StackPane pane = new StackPane(rect, rectOrig);
root.getChildren().add(pane);
return rect;
}
TilePane root = new TilePane(50,50);
@Override
public void start(Stage primaryStage) {
// rotate transformation
Rectangle rect1 = addRect();
rect1.setRotate(30);
// translate transformation
Rectangle rect2 = addRect();
rect2.setTranslateY(20);
// scale transformation
Rectangle rect3 = addRect();
rect3.setScaleX(2);
// shear transformation
Rectangle rect4 = addRect();
rect4.getTransforms().add(new Shear(0.3, 0));
// combining two transformations
Rectangle rect5 = addRect();
Transform t1 = new Shear(0.3, 0);
Transform t2 = new Rotate(-15);
rect5.getTransforms().add(t1.createConcatenation(t2));
// adding all transformed rectangles to the scene
root.setPadding(new Insets(50));
primaryStage.setTitle("Hello World!");
primaryStage.setScene(new Scene(root, 500, 250));
primaryStage.show();
}
}
- 異構基因共表達網絡的分析方法
- Hands-On Industrial Internet of Things
- SD-WAN架構與技術(第2版)
- 物聯(lián)網之霧:基于霧計算的智能硬件快速反應與安全控制
- Echo Quick Start Guide
- 網絡利他行為研究:積極心理學的視角
- 轉化:提升網站流量和轉化率的技巧
- Implementing NetScaler VPX?
- 5G技術核心與增強:從R15到R17
- 互聯(lián)網安全的40個智慧洞見(2016)
- 基于IPv6的家居物聯(lián)網開發(fā)與應用技術
- 商業(yè)的本質和互聯(lián)網
- 5G智慧交通
- 加密與解密實戰(zhàn)全攻略
- SD-WAN 架構與技術