28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Figure 9-4. Add in the .setVisible(true) method calls for the splashScreenBackplate and splashScreenTextArea objects<br />

Now you will see that when you use your Run ➤ Project work process, that all of your Button UI controls will do<br />

what they are supposed to do, and will display either a white background for the game or information screens that<br />

have the InvinciBagel splash screen artwork behind them. Now we are ready to learn about <strong>Java</strong> lambda expressions.<br />

Lambda Expression: A Powerful New <strong>Java</strong> 8 Feature<br />

One of the major new features that was recently released in <strong>Java</strong> 8 during 2014 is the lambda expression. Using the<br />

lambda expression can make your code more compact, and allows you to turn a method into a simple <strong>Java</strong> statement<br />

using <strong>Java</strong> 8’s new lambda -> “arrow” operator. The lambda expression provides a <strong>Java</strong> code shortcut to structure one<br />

single method interface by instead using a lambda expression.<br />

The <strong>Java</strong> 8 lambda expression has the same features as a <strong>Java</strong> method structure, as it requires a list of parameters<br />

to be passed in, as well as the code “body” to be specified. The code that the lambda expression calls can be a single<br />

java statement, or a block of code, containing multiple <strong>Java</strong> programming statements. This statement (or statements)<br />

will be expressed utilizing the parameters passed into the lambda expression. The basic <strong>Java</strong> 8 syntax for the simple<br />

lambda expression should be written as follows:<br />

(the parameter list) -> (a <strong>Java</strong> expression)<br />

You can also create a complex lambda expression by using the curly braces that are used in <strong>Java</strong> to define an<br />

entire block of <strong>Java</strong> code statements in conjunction with the lambda expression. This would be done by using the<br />

following format:<br />

(the parameter list) -> { statement one; statement two; statement three; statement n; }<br />

www.it-ebooks.info<br />

191

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

Saved successfully!

Ooh no, something went wrong!