我可以在 JavaFXCollection 中放置 3D 几何对象吗?

Can I place 3D geometry objects in a JavaFXCollection?

我有一些 3D 几何对象,如球体、管状体、立方体等。我使用 类 的常用方式生成,例如 SphereCylinderBox FXMLcontroller 中基于 FXML 的菜单中的等。这意味着对象 box1@FXMLmakeCube 类方法的本地对象。

现在我想在这个控制器中的另一个方法中执行一些操作,如布尔运算、复制、镜像等。我想将所有创建的几何图形保留在 JavaFXCollection 类型的列表中,以便我可以从任何其他方法中调用这些几何图形的句柄。

我的问题是我该怎么做?我如何在同一个 FXMLController?

中的其他方法中引用此句柄

我在网上没有找到确切的问题。

您可以将所有这些 3D 对象放在一个集合中,因为它们都来自 Shape3D

您可以创建一个 ObservableList<Shape3D> 集合,并在创建它们时将每个对象添加到其中。然后你可以监听集合中的变化,并添加到scene/subscene所有新对象。

这将是一个带有四个按钮的控制器示例,您可以在其中创建随机 BoxSphere 3D 对象,将它们添加到集合中,并将它们放置在子场景中。

您还可以对整个集合执行操作(在这种情况下平移或旋转它们)。

public class FXMLDocumentController {

    @FXML
    private Pane pane;

    private Group pane3D;
    private PerspectiveCamera camera;
    private ObservableList<Shape3D> items;

    @FXML
    void createBox(ActionEvent event) {
        Box box = new Box(new Random().nextInt(200), new Random().nextInt(200), new Random().nextInt(200));
        box.setMaterial(new PhongMaterial(new Color(new Random().nextDouble(), 
                new Random().nextDouble(), new Random().nextDouble(), new Random().nextDouble())));
        box.setTranslateX(-100 + new Random().nextInt(200));
        box.setTranslateY(-100 + new Random().nextInt(200));
        box.setTranslateZ(new Random().nextInt(200));
        items.add(box);
    }

    @FXML
    void createSphere(ActionEvent event) {
        Sphere sphere = new Sphere(new Random().nextInt(100));
        sphere.setMaterial(new PhongMaterial(new Color(new Random().nextDouble(), 
                new Random().nextDouble(), new Random().nextDouble(), new Random().nextDouble())));
        sphere.setTranslateX(-100 + new Random().nextInt(200));
        sphere.setTranslateY(-100 + new Random().nextInt(200));
        sphere.setTranslateZ(new Random().nextInt(200));
        items.add(sphere);
    }

    public void initialize() {
        camera = new PerspectiveCamera(true);
        camera.setNearClip(0.1);
        camera.setFarClip(10000);
        camera.setTranslateZ(-1000);
        pane3D = new Group(camera);
        SubScene subScene = new SubScene(pane3D, 400, 400, true, SceneAntialiasing.BALANCED);
        subScene.setFill(Color.ROSYBROWN);
        subScene.setCamera(camera);
        pane.getChildren().add(subScene);

        items = FXCollections.observableArrayList();

        items.addListener((ListChangeListener.Change<? extends Shape3D> c) -> {
            while (c.next()) {
                if (c.wasAdded()) {
                    c.getAddedSubList().forEach(i -> pane3D.getChildren().add(i));
                }
            }
        });

    }    

    @FXML
    void rotateAll(ActionEvent event) {
        items.forEach(s -> {
            s.setRotate(new Random().nextInt(360));
            s.setRotationAxis(new Point3D(-100 + new Random().nextInt(200), 
                    -100 + new Random().nextInt(200), new Random().nextInt(200)));
        });
    }

    @FXML
    void translateAll(ActionEvent event) {
        items.forEach(s -> {
            s.setTranslateX(-100 + new Random().nextInt(200));
            s.setTranslateY(-100 + new Random().nextInt(200));
            s.setTranslateZ(new Random().nextInt(200));
        });

    }
}