我有3D场景,我在场景中有一个窗格,它有一个x轴旋转变换,我想用这个窗格作为战略游戏板,但我有问题.
当我在窗格中输入鼠标时,它给了我错误的光标位置.
例如,当我从窗格内部的左上角(红色圆圈)输入鼠标(带有黑色边框的旋转窗格)时,它应该显示我(0,0)作为窗格内的光标位置,但它显示类似于(200,400)的内容.
我怎么解决这个问题?
或换句话说,如何在节点上获取相对于节点及其变换的鼠标坐标?
这是一个例子:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.RotateBuilder;
import javafx.stage.Stage;
public class JFXRotationXOrds extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox();
root.getChildren().add(new Rectangle(20, 20, Color.BLUE));
root.getChildren().add(new Circle(20, Color.RED));
//root.rotateProperty().set(30);
root.getTransforms().add(RotateBuilder.create().angle(-30).pivotX(0).pivotY(100).axis(new Point3D(1, 0, 0)).build());
root.setStyle("-fx-border-color: black; -fx-border-width:5; ");
root.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent arg0) {
if(arg0.getEventType() == MouseEvent.MOUSE_MOVED){
System.out.println(arg0.getX() + "," + arg0.getY());
}
}
});
Scene scene = new Scene(root, 200, 500);
primaryStage.setTitle("Rotation Coordinates Example");
primaryStage.setScene(scene);
scene.setCamera(PerspectiveCameraBuilder.create().fieldOfView(10).build());
primaryStage.show();
}
public static void main(String[] args){
Application.launch(args);
}
}
最佳答案 更新
该问题已在JDK8 Early Access Release中得到修复.您可以下载该版本,也可以等到它出来.作为这张票的一部分,它在2月份被修复:RT-28129
编辑
我输入了ticket on the JavaFX Jira.您可以按照它查看状态更新.
我已经更新了演示以反映您的问题.当转换使用z轴时(昨天 – 今天对我不同)似乎有效,但是当转换在X或Y轴上时则不行.
希望这对你有所帮助.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Point3D;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.RotateBuilder;
import javafx.stage.Stage;
public class JFXRotationXOrds extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox();
final Rotate rotate = RotateBuilder.create().angle(80).pivotX(100).pivotY(100).pivotZ(0).axis(new Point3D(1,0,0)).build();
root.getTransforms().add(rotate);
root.setStyle("-fx-border-color: black; -fx-border-width:5; ");
root.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent arg0) {
if(arg0.getEventType() == MouseEvent.MOUSE_MOVED){
System.out.println(arg0.getSceneX() + "," + arg0.getSceneY());
}
}
});
Scene scene = new Scene(root, 200, 500);
PerspectiveCamera camera = new PerspectiveCamera();
scene.setCamera(camera);
primaryStage.setTitle("BorderPane Example");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
Application.launch(args);
}
}