有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JavaFX矩形旋转轴从左上角以外的点开始

我已经使用构造函数将JavaFxRotation应用于矩形,该构造函数允许您设置轴

new Rotate(45, 15, 15)

这会旋转矩形,但它会围绕放置它的AnchorPane上的15,15左上角旋转。是否可以围绕一个点旋转一个矩形,该点的作用类似于圆心,并且该矩形围绕圆周旋转。与矩形类似,轮胎上的一块胎面围绕中心轴旋转。谢谢你


共 (1) 个答案

  1. # 1 楼答案

    这对我有用,但我不确定你在做什么

    package helloworld;
    
    import javafx.animation.KeyFrame;
    import javafx.animation.KeyValue;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Ellipse;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.transform.Rotate;
    import javafx.stage.Stage;
    
    import java.time.Duration;
    
    /**
     * Created by Matt on 25/08/16.
     */
    public class RotatingARectangle extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
            primaryStage.setTitle("Hello World!");
    
            Group root = new Group();
    
            Rectangle rect = new Rectangle(190, 395, 20, 5);
            rect.setFill(Color.BLUE);
            Rotate rot = new Rotate(0, 200, 200);
            rect.getTransforms().add(rot);
    
            Ellipse path = new Ellipse(200, 200, 200, 200);
            path.setStroke(Color.RED);
            path.setFill(null);
    
            root.getChildren().add(rect);
            root.getChildren().add(path);
    
            Scene scene = new Scene(root, 400, 400);
            primaryStage.setScene(scene);
            primaryStage.show();
    
            Timeline line = new Timeline(30);
    
            KeyFrame key1 = new KeyFrame(
                    new javafx.util.Duration(0),
                    new KeyValue(rot.angleProperty(), 0 )
            );
    
            KeyFrame key2 = new KeyFrame(
                new javafx.util.Duration(1000),
                new KeyValue(rot.angleProperty(), 360 )
            );
            line.getKeyFrames().addAll(key1, key2);
    
            scene.addEventHandler(MouseEvent.MOUSE_CLICKED, evt->{
                line.playFromStart();
            });
    
    
    
        }
    
    }
    

    我将轴设置为elipse的中心,然后使用时间线将角度从0更改为360