围绕自定义点

时间:2018-02-18 20:09:26

标签: java javafx javafx-8

我开始学习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旋转图像。 我知道我可以围绕原点旋转然后进行翻译,但我在问是否有高速公路可以做到这一点 提前致谢 enter image description here

1 个答案:

答案 0 :(得分:1)

这是一个应用程序,演示如何完成您的要求。完成此操作的关键部分是.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);
    }

}
相关问题