- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,这让我发疯。文档很薄弱,Oracle 的示例应用程序很奇怪,有一个巨大的复杂的帮助类,甚至这里的问题都没有答案!
我在很大程度上遵循并简化了 this tutorial ,但我不是旋转对象,而是尝试旋转相机,因此当您拖动鼠标时,它应该围绕相机运行。
然而,虽然我已经通过控制台日志和调试确认正在调用事件处理程序,并且一切似乎都有正确的值,但我的轮换从未发生过!我错过了什么?
此外,我根本无法移动相机,即使是(注释掉)translateX
之类的也不起作用,所以我很困惑,但不能让轴看起来像左上角以外的任何地方!
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
public class RotateCameraExample extends Group {
private double anchorX, anchorY;
private double anchorAngleX = 0;
private double anchorAngleY = 0;
private DoubleProperty angleX = new SimpleDoubleProperty(0);
private DoubleProperty angleY = new SimpleDoubleProperty(0);
Camera camera;
Group axes;
public RotateCameraExample() {
axes = buildAxes();
getChildren().add(axes);
camera = new PerspectiveCamera(true);
camera.setFarClip(6000);
camera.setNearClip(0.01);
//camera.translateYProperty().set(300); // this doesn't do anything! why?
getChildren().add(camera);
initMouseControl();
}
private void initMouseControl() {
Rotate xRotate = new Rotate(0, Rotate.X_AXIS);
Rotate yRotate = new Rotate(0, Rotate.Y_AXIS);
camera.getTransforms().addAll(xRotate, yRotate);
xRotate.angleProperty().bind(angleX);
yRotate.angleProperty().bind(angleY);
setOnMousePressed(event -> {
anchorX = event.getSceneX();
anchorY = event.getSceneY();
anchorAngleX = angleX.get();
anchorAngleY = angleY.get();
});
setOnMouseDragged(event -> {
angleX.set(anchorAngleX - (anchorY - event.getSceneY()));
angleY.set(anchorAngleY + anchorX - event.getSceneX());
});
}
private Group buildAxes() {
final Box xAxis = new Box(1200, 10, 10);
final Box yAxis = new Box(10, 1200, 10);
final Box zAxis = new Box(10, 10, 1200);
xAxis.setMaterial(new PhongMaterial(Color.RED));
yAxis.setMaterial(new PhongMaterial(Color.GREEN));
zAxis.setMaterial(new PhongMaterial(Color.BLUE));
Group axisGroup = new Group();
axisGroup.getChildren().addAll(xAxis, yAxis, zAxis);
return axisGroup;
}
}
在这里可以看到轴在左上角可见,我希望它在围绕它移动相机时保持在 (0, 0, 0) 处。
Application
启动代码,这显然不是问题:
public class TestApp extends Application {
@Override
public void start(Stage stage) throws IOException {
RotateCameraExample g = new RotateCameraExample();
Scene scene = new Scene(g, 800, 800, Color.BLACK);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
最佳答案
而不是将相机添加到 Group
的 children ,
getChildren().add(camera);
您应该设置场景的相机。
scene.setCamera(g.camera);
您将立即看到屏幕中央的轴。同样,鼠标处理程序应应用于场景。然后,您可以在场景处理程序中更新组的变换。
import javafx.application.Application;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
/**
* @see https://stackoverflow.com/a/69260181/230513
*/
public class RotateCameraExample extends Application {
private static class RotateCamera extends Group {
private final Camera camera;
private final Rotate xRotate = new Rotate(0, Rotate.X_AXIS);
private final Rotate yRotate = new Rotate(0, Rotate.Y_AXIS);
private final Rotate zRotate = new Rotate(0, Rotate.Z_AXIS);
public RotateCamera() {
buildAxes();
camera = new PerspectiveCamera(true);
camera.setFarClip(6000);
camera.setNearClip(0.01);
camera.setTranslateZ(-2000);
camera.getTransforms().addAll(xRotate, yRotate, zRotate);
}
private void buildAxes() {
final Box xAxis = new Box(1200, 10, 10);
final Box yAxis = new Box(10, 1200, 10);
final Box zAxis = new Box(10, 10, 1200);
xAxis.setMaterial(new PhongMaterial(Color.RED));
yAxis.setMaterial(new PhongMaterial(Color.GREEN));
zAxis.setMaterial(new PhongMaterial(Color.BLUE));
Group axisGroup = new Group();
axisGroup.getChildren().addAll(xAxis, yAxis, zAxis);
this.getChildren().add(axisGroup);
}
}
@Override
public void start(Stage stage) {
RotateCamera g = new RotateCamera();
Scene scene = new Scene(g, 800, 800, Color.BLACK);
scene.setCamera(g.camera);
stage.setScene(scene);
stage.show();
scene.setOnScroll((final ScrollEvent e) -> {
g.xRotate.setAngle(g.xRotate.getAngle() + e.getDeltaY() / 10);
g.yRotate.setAngle(g.yRotate.getAngle() - e.getDeltaX() / 10);
g.setTranslateX(g.getTranslateX() + e.getDeltaX());
g.setTranslateY(g.getTranslateY() + e.getDeltaY());
});
scene.setOnKeyPressed((KeyEvent e) -> {
KeyCode code = e.getCode();
switch (code) {
case LEFT:
g.zRotate.setAngle(g.zRotate.getAngle() + 10);
break;
case RIGHT:
g.zRotate.setAngle(g.zRotate.getAngle() - 10);
break;
case UP:
g.setTranslateZ(g.getTranslateZ() - 100);
break;
case DOWN:
g.setTranslateZ(g.getTranslateZ() + 100);
break;
case HOME:
g.xRotate.setAngle(0);
g.yRotate.setAngle(0);
g.zRotate.setAngle(0);
g.setTranslateX(0);
g.setTranslateY(0);
g.setTranslateZ(0);
break;
default:
break;
}
});
}
public static void main(String[] args) {
launch();
}
}
关于java - 最简单的通过鼠标旋转相机不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69249037/
我制作手机游戏,但我想为我的社交游戏制作一些简单的基于浏览器的客户端,以便我们可以更轻松地调试。最简单的引擎是什么? 我研究了一些 HTML5 引擎和 GWT,但我想听听社区的意见。我正在寻找一种能够
有一个有趣的例子,有人设法为一个只占用 1kb 的论坛创建了一个 PHP 脚本:http://www.nerdparadise.com/blogs/blake/6034/ 我想知道是否有类似的小脚本可
首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真tm的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易
我的proguard配置太糟糕了,我的游戏崩溃了,而且似乎不正常。在我弄清楚到底哪里出了问题之前,最简单,最安全的配置用于全部安装是什么?有没有办法使它仅更改变量名?或者只是混淆代码以使其更难阅读而不
我正在重构一些 C# 代码,其中一部分是重做一些引用,因为我们正在完全重做文件夹结构。我想做的就是进入 .csproj 或 .sln 文件并修改路径。 然而,一些引用文献有类似的路径 "../../.
免责声明:这是一个理论问题,目的是增加我的理解。我知道我总是可以使用像 JSON 库这样的工具来解决问题。 假设我想创建一个逗号分隔的值列表,这些值本身可能包含逗号。这些逗号需要先转义。假设我使用 .
我对编程完全陌生,我选择 Delphi 作为我想学习的编程语言。 我基本上想构建使用套接字填写和提交 Web 表单的工具,并且我希望它们也是多线程的。 我希望它们功能丰富且性能正确。 我并不急于这样做
我正在构建一个协作创作工具,该工具允许用户共同编辑信息空间,该信息空间是节点和链接的可视化。一个客户端应用程序中所做的更改需要反映到所有其他客户端中。由于它是可视化的,因此可能需要经常更新潜在的大数据
作为一家专门开发自定义 CMS 的公司,我们被要求在下一个项目中部署开源 CMS。 我们可以自由选择系统。对于熟悉 PHP5 中的 MVC 模型和 OOP 的团队,您会推荐什么? 有人告诉我Drupa
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我完全是 Ember.js 菜鸟,需要了解后端注意事项,目前似乎很少有教程涵盖。对于快速原型(prototype)设计,最简单/最简单的后端设置是什么?我看到了一些 ember-rails 教程,但是
我正在寻找这种最简单、最简单的方法来启动 Java Web 服务。我曾经使用 Axis-1-on-Tomcat,但是对于 Axis 2,它变得太厚了。我正在寻找的一些偏好: 低内存占用 - 一个包含最
我多年来一直使用旧版 openGL 和 cocoa,但现在我正在努力过渡到 openGL 3.2。互联网上有几个例子,但它们都太复杂了(许多甚至在 XCode 5.1 下不再编译)。有人可以编写一个最
我正在构建一个简单的应用程序,它应该将开关/支票簿的状态存储 7 天。我遇到的唯一问题是我用来构建所述应用程序的网站不适合手机上的本地存储。没有通过本地存储或链接到在线数据库来存储开关/支票簿状态的良
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 个月前。 Improv
有时我需要 1 个用户类型元素的集合(或任何其他容器)并以这种方式创建它们: boost::assign::list_of(typeVariable).convert_to_container >()
我的页面上有一个 DropDownList 和一个 TextBox。当用户在 DropDownList 中选择“其他”选项时,我想在其右侧显示一个文本框。我不想使用传统的回发技术。我希望这种交互是在客
说到编码,我还很年轻,而且我听说过很多关于组织的事情。有些使用部分,有些使用 div,有些使用 div 作为按钮,其他使用 css 中的输入来更改它。作为一个喜欢让他的代码干净、简单易懂但又正确的人,
我需要将带有变量项的 ContentValues 转换为 JSON 字符串,我可以将其保存到数据库中,并在以后用作 HTTP 请求的正文。网络上的所有内容都只会以相反的方向进行转换。 最佳答案 每当您
我的电脑上安装了 Python 3.7。想用tensorflow,发现基本不支持3.7,所以想(也)安装Python 3.6。 关于如何做到这一点有什么建议吗?我是否必须卸载 3.7 并将其替换为 3
我是一名优秀的程序员,十分优秀!