O'Reilly logo

Learning Java Through Games by Lubomir Stanchev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10
Nested Classes and Event Handling
10.1 The Timer Class ........................................................................ 213
10.2 Nested Classes .......................................................................... 215
10.2.1 Static Nested Classes ........................................................... 215
10.2.2 Inner Classes .................................................................... 216
10.2.3 Local Classes .................................................................... 218
10.3 Event Listeners ......................................................................... 220
10.3.1 Key Listeners ................................................................... 220
10.3.2 Mouse Listeners ................................................................. 223
10.3.3 Menu Listeners .................................................................. 228
10.4 Multicasting ............................................................................. 231
10.5 Summary ................................................................................ 233
10.6 Syntax .................................................................................. 234
10.7 Important Points ....................................................................... 235
10.8 Exercises ................................................................................ 236
10.9 Lab ...................................................................................... 237
10.10 Project .................................................................................. 237
The chapter teaches how to write Java code that responds to events. We will show how to
write code that responds to keyboard strokes, mouse movement, window closing, and so on.
Since Java is an object-oriented language, we cannot directly tell Java to execute a method
when an event occurs. Instead, we need to create an object that supports the method that
needs to be executed when the event occurs and pass this object as a parameter. In other
words, event handling is supported in Java by creating new classes and objects that belong
to them. However, creating too many new classes can be burdensome. To simplify the code,
Java allows us to create a class within a class and even within a method. The chapter
describes the different ways of creating such nested classes in the context of event handling.
10.1 The Timer Class
We will start this chapter with the Typing Game. The objective of the game is to type
characters faster than they appear on the screen. We will create an ArrayList of characters,
where the ArrayList will be initially empty. Every 200 milliseconds (or 1/5 of a second) we
will add a new character to the list and display it. If the user types in a character that is in
the list, then we will remove the character from the list and redisplay the list. The player
wins if they are able to stay alive for more than 30 seconds. They lose if the list becomes
longer than 10 characters. We will allow duplicate characters in the list.
When implementing the game, we want to tell Java to execute a method that adds a
character to the list every 200 milliseconds. However, Java does not allow us to send a
method name as a parameter to another method. Instead, we need to create a new class
that implements the method and pass as input an instance of that class. For example, Java
contains a Timer class that can be used to call a method periodically. Consider the following
program.
213

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required