Bashan
Bashan

Reputation: 11

How do I get the clicked Box in JavaFX 3d

I've got a problem with my JavaFX 3D application. My application creates a grid consisting of different boxes. The data used for the boxes are from the harry potter books, which are transferred to nodes and then to boxes. Every box symbolizes one character. Now I want to add an event, that when a box is clicked application a window should open to show, which character it is and some other data.

"for (Book book : books) {

        Node bookNode = new Node();
        bookNode.setType(NodeType.BOOK);
        bookNode.setName(book.getName());
        bookNode.setMetric(getMetricFromPrimaryInt(book.getPages(),overallPages));

        for (Chapter chapter : book.chapters) {

            Node chapterNode = new Node();
            chapterNode.setType(NodeType.CHAPTER);
            chapterNode.setName(chapter.getName());
            chapterNode.setMetric(getMetricFromPrimaryInt(chapter.getPages(), book.getPages()));

            for (String character : chapter.characters) {

                Node characterNode = new Node();
                characterNode.setType(NodeType.CHARACTER);
                characterNode.setName(character);
                characterNode.setMetric(getMetricFromPrimaryInt(1, chapter.characters.length));

                //Das neue child node dem parent hinzufügen
                chapterNode.extendChildren(characterNode);

            }

            //Das neue child node dem parent hinzufügen
            bookNode.extendChildren(chapterNode);

        }"

. . . "for (Node chars: chapt.getChildren()) { double height = rnd.nextDouble() * 500;

                Box box = new Box(chars.getHeight(), height, chars.getWidth());

                // color
                PhongMaterial mat = new PhongMaterial();
                mat.setDiffuseColor(randomColor());
                box.setMaterial(mat);

                // location
                box.setLayoutY(-height * 0.5);
                box.setTranslateX(xStart-(chars.getHeight()/2));
                box.setTranslateZ(yStart-(chars.getWidth()/2));

                grid.getChildren().addAll(box);
                Boxliste.add(box);

}"

My problem now is: How do I differentiate which box got clicked, because I need the box to get the characterdata and then display it. Hope it's clear what I mean.

Upvotes: 1

Views: 116

Answers (1)

Giovanni Contreras
Giovanni Contreras

Reputation: 2579

Casting Intersected Node with mouse event

pickresult javafx

In this approach getIntersectedNode() method from PickResult class will return a Node object . That object is casted to CustomSphere which extends Sphere and , like any other node in javafx , extends Node class .

This is a single javafx app you can try .

App.java

public class App extends Application {
    
    @Override
    public void start(Stage stage) {
        Label label = new Label("name");
        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setTranslateZ(-10);
        
        Group group3d = new Group(camera, new CustomSphere("violet sphere ", Color.BLUEVIOLET, 0.8, 0),
                new CustomSphere("coral sphere ", Color.CORAL, 0.7, 2),
                new CustomSphere("yellow-green sphere ", Color.YELLOWGREEN, 0.9, -2));
        
        SubScene subScene = new SubScene(group3d, 480, 320, true, SceneAntialiasing.DISABLED);
        subScene.setOnMouseClicked(t -> {
            PickResult result = t.getPickResult();
            Node intersectedNode = result.getIntersectedNode();
            
            if (intersectedNode instanceof CustomSphere) {
                CustomSphere clickedSphere = (CustomSphere) intersectedNode;
                label.setText(clickedSphere.getName());
                
            }
            
        });
        subScene.setCamera(camera);
        StackPane stackPane = new StackPane(subScene, label);
        StackPane.setAlignment(label, Pos.TOP_CENTER);
        
        Scene scene = new Scene(stackPane, 480, 320, true, SceneAntialiasing.BALANCED);
        stage.setTitle("pickresult");
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch();
    }
    
    class CustomSphere extends Sphere {
        
        private String name;
        PhongMaterial material = new PhongMaterial();

        public CustomSphere(String name, Color color, double d, double x) {
            super(d);
            this.name = name;
            material.setDiffuseColor(color);
            this.setMaterial(material);
            this.setTranslateX(x);
        }
        
        public String getName() {
            return name;
        }
        
    }
    
}

Upvotes: 0

Related Questions