Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 17 ■ i3D Game Square SeleCtion: uSinG the piCkreSult ClaSS with 3D moDelS

Double-click your setOnMouseClicked(EventHandler<? super MouseEvent> value) (void) option,
shown in bright blue in Figure 17-2, and add the (event->{}); empty lambda expression inside the
.setOnMouseClicked() method call parameter area to create an empty event processing infrastructure,
which will yield zero errors in NetBeans 9. As I have said before in this book, when you write code, make sure
it is error-free in your IDE at all times!
Now you can start configuring the onMouseClicked() event handling, which as you can see uses a
simplified lambda expression that was introduced in Java 8. All a lambda (as it’s called for short) needs
is the event name and an arrow, and the Java compiler will figure out what type of event-handling object
(EventHandler) to use and what type of Event object (MouseEvent) will need to be processed. Your
logic goes inside of your curly braces, and you can focus on what your event-processing logic is going
to do, which is to declare a Node object named picked and load it with the result of a .getPickResult().
getIntersectedNode() method chain. Make sure to use Alt+Enter when you get a wavy red error underline,
under the Node picked (initial) portion of your Java statement, and select the “import javafx.scene.Node”
option from the pop-up helper dialog to instruct NetBeans 9 to write the Node class import statement for
you. If you like, you can type in the equals (=) sign and the event and hit the period; the NetBeans pop-
up helper will let you select the .getPickResult() method. Double-click that to insert it and then use the
period again to bring up the pop-up helper. This time select the .getIntersectedNode() method call. Add a
semicolon to finalize the Java statement. The Java statements for your MouseEvent handling should look like
the following and are shown at the bottom of Figure 17-3:


private void createSceneProcessing() {
scene = new Scene(root, 1280, 640, true, SceneAntialiasing.BALANCED);
scene.setFill(Color.BLACK);
scene.setCamera(camera);
scene.setOnMouseClicked(event->{
Node picked = event.getPickResult().getIntersectedNode();
});
}


Figure 17-2. Cut and paste the Scene object code into the new method and call .setOnMouseClicked() off the
scene object

Free download pdf