如何在 JavaFX 中创建 Box(3D)?
盒子是一种具有长度(深度)、宽度和高度的三维形状。在 JavaFX 中,盒子用 javafx.scene.shape.Box 类表示。此类包含 3 个属性,它们为 -
深度 - 此属性表示盒子的深度,你可以使用 setDepth() 方法为此属性设置值。
高度 - 此属性表示盒子的高度,你可以使用 setHeight() 方法为此属性设置值。
宽度 - 此属性表示盒子的宽度,你可以使用 setWidth() 方法为此属性设置值。
要创建一个 3D 盒子,你需要 -
实例化此类。
使用设置器方法设置所需属性,或者将它们作为参数传递给构造函数。
将创建的节点(形状)添加到 Group 对象。
范例
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.DrawMode;
public class DrawingBox extends Application {
public void start(Stage stage) {
//Drawing a Box
Box cube = new Box();
//Setting the properties of the Box(cube)
cube.setDepth(150.0);
cube.setHeight(150.0);
cube.setWidth(150.0);
//Setting other properties
cube.setCullFace(CullFace.BACK);
cube.setDrawMode(DrawMode.FILL);
PhongMaterial material = new PhongMaterial();
material.setDiffuseColor(Color.BROWN);
cube.setMaterial(material);
//Translating the box
cube.setTranslateX(300.0);
cube.setTranslateY(150.0);
cube.setTranslateZ(150.0);
//Setting the perspective camera
PerspectiveCamera cam = new PerspectiveCamera();
cam.setTranslateX(-150);
cam.setTranslateY(25);
cam.setTranslateZ(150);
//Setting the Scene
Group root = new Group(cube);
Scene scene = new Scene(root, 595, 300, Color.BEIGE);
scene.setCamera(cam);
stage.setTitle("Drawing A Cube");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP