28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9 ■ Controlling Your Action Figure: Implementing <strong>Java</strong> Event Handlers and Using Lambda Expressions<br />

Also, notice that the code-collapsing plus and minus icons in the left margin of NetBeans are also gone! This is<br />

because a lambda expression is a basic <strong>Java</strong> code statement, and not a construct, such as a method or inner class as<br />

it was before you converted it to a lambda expression. If you look at Figure 9-12, your event handling code is looking<br />

very clean and well-structured, and yet, in just over a dozen lines of code, it is actually doing quite a lot for your game.<br />

Next, let’s take a look at your import statements code block (especially if you have your import code block<br />

collapsed), since you had NetBeans 8 create lambda expressions for you. Let’s see if you have any unneeded imports!<br />

Optimizing Import Statements: Remove the EventHandler Class Import Statement<br />

Click the + plus icon at the top-left of NetBeans and expand your import statement section and see if you now<br />

have an unused import javafx.event.EventHandler statement with wavy yellow underline warning highlighting<br />

underneath it. I have this, as you can see in Figure 9-13, and when I mouse-over it I get the “Unused Import”<br />

warning message. I used the Alt-Enter work process to bring up the solutions options helper dialog, and sure<br />

enough, there was a “Remove Import Statement” option. So NetBeans will unwrite code for you as well as writing it<br />

for you! Pretty amazing feature!<br />

Figure 9-13. Mouse-over the import EventHandler warning highlight, and display the pop-up “Unused Import” warning<br />

Next, let’s add the traditional ASDW game play key event handling, to give our users an option to use those keys<br />

or to use two hands for their game play! This will show you how to add alternate key mapping support to your existing<br />

event handling code, using only a few more lines of code in the highly efficient switch-case statement.<br />

www.it-ebooks.info<br />

203

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!