现场演示
->
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.paint.PhongMaterial; import javafx.stage.Stage; import javafx.scene.shape.Box; import javafx.scene.shape.CullFace; import javafx.scene.shape.Cylinder; import javafx.scene.shape.DrawMode; import javafx.scene.shape.Sphere; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class JavaFX3DShapesProperties extends Application { public void start(Stage stage) { //画一个盒子 Box cube = new Box(100, 120, 100); cube.setTranslateX(30.0); cube.setTranslateY(180.0); cube.setTranslateZ(150.0); //Setting the property "cull face" cube.setCullFace(CullFace.FRONT); //绘制圆柱 Cylinder cylinder = new Cylinder(50, 150); //设置圆柱体的属性 cylinder.setTranslateX(250.0); cylinder.setTranslateY(180.0); cylinder.setTranslateZ(150.0); //Setting the cull face "material" PhongMaterial material = new PhongMaterial(); material.setDiffuseColor(Color.DARKRED); cylinder.setMaterial(material); //绘制球体 Sphere sphere = new Sphere(75); sphere.setTranslateX(480.0); sphere.setTranslateY(180.0); sphere.setTranslateZ(150.0); //Setting the property "draw mode" sphere.setDrawMode(DrawMode.LINE); //设置透视相机 PerspectiveCamera cam = new PerspectiveCamera(); cam.setTranslateX(-50); cam.setTranslateY(50); cam.setTranslateZ(0); Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 15); Text label1 = new Text("CullFace: front"); label1.setFont(font); label1.setX(10); label1.setY(270); Text label2 = new Text("Material: colored"); label2.setFont(font); label2.setX(180); label2.setY(270); Text label3 = new Text("Draw Mode: line"); label3.setFont(font); label3.setX(370); label3.setY(270); //设置场景 Group root = new Group(cube, cylinder, sphere, label1, label2, label3); Scene scene = new Scene(root, 595, 300, Color.BEIGE); scene.setCamera(cam); stage.setTitle("JavaFX 3D Shape properties"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出结果