JavaFX |带有示例的框
Box 是 JavaFX 的一部分。 Box 类定义了一个 3 维的盒子宽度、高度和深度。该框以原点为中心。
类的构造函数是:
- Box() :创建一个空的 Box 实例。
- Box(double w, double h, double d) :创建具有指定宽度、高度和深度的 Box 的空实例。
常用方法
method | explanation |
---|---|
getDepth() | get the depth of the box. |
getWidth() | get the width of the box. |
getHeight() | get the height of the box |
setHeight(double v) | set the height of the box |
setWidth(double v) | set the width of the box |
setDepth(double v) | set the depth of the box |
下面的程序将说明 Box 类的使用。
Java程序创建一个盒子并将其显示在舞台上
该程序创建一个由名称 box 指示的 Box(高度、宽度和深度作为参数传递)。盒子将在场景中创建,而场景又将托管在舞台中。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加盒子。组附加到场景。最后调用 show() 方法显示最终结果。
Java
// Java program to create a box and display it on the stage
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Box;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
public class box_0 extends Application {
// launch the application
public void start(Stage stage)
{
// set title for the stage
stage.setTitle("creating box");
// create a box
Box box = new Box(200.0f, 120.0f, 150.0f);
// create a Group
Group group = new Group(box);
// translate the box to a position
box.setTranslateX(100);
box.setTranslateY(100);
// create a scene
Scene scene = new Scene(group, 500, 300);
// set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Java
// Java program to create a box and add a
// perspective camera to render the 3D object
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Box;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
public class box_1 extends Application {
// launch the application
public void start(Stage stage)
{
// set title for the stage
stage.setTitle("creating box");
// create a box
Box box = new Box(70.0f, 70.0f, 40.0f);
// create a Group
Group group = new Group(box);
// translate the box to a position
box.setTranslateX(100);
box.setTranslateY(100);
// create a perspective camera
PerspectiveCamera perspectivecamera = new PerspectiveCamera(false);
perspectivecamera.setTranslateX(0);
perspectivecamera.setTranslateY(0);
perspectivecamera.setTranslateZ(0);
// create a scene
Scene scene = new Scene(group, 500, 300);
// set camera for scene
scene.setCamera(perspectivecamera);
// set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
输出:
Java程序创建一个盒子并添加一个透视相机来渲染 3D 对象
该程序创建一个由名称 box 指示的 Box(高度、宽度和深度作为参数传递)。盒子将在场景中创建,而场景又将托管在舞台中。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加盒子。组附加到场景。最后,调用 show() 方法显示最终结果。将创建透视相机并将其添加到场景中,以 3D 渲染框。
Java
// Java program to create a box and add a
// perspective camera to render the 3D object
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Box;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
public class box_1 extends Application {
// launch the application
public void start(Stage stage)
{
// set title for the stage
stage.setTitle("creating box");
// create a box
Box box = new Box(70.0f, 70.0f, 40.0f);
// create a Group
Group group = new Group(box);
// translate the box to a position
box.setTranslateX(100);
box.setTranslateY(100);
// create a perspective camera
PerspectiveCamera perspectivecamera = new PerspectiveCamera(false);
perspectivecamera.setTranslateX(0);
perspectivecamera.setTranslateY(0);
perspectivecamera.setTranslateZ(0);
// create a scene
Scene scene = new Scene(group, 500, 300);
// set camera for scene
scene.setCamera(perspectivecamera);
// set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
输出:
注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译器
参考
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Box.html