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

根據https://github.com/dlsc-software-consulting-gmbh/WorkbenchFX建立javafx示例:html

根據https://edvin.gitbooks.io/tornadofx-guide/part2/Integration.html 在javafx程序中使用tornadofx建立的viewjava

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:git

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)
            }
        }
    }
}
相關文章
相關標籤/搜索