围绕自定义点旋转javafx图像视图 [英] rotate javafx image view around custom point

查看:158
本文介绍了围绕自定义点旋转javafx图像视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用

imgv.setRotate(angle);

它围绕中心轴旋转
但是当我尝试围绕自定义点旋转它时图像内部
使用

it rotates around its central axis but when I try to rotate it around a custom point inside the image using

imgv.getTransforms().add(new Rotate(Angle,custom_x,custom_y);

它随机旋转我无法计算其旋转轴
这是相同于

it rotates randomly and I can't figure its axis of the rotation this was the same as

 imgv.getTransforms().add(new Rotate(Angle,custom_x,custom_y,1.0,Rotate.Z_AXIS);

有没有办法围绕自定义点旋转图像
这里是图像解释一个点的位置如果(0,0)位于图像的左上角或中心,我无法根据x,y
旋转图像,我知道我可以旋转原点然后做翻译,但我问是否有高速公路做这个
提前谢谢

is there any way to rotate the image around a custom point here is image explaining the position of a point if (0,0) was at the top left or at the center of the image both way I can't rotate the image around that point according to x,y I know that I can rotate around the origin then do translation but am asking if there is a speedway to do that thanks in advance

推荐答案

这是一款应用这表明了如何完成你的要求。完成此操作的关键部分是 .getTransforms()。add(..)旋转。代码中的注释。我添加了 Circle ,这样你就可以真正看到旋转点的位置。

Here is an app that demonstrates how to accomplish what you are asking. The key parts of accomplishing this are .getTransforms().add(..) and Rotate. Comments in the code. I added the Circle so that you can actually see where the point of rotation is located.

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图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆