import javafx.application.Application; import javafx.beans.property.DoubleProperty; import javafx.scene.DepthTest; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.shape.Rectangle; import javafx.scene.transform.Rotate; import javafx.scene.transform.Translate; import javafx.stage.Stage; /* * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ /** * * @author Alexander Kouznetsov */ public class Depth3D extends Application { public static void main(String[] args) { System.setProperty("prism.verbose", "true"); System.setProperty("prism.depthtest", "true"); // System.setProperty("prism.multisample", "4"); launch(args); } @Override public void start(Stage stage) throws Exception { Bar3D bar3D = new Bar3D(20, 20, 0, 100, 100, 380); bar3D.setClip(new Rectangle(50, 50, 100, 100)); bar3D.getTransforms().setAll( new Translate(200, 200), new Rotate(20, Rotate.X_AXIS), new Rotate(-20, Rotate.Y_AXIS) ); Scene scene = new Scene(bar3D, 600, 600); scene.setCamera(new PerspectiveCamera()); stage.setScene(scene); stage.setVisible(true); } } class Bar3D extends Group { private DoubleProperty x; private DoubleProperty y; private DoubleProperty z; private DoubleProperty xs; private DoubleProperty ys; private DoubleProperty zs; public double getX() { return x.get(); } public double getXs() { return xs.get(); } public double getY() { return y.get(); } public double getYs() { return ys.get(); } public double getZ() { return z.get(); } public double getZs() { return zs.get(); } public void setX(double value) { x.set(value); } public void setXs(double value) { xs.set(value); } public void setY(double value) { y.set(value); } public void setYs(double value) { ys.set(value); } public void setZ(double value) { z.set(value); } public void setZs(double value) { zs.set(value); } public DoubleProperty getXProperty() { return x; } public DoubleProperty getXsProperty() { return xs; } public DoubleProperty getYProperty() { return y; } public DoubleProperty getYsProperty() { return ys; } public DoubleProperty getZProperty() { return z; } public DoubleProperty getZsProperty() { return zs; } public Bar3D() { this(0, 0, 0, 0, 0, 0); } public Bar3D(double x, double y, double z, double xs, double ys, double zs) { this.x = new DoubleProperty(x); this.y = new DoubleProperty(y); this.z = new DoubleProperty(z); this.xs = new DoubleProperty(xs); this.ys = new DoubleProperty(ys); this.zs = new DoubleProperty(zs); boolean cache = false; // Can't use Regions here due to RT-15091 Rectangle front, back, left, right, top, bottom; front = new Rectangle(); front.setStyle("-fx-fill: red; -fx-stroke: black; -fx-stroke-width: 1;"); front.setCache(cache); front.widthProperty().bind(this.xs); front.heightProperty().bind(this.ys); back = new Rectangle(); back.setStyle("-fx-fill: green; -fx-stroke: black; -fx-stroke-width: 1;"); back.setCache(cache); back.widthProperty().bind(this.xs); back.heightProperty().bind(this.ys); back.translateZProperty().bind(this.zs); left = new Rectangle(); left.setStyle("-fx-fill: red; -fx-stroke: black; -fx-stroke-width: 1;"); left.setCache(cache); left.widthProperty().bind(this.zs); left.heightProperty().bind(this.ys); left.getTransforms().setAll(new Rotate(-90, Rotate.Y_AXIS)); right = new Rectangle(); right.setStyle("-fx-fill: yellow; -fx-stroke: black; -fx-stroke-width: 1;"); right.setCache(cache); right.widthProperty().bind(this.zs); right.heightProperty().bind(this.ys); right.translateXProperty().bind(this.xs); right.getTransforms().setAll(new Rotate(-90, Rotate.Y_AXIS)); top = new Rectangle(); top.setStyle("-fx-fill: orange; -fx-stroke: black; -fx-stroke-width: 1;"); top.setCache(cache); top.widthProperty().bind(this.xs); top.heightProperty().bind(this.zs); top.getTransforms().setAll(new Rotate(90, Rotate.X_AXIS)); bottom = new Rectangle(); bottom.setStyle("-fx-fill: magenta; -fx-stroke: black; -fx-stroke-width: 1;"); bottom.setCache(cache); bottom.widthProperty().bind(this.xs); bottom.heightProperty().bind(this.zs); bottom.translateYProperty().bind(this.ys); bottom.getTransforms().setAll(new Rotate(90, Rotate.X_AXIS)); translateXProperty().bind(this.x); translateYProperty().bind(this.y); translateZProperty().bind(this.z); setAutoSizeChildren(false); setDepthTest(DepthTest.ENABLE); getChildren().setAll(front, bottom, top, right, left, back); } }