java定义一个盒子类box_JavaFX 类 Box用法及代码示例

Box是JavaFX的一部分。 Box类定义3D框的width,height和depth。盒子以原点为中心。

该类的构造函数是:

Box():创建Box的空实例。

Box(double w, double h, double d):创建具有指定宽度,高度和深度的Box的空实例。

常用方法

方法

说明

getDepth()

得到盒子的深度。

getWidth()

得到盒子的宽度。

getHeight()

得到盒子的高度

setHeight(double v)

设置盒子的高度

setWidth(double v)

设置盒子的宽度

setDepth(double v)

设置盒子的深度

以下程序将说明Box类的用法。

Java程序创建一个盒子并将其显示在舞台上

该程序创建一个由名称框指示的Box(高度,宽度和深度作为参数传递)。盒子将在场景中创建,而场景又将被放置在舞台中。函数setTitle()用于为舞台提供标题。然后创建一个组,并附加框。该组将附加到场景。最后,调用show()方法以显示最终结果。

// 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程序创建一个框并添加透视相机以渲染3D对象

该程序创建一个由名称框指示的Box(高度,宽度和深度作为参数传递)。盒子将在场景中创建,而场景又将被放置在舞台中。函数setTitle()用于为舞台提供标题。然后创建一个组,并附加框。该组将附加到场景。最后,将调用show()方法以显示最终结果。将创建一个透视摄像机,并将其添加到场景中以3D渲染框。

// 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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值