围绕自定义点旋转 javafx 图像视图
rotate javafx image view around custom point
我开始学习JavaFX
现在,当我尝试使用
旋转图像视图时
imgv.setRotate(angle);
它绕着它的中心轴旋转
但是当我尝试围绕图像内的自定义点旋转它时
使用
imgv.getTransforms().add(new Rotate(Angle,custom_x,custom_y);
它随机旋转,我不知道它的旋转轴
这与
相同
imgv.getTransforms().add(new Rotate(Angle,custom_x,custom_y,1.0,Rotate.Z_AXIS);
有什么方法可以围绕自定义点旋转图像
这是解释点位置的图像,如果 (0,0) 位于图像的左上角或图像的中心,我无法根据 x,y 围绕该点旋转图像
我知道我可以绕原点旋转然后进行平移但我想问是否有高速公路可以做到这一点
提前致谢
这是一个演示如何完成您所要求的应用程序。实现这一点的关键部分是 .getTransforms().add(..)
和 Rotate
。代码中的注释。我添加了 Circle
以便您可以实际看到旋转点所在的位置。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication117 extends Application
{
@Override
public void start(Stage primaryStage)
{
Image image = new Image("http://lmsotfy.com/so.png");
ImageView imageView = new ImageView(image);
imageView.setFitHeight(400);
imageView.setFitWidth(400);
Button btn = new Button();
btn.setText("Say 'Hello World'");
//Use this Circle to help see where the rotation occurs
Circle circle = new Circle(5);
circle.setFill(Color.RED);
circle.setCenterX(100);
circle.setCenterY(300);
//Add the Rotate to the ImageView's Transforms
Rotate rotation = new Rotate();
rotation.setPivotX(circle.getCenterX());//Set the Pivot's X to be the same location as the Circle's X. This is only used to help you see the Pivot's point
rotation.setPivotY(circle.getCenterY());//Set the Pivot's Y to be the same location as the Circle's Y. This is only used to help you see the Pivot's point
imageView.getTransforms().add(rotation);//Add the Rotate to the ImageView
//Use the Button's handler to rotate the ImageView
btn.setOnAction((ActionEvent event) -> {
rotation.setAngle(rotation.getAngle() + 15);
});
Pane pane = new Pane();
pane.getChildren().addAll(imageView, circle);
VBox.setVgrow(pane, Priority.ALWAYS);
VBox vBox = new VBox(pane, new StackPane(btn));
StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 1080, 720);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
我开始学习JavaFX 现在,当我尝试使用
旋转图像视图时imgv.setRotate(angle);
它绕着它的中心轴旋转 但是当我尝试围绕图像内的自定义点旋转它时 使用
imgv.getTransforms().add(new Rotate(Angle,custom_x,custom_y);
它随机旋转,我不知道它的旋转轴 这与
相同 imgv.getTransforms().add(new Rotate(Angle,custom_x,custom_y,1.0,Rotate.Z_AXIS);
有什么方法可以围绕自定义点旋转图像
这是解释点位置的图像,如果 (0,0) 位于图像的左上角或图像的中心,我无法根据 x,y 围绕该点旋转图像
我知道我可以绕原点旋转然后进行平移但我想问是否有高速公路可以做到这一点
提前致谢
这是一个演示如何完成您所要求的应用程序。实现这一点的关键部分是 .getTransforms().add(..)
和 Rotate
。代码中的注释。我添加了 Circle
以便您可以实际看到旋转点所在的位置。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication117 extends Application
{
@Override
public void start(Stage primaryStage)
{
Image image = new Image("http://lmsotfy.com/so.png");
ImageView imageView = new ImageView(image);
imageView.setFitHeight(400);
imageView.setFitWidth(400);
Button btn = new Button();
btn.setText("Say 'Hello World'");
//Use this Circle to help see where the rotation occurs
Circle circle = new Circle(5);
circle.setFill(Color.RED);
circle.setCenterX(100);
circle.setCenterY(300);
//Add the Rotate to the ImageView's Transforms
Rotate rotation = new Rotate();
rotation.setPivotX(circle.getCenterX());//Set the Pivot's X to be the same location as the Circle's X. This is only used to help you see the Pivot's point
rotation.setPivotY(circle.getCenterY());//Set the Pivot's Y to be the same location as the Circle's Y. This is only used to help you see the Pivot's point
imageView.getTransforms().add(rotation);//Add the Rotate to the ImageView
//Use the Button's handler to rotate the ImageView
btn.setOnAction((ActionEvent event) -> {
rotation.setAngle(rotation.getAngle() + 15);
});
Pane pane = new Pane();
pane.getChildren().addAll(imageView, circle);
VBox.setVgrow(pane, Priority.ALWAYS);
VBox vBox = new VBox(pane, new StackPane(btn));
StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 1080, 720);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}