Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 21 ■ Questions and answers: Finishing the setup Methods and digital audio


The next task is to add the four answer Button element a1Button through a4Button declarations at the
top of the class and instantiate and configure these Button objects inside of the createQAnodes() method. I
sized them at 350 units wide and 100 units tall using setMaxSize() and placed them at -180, -60, 60, and 180
using setTranslateY(). I named them Answer One through Answer Four using the setText() method for UI
design testing purposes. The Java 9 code needed to implement these four Button UI elements is shown in
Figures 21-10 and 21-11 and looks like this:


Button gameButton, ..., a1Button, a2Button, a3Button, a4Button; // at top of JavaFXGame class
a1Button = new Button(); // statements in createQAnodes() method
a1Button.setText("Answer One");
a1Button.setMaxSize(350, 100);
a1Button.setTranslateY(-180);
a2Button = new Button();
a2Button.setText("Answer Two");
a2Button.setMaxSize(350, 100);
a2Button.setTranslateY(-60);
a3Button = new Button();
a3Button.setText("Answer Three");
a3Button.setMaxSize(350, 100);
a3Button.setTranslateY( 60 );
a4Button = new Button();
a4Button.setText("Answer Four");
a4Button.setMaxSize(350, 100);
a4Button.setTranslateY( 180 );
... // Remember to add Button Nodes to SceneGraph
qaLayout.getChildren().addAll(a1Button, a2Button, a3Button, a4Button); // addNodesToSceneGraph()

Free download pdf