个性化阅读
专注于IT技术分析

JavaFX缩放

本文概述

缩放是一种转换, 用于更改对象的大小。它可以扩大或缩小对象的大小。可以通过将对象的坐标乘以一个称为比例因子的因子来更改大小。在JavaFX中, 类javafx.scene.transform.Scale表示缩放转换。

在下图中, 缩放变换应用于多维数据集以扩展其大小。

JavaFX缩放

属性

下表描述了该类的属性。

属性 描述 设置方法
pivotX 这是一个双精度类型的属性。它表示围绕其缩放的枢轴点的x坐标。 setPivotX(double value)
pivotY 这是一个双精度类型的属性。它代表围绕其缩放的枢轴点的y坐标。 setPivotY(double value)
pivotZ 这是一个双精度类型的属性。它表示围绕其缩放的枢轴点的z坐标。 setPivotZ(double value)
x 这是一个双精度类型的属性。它代表对象沿X轴缩放的因子。 setX(double value)
y 这是一个双精度类型的属性。它代表对象沿Y轴缩放的因子。 setY(double value)
z 这是一个双精度类型的属性。它代表对象沿Z轴缩放的因子。 setZ(double value)

构造函数

该类包含以下描述的五个构造函数。

  1. public Sc​​ale():使用默认参数创建新实例。
  2. public Sc​​ale(double X, double Y):创建2D Scale的新实例。
  3. public Sc​​ale(double X, double Y, double Z):创建3D比例尺的新实例。
  4. public Sc​​ale(double X, double Y, double axisX, double axisY):使用指定的轴心坐标创建2D比例尺的新实例。
  5. public Sc​​ale(双X, 双Y, 双Z, 双PivotX, 双PivotY, 双PivotZ):使用指定的枢轴坐标创建3D比例尺的新实例。

例:

以下示例说明了缩放转换的实现。在这里, 我们创建了两个具有相同尺寸和颜色的圆。将缩放转换应用于第二个圆, 以使其在X-Y方向上均按因子1.5缩放。将缩放变换应用到第二个圆后, 它将获得第一个圆的1.5。

package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
public class ScaleExample extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
	// TODO Auto-generated method stub
	//Creating the two circles 
	Circle cir1=new Circle(230, 200, 100);
	Circle cir2=new Circle(550, 200, 100);
	
	//setting the color and stroke for the circles
	cir1.setFill(Color.YELLOW);
	cir1.setStroke(Color.BLACK);
	cir2.setFill(Color.YELLOW);
	cir2.setStroke(Color.BLACK);
	
	// creating the text nodes for the identification 
	Text text1 = new Text("Original Circle");
	Text text2 = new Text("Scaled with factor 1.5 in XY");
	
	//setting the properties for the text nodes
	text1.setX(150);
	text1.setY(400);
	text2.setX(400);
	text2.setY(400);
	text1.setFont(Font.font("calibri", FontWeight.BLACK, FontPosture.ITALIC, 20));
	text2.setFont(Font.font("calibri", FontWeight.BLACK, FontPosture.ITALIC, 20));
	
	//creating a 2D scale 
	Scale scale = new Scale();
	
	// setting the X-y factors for the scale 
	scale.setX(1.5);
	scale.setY(1.5);
	
	//setting the pivot points along which the scaling is done
	scale.setPivotX(550);
	scale.setPivotY(200);

	//applying transformations on the 2nd circle 
	cir2.getTransforms().add(scale);
	Group root = new Group();
	root.getChildren().addAll(cir1, cir2, text1, text2);
	Scene scene = new Scene(root, 800, 450);
	primaryStage.setScene(scene);
	primaryStage.setTitle("Scale Example");
	primaryStage.show();
	
}
public static void main(String[] args) {
	launch(args);
}
}
JavaFX缩放
赞(0)
未经允许不得转载:srcmini » JavaFX缩放

相关推荐

评论 抢沙发

评论前必须登录!