在javafx程序中使用tornadofx创建的view

本文介绍了一个结合JavaFX与TornadoFX的示例项目,展示了如何在JavaFX应用程序中使用TornadoFX创建并集成3D视图。通过自定义模块,将TornadoFX的Sphere3D视图嵌入到JavaFX工作台中,实现了动态的3D球体显示与交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据https://github.com/dlsc-software-consulting-gmbh/WorkbenchFX创建javafx示例:

根据https://edvin.gitbooks.io/tornadofx-guide/part2/Integration.html 在javafx程序中使用tornadofx创建的view

CustomDemo.java
import com.dlsc.workbenchfx.Workbench;
import javafx.application.Application;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.stage.Stage;
import tornadofx.FX;

public class CustomDemo extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        Workbench customWorkbench = Workbench.builder(
                new CustomModule()
        ).build();

        Scene myScene = new Scene(customWorkbench);
        myScene.setCamera(new PerspectiveCamera());
        primaryStage.setScene(myScene);
        primaryStage.setWidth(1024);
        primaryStage.setHeight(768);
        FX.registerApplication(this, primaryStage);
        primaryStage.show();
    }
}
CustomModule.java
import JavaFX3D.Sphere3D;
import com.dlsc.workbenchfx.model.WorkbenchModule;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIcon;
import javafx.scene.Group;
import javafx.scene.Node;
import tornadofx.FX;

public class CustomModule extends WorkbenchModule {
    public CustomModule() {
        super("My first Workbench module", MaterialDesignIcon.THUMB_UP); // A name and an icon is required
    }
    @Override
    public Node activate() {
        Group gp= FX.find(Sphere3D.class).getRoot();
        return gp; // return here the actual content to display
    }
}

tornadofx创建的view:

Sphere3D.kt

import javafx.scene.PerspectiveCamera
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import javafx.scene.paint.Color
import javafx.scene.shape.Sphere
import tornadofx.*

class Sphere3D : View("Sphere3D") {
    val sphere = Sphere(50.0)
    val WIDTH = 1400
    val HEIGHT = 800
    val camera = PerspectiveCamera()

    override val root = group {
        style {
            fill= Color.SILVER
        }
        sphere.translateXProperty().set((WIDTH / 4).toDouble())
        sphere.translateYProperty().set((HEIGHT / 4).toDouble())
        add(sphere)
        hbox(5) {
            button("far") {
                action {
                    sphere.translateZProperty().set(sphere.translateZ + 100)
                }
            }
            button("near") {
                action {
                    sphere.translateZProperty().set(sphere.translateZ - 100)
                }
            }
        }
        addEventHandler(KeyEvent.KEY_PRESSED) { event ->
            when (event.getCode()) {
                KeyCode.W -> sphere.translateZProperty().set(sphere.translateZ + 100)
                KeyCode.S -> sphere.translateZProperty().set(sphere.translateZ - 100)
            }
        }
    }
}

 

转载于:https://my.oschina.net/u/3820046/blog/3080336

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值