由于JavaFX 8中的新功能,可以将3D对象与2D UI控件结合使用。
我将这些文档用作手册:JavaFX Tutorial,Exploring JavaFX 3D。
所以,我制作了这段代码:
public class CastAnalytics extends Application {
final Group root = new Group();
final Group axisGroup = new Group();
final XForm world = new XForm();
final PerspectiveCamera camera = new PerspectiveCamera(true);
final PerspectiveCamera subSceneCamera = new PerspectiveCamera(false);
final XForm cameraXForm = new XForm();
final XForm cameraXForm2 = new XForm();
final XForm cameraXForm3 = new XForm();
final double cameraDistance = 450;
final XForm moleculeGroup = new XForm();
private Timeline timeline;
boolean timelinePlaying = false;
double CONTROL_MULTIPLIER = 0.1;
double SHIFT_MULTIPLIER = 0.1;
double ALT_MULTIPLIER = 0.5;
double mousePosX;
double mousePosY;
double mouseOldX;
double mouseOldY;
double mouseDeltaX;
double mouseDeltaY;
@Override
public void start(Stage primaryStage) throws Exception{
buildScene();
buildCamera();
buildAxes();
Scene scene = new Scene(root, 1024, 768, true);
scene.setFill(Color.GREY);
handleKeyboard(scene, world);
handleMouse(scene, world);
primaryStage.setTitle("Sample Application");
primaryStage.setScene(scene);
primaryStage.show();
scene.setCamera(subSceneCamera);
scene.setCamera(camera);
}
private void buildScene() {
root.getChildren().add(world);
Label label = new Label("123");
HBox hBox = new HBox();
hBox.getChildren().add(label);
SubScene subScene = new SubScene(hBox, 200, 200);
subScene.setLayoutX(100);
subScene.setLayoutY(100);
root.getChildren().addAll(subScene);
}
private void buildCamera() {
root.getChildren().addAll(cameraXForm);
cameraXForm.getChildren().add(cameraXForm2);
cameraXForm2.getChildren().add(cameraXForm3);
cameraXForm3.getChildren().add(camera);
cameraXForm3.setRotateZ(180.0);
camera.setNearClip(0.1);
camera.setFarClip(10000.0);
camera.setTranslateZ(-cameraDistance);
cameraXForm.ry.setAngle(320.0);
cameraXForm.rx.setAngle(40);
}
private void buildAxes() {
Box box = new Box(200,200,200);
axisGroup.getChildren().addAll(box);
world.getChildren().addAll(axisGroup);
}
private void handleMouse(Scene scene, final Node root) {
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent me) {
mousePosX = me.getSceneX();
mousePosY = me.getSceneY();
mouseOldX = me.getSceneX();
mouseOldY = me.getSceneY();
}
});
scene.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
mouseOldX = mousePosX;
mouseOldY = mousePosY;
mousePosX = me.getSceneX();
mousePosY = me.getSceneY();
mouseDeltaX = (mousePosX - mouseOldX);
mouseDeltaY = (mousePosY - mouseOldY);
double modifier = 1.0;
double modifierFactor = 0.1;
if (me.isControlDown()) {
modifier = 0.1;
}
if (me.isShiftDown()) {
modifier = 10.0;
}
if (me.isPrimaryButtonDown()) {
cameraXForm.ry.setAngle(cameraXForm.ry.getAngle() - mouseDeltaX * modifierFactor * modifier * 2.0); // +
cameraXForm.rx.setAngle(cameraXForm.rx.getAngle() + mouseDeltaY * modifierFactor * modifier * 2.0); // -
} else if (me.isSecondaryButtonDown()) {
double z = camera.getTranslateZ();
double newZ = z + mouseDeltaX * modifierFactor * modifier;
camera.setTranslateZ(newZ);
} else if (me.isMiddleButtonDown()) {
cameraXForm2.t.setX(cameraXForm2.t.getX() + mouseDeltaX * modifierFactor * modifier * 0.3); // -
cameraXForm2.t.setY(cameraXForm2.t.getY() + mouseDeltaY * modifierFactor * modifier * 0.3); // -
}
}
});
}
private void handleKeyboard(Scene scene, final Node root) {
final boolean moveCamera = true;
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
Duration currentTime;
switch (event.getCode()) {
case Z:
if (event.isShiftDown()) {
cameraXForm.ry.setAngle(0.0);
cameraXForm.rx.setAngle(0.0);
camera.setTranslateZ(-300.0);
}
cameraXForm2.t.setX(0.0);
cameraXForm2.t.setY(0.0);
break;
case X:
if (event.isControlDown()) {
if (axisGroup.isVisible()) {
axisGroup.setVisible(false);
} else {
axisGroup.setVisible(true);
}
}
break;
case S:
if (event.isControlDown()) {
if (moleculeGroup.isVisible()) {
moleculeGroup.setVisible(false);
} else {
moleculeGroup.setVisible(true);
}
}
break;
case SPACE:
if (timelinePlaying) {
timeline.pause();
timelinePlaying = false;
} else {
timeline.play();
timelinePlaying = true;
}
break;
case UP:
if (event.isControlDown() && event.isShiftDown()) {
cameraXForm2.t.setY(cameraXForm2.t.getY() - 10.0 * CONTROL_MULTIPLIER);
} else if (event.isAltDown() && event.isShiftDown()) {
cameraXForm.rx.setAngle(cameraXForm.rx.getAngle() - 10.0 * ALT_MULTIPLIER);
} else if (event.isControlDown()) {
cameraXForm2.t.setY(cameraXForm2.t.getY() - 1.0 * CONTROL_MULTIPLIER);
} else if (event.isAltDown()) {
cameraXForm.rx.setAngle(cameraXForm.rx.getAngle() - 2.0 * ALT_MULTIPLIER);
} else if (event.isShiftDown()) {
double z = camera.getTranslateZ();
double newZ = z + 5.0 * SHIFT_MULTIPLIER;
camera.setTranslateZ(newZ);
}
break;
case DOWN:
if (event.isControlDown() && event.isShiftDown()) {
cameraXForm2.t.setY(cameraXForm2.t.getY() + 10.0 * CONTROL_MULTIPLIER);
} else if (event.isAltDown() && event.isShiftDown()) {
cameraXForm.rx.setAngle(cameraXForm.rx.getAngle() + 10.0 * ALT_MULTIPLIER);
} else if (event.isControlDown()) {
cameraXForm2.t.setY(cameraXForm2.t.getY() + 1.0 * CONTROL_MULTIPLIER);
} else if (event.isAltDown()) {
cameraXForm.rx.setAngle(cameraXForm.rx.getAngle() + 2.0 * ALT_MULTIPLIER);
} else if (event.isShiftDown()) {
double z = camera.getTranslateZ();
double newZ = z - 5.0 * SHIFT_MULTIPLIER;
camera.setTranslateZ(newZ);
}
break;
case RIGHT:
if (event.isControlDown() && event.isShiftDown()) {
cameraXForm2.t.setX(cameraXForm2.t.getX() + 10.0 * CONTROL_MULTIPLIER);
} else if (event.isAltDown() && event.isShiftDown()) {
cameraXForm.ry.setAngle(cameraXForm.ry.getAngle() - 10.0 * ALT_MULTIPLIER);
} else if (event.isControlDown()) {
cameraXForm2.t.setX(cameraXForm2.t.getX() + 1.0 * CONTROL_MULTIPLIER);
} else if (event.isAltDown()) {
cameraXForm.ry.setAngle(cameraXForm.ry.getAngle() - 2.0 * ALT_MULTIPLIER);
}
break;
case LEFT:
if (event.isControlDown() && event.isShiftDown()) {
cameraXForm2.t.setX(cameraXForm2.t.getX() - 10.0 * CONTROL_MULTIPLIER);
} else if (event.isAltDown() && event.isShiftDown()) {
cameraXForm.ry.setAngle(cameraXForm.ry.getAngle() + 10.0 * ALT_MULTIPLIER); // -
} else if (event.isControlDown()) {
cameraXForm2.t.setX(cameraXForm2.t.getX() - 1.0 * CONTROL_MULTIPLIER);
} else if (event.isAltDown()) {
cameraXForm.ry.setAngle(cameraXForm.ry.getAngle() + 2.0 * ALT_MULTIPLIER); // -
}
break;
}
}
});
}
public static void main(String[] args) {
launch(args);
}
}
但结果并不是我的预期。我想让Pane
用于3D对象上方的UI控件,但我得到的是:
我做错了什么?
答案 0 :(得分:1)
根据我所理解的(有限的)测试,我有两种选择:
为子场景设置摄像机,并将该子场景添加到根目录。您将只使用一台相机。你的世界必须是一个独立的团体,飞行/旋转摄像机视图必须通过改造世界组来实现。
使用JavaFX jira提交错误报告。
我没有成功使用单独的相机作为子场景相机。没有应用于相机或子场景本身的变换曾经从默认位置旋转子场景,类似于屏幕截图中的位置。在这一点上,Oracle没有发布任何子场景文档,我们只能等到它们变得干净并填补空白。在此之前,我们可以考虑在JavaFX 3D中支持子网络支持。
答案 1 :(得分:0)
问题在于:
scene.setCamera(subSceneCamera);
scene.setCamera(camera);
您只能在Scene(或SubScene)中设置一个摄像头。 您需要在SubScene中设置第二个Camera。尝试这样的事情:
@Override
public void start(Stage primaryStage) throws Exception{
...
primaryStage.show();
scene.setCamera(camera);
}
private void buildScene() {
...
subScene.setLayoutX(100);
subScene.setLayoutY(100);
subScene.setCamera(subSceneCamera);
root.getChildren().addAll(subScene);
}
答案 2 :(得分:0)
这是解决方案
@using (Html.BeginForm("Save","Home"))
{
<div class="form-horizontal">
@if (TempData["CallFrom"] != null && Convert.ToString(TempData["CallFrom"]) == "SenditNow")
{
<h1>SenditNow</h1>
<input type="text" value="NameOfCustomer" />
<input type="text" value="Address" />
}
else
{
<h1>SafeForLater</h1>
<input type="text" value="NameOfCustomer" />
<input type="text" value="Address" />
}
<hr />
<button class="formular-button-submit" type="submit" name="submit" id="SenditNow" value="SenditNow">Send it now!</button>
<button class="formular-button-submit" type="submit" name="submit" id="SafeForLater" value="SafeForLater">Safe it for later!</button>
</div>
}