Javafx轮换 [英] Javafx rotation

查看:194
本文介绍了Javafx轮换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我在其中绘制一个矩形,然后将其旋转90度。在这种情况下,我有一个框架,我可以移动我的矩形。但是在我转动它(90度的一个例子)之后,当区域向上移动时,矩形本身向右移动。旋转时,形状本身的坐标代理会变成什么?
轮换代码:

I have an application in which I draw a rectangle and then turn it 90 degrees. In this case, I have a frame with which I can move my rectangle. But after I turned it (an example of 90 degrees), when the region moves up, the rectangle itself moves to the right. When rotating, what does the coordinate proxy of the shape itself turn? Rotation code:

this.rotation.addListener((obs, old, fresh) -> { 
    Rotate rotate = new Rotate((double) fresh - (double) old,
        x.getValue().doubleValue() + (width.getValue().doubleValue() / 2),
        y.getValue().doubleValue() + (height.getValue().doubleValue() / 2));
    shape.getTransforms().add(rotate);
    rotate.angleProperty().bind(this.rotation);
});

旋转矩形时,其坐标轴随之旋转。

When you rotate the rectangle, its coordinate axis rotates with it.

如何在初始位置更新坐标轴时使其成为?

How to make it so that when you update the coordinate axis in the initial position?

推荐答案

我真的不明白你的问题,但这是一个演示旋转和移动矩形的例子。双击矩形以旋转它。 (如果点击太快,双击并不完美。)拖动矩形移动它。

I don't truly understand your question but here is an example that demos rotating and moving a rectangle. Double click the rectangle to rotate it. (Double click is not perfect if clicking too fast.). Drag the rectangle to move it.

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class JavaFXApplication extends Application
{

    final ObjectProperty<Point2D> mousePosition = new SimpleObjectProperty<>();

    public static void main(String[] args)
    {

        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage)
    {
        Rotate rotate = new Rotate();

        Rectangle rectangle = new Rectangle(33, 100, Color.GREEN);
        rectangle.setX((1200 / 2) - (33 / 2));
        rectangle.setY((900 / 2) - (100 / 2));
        rectangle.rotateProperty().bind(rotate.angleProperty());
        rectangle.setOnMouseClicked((event) -> {
            if (event.getButton().equals(MouseButton.PRIMARY)) {
                if (event.getClickCount() == 2) {
                    System.out.println("Double Clicked!");
                    rotate.setAngle(rotate.getAngle() + 90);

                }
            }
        });

        rectangle.setOnMousePressed((MouseEvent event) -> {
            mousePosition.set(new Point2D(event.getSceneX(), event.getSceneY()));
        });

        rectangle.setOnMouseDragged((MouseEvent event) -> {
            double deltaX = event.getSceneX() - mousePosition.get().getX();
            double deltaY = event.getSceneY() - mousePosition.get().getY();
            rectangle.setLayoutX(rectangle.getLayoutX() + deltaX);
            rectangle.setLayoutY(rectangle.getLayoutY() + deltaY);
            mousePosition.set(new Point2D(event.getSceneX(), event.getSceneY()));
        });

        Pane root = new Pane(rectangle);

        Scene scene = new Scene(root, 1200, 900, Color.LIGHTGRAY);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Path Transition Example");
        primaryStage.show();
    }
}

这篇关于Javafx轮换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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