JavaFX 简单3D示例

22 篇文章 30 订阅

  从Java8开始,在JavaFX中便增加了3D部分的内容,包括Camera,Material,Light,Shape3D等基础内容。

 当然,JavaFX 3D应该是OpenJFX里目前正在补充和完善的一个模块,很多地方还不尽如人意,所以该示例仅供参考。另外,OpenJFX目前已经有人通过RovoVM运行在Android和IOS的设备上了。不过,个人认为这个只是小打小闹,还远远不能进入实际运用当中。

  下面是JavaFX 3D示例,我会逐一解释:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.DrawMode;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;


public class Main extends Application {
	private Thread thread;
	private boolean isRunning = true;
	private PerspectiveCamera camera;
	private int speed = -1;
	private int count = 1;
	private int maxCount = 50;
	public Parent createContent() throws Exception {
        // Box
        Box testBox = new Box(5, 5, 5);
        testBox.setMaterial(new PhongMaterial(Color.BLUE));
        testBox.setDrawMode(DrawMode.FILL);
        
        // Create and position camera
        camera = new PerspectiveCamera(true);
        camera.getTransforms().addAll (
                new Rotate(-20, Rotate.Y_AXIS),
                new Rotate(-20, Rotate.X_AXIS),
                new Translate(0, 0, -20));
 
        // Build the Scene Graph
        Group root = new Group();       
        root.getChildren().add(camera);
        root.getChildren().add(testBox);
 
        // Use a SubScene       
        SubScene subScene = new SubScene(root, 310,310, true, SceneAntialiasing.BALANCED);
        subScene.setFill(Color.ALICEBLUE);
        
        subScene.setCamera(camera);
        Group group = new Group();
        group.getChildren().add(subScene);
        return group;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setResizable(false);
        Scene scene = new Scene(createContent(), 300, 300);
        thread = new Thread(new Runnable() {
			@Override
			public void run() {
			  	while(isRunning){
			  		try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
			  		Platform.runLater(new Runnable() {
						@Override
						public void run() {
					        camera.getTransforms().addAll(
					                new Translate(0, 0,speed));
					        count++;
					        if(count >= maxCount){
					        	speed = -speed;
					        	count = 0;
					        }
						}
					});
			  	}
			}
		});
        thread.start();
        primaryStage.setScene(scene);
        primaryStage.show();
    }
	
	public static void main(String[] args) {
		launch(args);
	}
}
  PerspectiveCamera是透视投影的摄像机,基本是3D开发中的标配了。Box是JavaFX 3D中内置的3D物体,通过setMaterial来设置材质,通过setDrawMode来设置绘制方式,有填充和线框两种模式。

  我们也可以通过Camera.getTransforms()来获取所有Object的Transform然后进行Rotate,Translate等变换。

  SubScene是一个子场景,是一个特殊的独立场景。我们可以通过SubScene来通过不同的Camera来渲染场景中的某一部分。例如2D UI,3D场景,整个背景的分离显示,也是很常见的用法。另外,SubScene中可以通过SceneAntialiasing来设置是否抗锯齿。

  在该示例中,我们另外通过线程对Camera中的transform进行translate变换,会循环移近移远。

  效果图:


  因为是动态变化的,大家可以自己运行看看效果。

  抗锯齿的效果也很明显,可以自行修改。

  本文章为个人原创,版权所有,转载请注明出处:http://blog.csdn.net/ml3947。另外我的个人博客:http://www.wjfxgame.com. 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个完整的JavaFX示例,展示了一个简单的登录界面: ```java import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class JavaFXExample extends Application { @Override public void start(Stage primaryStage) { primaryStage.setTitle("JavaFX Example"); // 创建GridPane布局 GridPane gridPane = new GridPane(); gridPane.setPadding(new Insets(10)); gridPane.setVgap(5); gridPane.setHgap(5); // 创建用户名标签 Label usernameLabel = new Label("用户名:"); GridPane.setConstraints(usernameLabel, 0, 0); // 创建用户名输入框 TextField usernameInput = new TextField(); GridPane.setConstraints(usernameInput, 1, 0); // 创建密码标签 Label passwordLabel = new Label("密码:"); GridPane.setConstraints(passwordLabel, 0, 1); // 创建密码输入框 PasswordField passwordInput = new PasswordField(); GridPane.setConstraints(passwordInput, 1, 1); // 创建登录按钮 Button loginButton = new Button("登录"); GridPane.setConstraints(loginButton, 1, 2); // 给登录按钮添加点击事件 loginButton.setOnAction(e -> { String username = usernameInput.getText(); String password = passwordInput.getText(); System.out.println("用户名: " + username); System.out.println("密码: " + password); // 在这里可以添加登录逻辑 }); // 将控件添加到GridPane布局中 gridPane.getChildren().addAll(usernameLabel, usernameInput, passwordLabel, passwordInput, loginButton); // 创建Scene并将GridPane布局添加进去 Scene scene = new Scene(gridPane, 300, 200); // 将Scene设置给Stage并显示 primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 在这个示例中,我们创建了一个JavaFX的主应用程序类,通过GridPane布局创建了一个简单的登录界面。界面包含一个用户名输入框、一个密码输入框和一个登录按钮。当点击登录按钮时,会获取用户名和密码的输入值,并在控制台上打印出来。 你可以将上述代码保存为Java文件,并编译并运行该Java文件,就可以看到一个简单的登录界面。你可以根据需要在此基础上添加更多的控件和逻辑来完善你的JavaFX应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值