📄 ch9.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 9 -- Handling User Input with Java</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 9</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Handling User Input with Java</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#EventDrivenProgramming" >Event-Driven Programming</A>
<LI><A HREF="#JavaInputEvent" >Java Input Event</A>
<LI><A HREF="#awtEventHandling" >awt Event Handling</A>
<LI><A HREF="#KeyboardEvents" >Keyboard Events</A>
<LI><A HREF="#MouseEvents" >Mouse Events</A>
<LI><A HREF="#SampleAppletFlyingSaucer" >Sample Applet: Flying Saucer</A>
<LI><A HREF="#Summary" >Summary </A>
<LI><A HREF="#QA" >Q&A</A>
<LI><A HREF="#Workshop" >Workshop</A>
<UL>
<LI><A HREF="#Quiz" >Quiz</A>
<LI><A HREF="#Exercises" >Exercises</A>
</UL>
</UL>
<HR>
<P>
Now that you have an idea about the role that user input plays
in games, you're ready to learn the specifics of how user input
works in Java. This lesson is devoted to the handling of user
input in Java, including the supported input devices and the methods
used to trap input events. You also learn about the event-driven
structure of Java event handling and how it applies to input events.
<P>
This lesson clearly establishes the fact that Java user input
handling is both powerful and simple. A handful of methods is
really all it takes to deal with user input in a Java game. You
get to see this firsthand by building a Java applet with keyboard
and mouse input support. Let's get started.
<H2><A NAME="EventDrivenProgramming"><B><FONT SIZE=5 COLOR=#FF0000>Event-Driven
Programming</FONT></B></A></H2>
<P>
Before getting into the details of how user input is handled in
Java, it's important that you understand how event-driven programming
works. This is important because user input in Java is heavily
based on the event-driven architecture that makes up the heart
of Java. In Java, an <I>event</I> is defined quite literally as
something that happens that you might want to know about. For
example, when a Java component gains the input focus, an event
occurs because it might be important for your applet to know about
the focus change.
<P>
An <I>event</I> is something that happens that you might want
to know about and possibly react to.
<P>
In the event-driven world of Java, the flow of your program follows
events external to your applet, as opposed to following an internally
linear program flow. This is an important point because it means
that a Java applet is in a constant state of responding to events.
The most visible events are things such as mouse clicks and key
presses, which are known as <I>input events</I>. You provide methods
that respond to these events, which are called <I>event handlers</I>.
<P>
An <I>input event</I> is an event generated by user manipulation
of an input device such as the mouse or keyboard.
<P>
<I>Event handlers</I> are special methods that are used to respond
or react to events.
<P>
Because of the inherent graphical nature of Java applets, it will
eventually become obvious to you why the event-driven programming
model is not only more convenient, but downright necessary. With
the potential of having multiple applets on a single Web page,
along with on-the-fly system configuration changes and a multitude
of other things going on, a procedural programming model would
be much more difficult to manage. The event-based model provides
a more sound solution to the problems inherent in a system with
a graphical interface, such as Java.
<P>
All events in Java are processed within the awt windowing toolkit
package, and they are tightly linked to awt components. A component
is basically a generic abstraction for a Java window. You might
recall that Java applets are themselves a specific type of component,
which means that they inherit the same event-processing features
built into the <TT><FONT FACE="Courier">Component</FONT></TT>
superclass.
<P>
A <I>component</I> is a generic abstraction of a window in Java.
<H2><A NAME="JavaInputEvent"><B><FONT SIZE=5 COLOR=#FF0000>Java
Input Event</FONT></B></A></H2>
<P>
As you just learned, user input in Java is handled through an
event-driven architecture. When the user interacts with an input
device, it results in an input event being dispatched to the component
with the input focus. In most cases, this component is the applet
window. An input event is a special type of event that notifies
an applet that something has occurred on an input device. An example
of an input event is a movement of the mouse.
<P>
Input events are crucial in Java games because they provide a
means of handling user responses. Without being able to monitor
user responses, Java games wouldn't be too exciting. User response
handling is not only important for providing an interface to the
user for playing a game; it also establishes much of the feel
of a game. Simply altering the means by which you provide user
response support can dramatically alter the play of a game. This
is an important point, one that you'll deal with later in this
lesson when you develop the Flying Saucer sample applet.
<P>
Java user event responses come in two varieties, which correspond
to the input devices supported by Java. The two types of input
events supported by release 1.0 of Java are as follows:
<UL>
<LI>Keyboard events
<LI>Mouse events
</UL>
<P>
Keyboard events are events generated by a key press on the keyboard.
Any time the user presses a key, a keyboard event is generated
that can be trapped and handled by the applet with the input focus.
Actually, a key press generates two events: a key down event and
a key up event. You'll learn more about these two types soon.
<P>
Mouse events are generated by mouse clicks and movements. Every
mouse click and mouse movement generates a corresponding mouse
input event. Like key presses, mouse clicks actually come as a
series of events: a mouse click down event and a mouse click up
event. A mouse event is also specifically targeted at mouse dragging.
Mouse dragging occurs when the mouse is moved with the button
down. Applets that want to respond to mouse clicks and movement
simply have to process these events and take action accordingly.
You learn more about processing mouse events a little later in
this lesson.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
You might have noticed in the discussion of mouse events the mention of the mouse <I>button</I>, as opposed to the mouse <I>buttons</I>. This is intentional because Java only supports a single mouse button. This might seem limiting to users on some
platforms, such as Windows, but keep in mind that Java is designed to support as many platforms as possible. Considering the fact that some platforms (such as Macintosh) have mice with a single button, it makes sense for Java to support only a single
button.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<H2><A NAME="awtEventHandling"><B><FONT SIZE=5 COLOR=#FF0000>awt
Event Handling</FONT></B></A></H2>
<P>
Before getting into the specific event handlers for keyboard and
mouse events, let's look at how events are handled in a general
sense in Java. The Java awt provides the <TT><FONT FACE="Courier">Event</FONT></TT>
class for encapsulating all types of events that can occur within
the system. The <TT><FONT FACE="Courier">Event</FONT></TT> class
models a generic event and has constants defined within it to
represent specific events. The <TT><FONT FACE="Courier">Event</FONT></TT>
class is used primarily by the <TT><FONT FACE="Courier">handleEvent</FONT></TT>
method of <TT><FONT FACE="Courier">Component</FONT></TT>. The
<TT><FONT FACE="Courier">handleEvent</FONT></TT> method is the
default event handler for all events, and is defined as follows:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean handleEvent(Event evt)</FONT></TT>
</BLOCKQUOTE>
<P>
Notice that <TT><FONT FACE="Courier">handleEvent</FONT></TT> takes
an <TT><FONT FACE="Courier">Event</FONT></TT> object as its only
parameter. <TT><FONT FACE="Courier">handleEvent</FONT></TT> uses
this <TT><FONT FACE="Courier">Event</FONT></TT> object to determine
what type of event has occurred. It then calls a more specific
event handler method to deal with the specific event. For example,
if a key is pressed, the <TT><FONT FACE="Courier">Event</FONT></TT>
object's <TT><FONT FACE="Courier">id</FONT></TT> member variable
is set to <TT><FONT FACE="Courier">KEY_PRESS</FONT></TT>, which
is a constant defining the key press event. <TT><FONT FACE="Courier">handleEvent</FONT></TT>
checks the value of <TT><FONT FACE="Courier">id</FONT></TT> and,
upon finding it equal to <TT><FONT FACE="Courier">KEY_PRESS</FONT></TT>,
calls the <TT><FONT FACE="Courier">keyDown</FONT></TT> handler
method. Listing 9.1 shows the key press handling portion of the
<TT><FONT FACE="Courier">handleEvent</FONT></TT> method in the
1.0 release of Java.
<HR>
<BLOCKQUOTE>
<B>Listing 9.1. The key press portion of the </B><TT><B><FONT FACE="Courier">handleEvent</FONT></B></TT><B>
method.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean handleEvent(Event evt)
{<BR>
switch (evt.id) {<BR>
...<BR>
case Event.KEY_PRESS:<BR>
return keyDown(evt, evt.key);<BR>
...<BR>
}<BR>
return false;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The handling of other system events is very similar to that of
the <TT><FONT FACE="Courier">KEY_PRESS</FONT></TT> event. You
could easily override <TT><FONT FACE="Courier">handleEvent</FONT></TT>
to provide custom routing of event handlers, but it is rarely
necessary. Although you might not ever need to intervene with
the default event handling provided by <TT><FONT FACE="Courier">handleEvent</FONT></TT>,
it is nevertheless important to understand how it works.
<H2><A NAME="KeyboardEvents"><B><FONT SIZE=5 COLOR=#FF0000>Keyboard
Events</FONT></B></A><BR>
</H2>
<P>
Java keyboard events are generated when the user presses or releases
a key. Two standard keyboard event handler methods are supported
by the <TT><FONT FACE="Courier">Component</FONT></TT> class: <TT><FONT FACE="Courier">keyDown</FONT></TT>
and <TT><FONT FACE="Courier">keyUp</FONT></TT>. These two methods
are defined as follows:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean keyDown(Event evt, int
key)<BR>
public boolean keyUp(Event evt, int key)</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">keyDown</FONT></TT> method is called
in response to the user pressing a key, and the <TT><FONT FACE="Courier">keyUp</FONT></TT>
method is called in response to the user releasing a key. Both
methods are passed an <TT><FONT FACE="Courier">Event</FONT></TT>
object and an integer key value parameter. The key value parameter,
<TT><FONT FACE="Courier">key</FONT></TT>, specifies which key
was pressed or released. The <TT><FONT FACE="Courier">Event</FONT></TT>
object parameter contains extra information relating to the keyboard
event, such as whether the Shift key was held down when the key
was pressed.
<P>
The <TT><FONT FACE="Courier">Event</FONT></TT> object contains
constants representing the different keys that can be specified
in the <TT><FONT FACE="Courier">key</FONT></TT> parameter. Table
9.1 shows a list of the more useful key constants.<BR>
<BLOCKQUOTE>
<CENTER><B>Table 9.1. Useful key constants for games.</B></CENTER>
</BLOCKQUOTE>
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><I>Constant</I></TD><TD WIDTH=103><I>Key</I>
</TD></TR>
<TR><TD WIDTH=78><TT><FONT FACE="Courier">UP</FONT></TT></TD>
<TD WIDTH=103>Up arrow</TD></TR>
<TR><TD WIDTH=78><TT><FONT FACE="Courier">DOWN</FONT></TT></TD>
<TD WIDTH=103>Down arrow</TD></TR>
<TR><TD WIDTH=78><TT><FONT FACE="Courier">LEFT</FONT></TT></TD>
<TD WIDTH=103>Left arrow</TD></TR>
<TR><TD WIDTH=78><TT><FONT FACE="Courier">RIGHT</FONT></TT></TD>
<TD WIDTH=103>Right arrow</TD></TR>
<TR><TD WIDTH=78><TT><FONT FACE="Courier">HOME</FONT></TT></TD>
<TD WIDTH=103>Home</TD></TR>
<TR><TD WIDTH=78><TT><FONT FACE="Courier">END</FONT></TT></TD>
<TD WIDTH=103>End</TD></TR>
<TR><TD WIDTH=78><TT><FONT FACE="Courier">PGUP</FONT></TT></TD>
<TD WIDTH=103>Page Up</TD></TR>
<TR><TD WIDTH=78><TT><FONT FACE="Courier">PGDN</FONT></TT></TD>
<TD WIDTH=103>Page Down</TD></TR>
</TABLE></CENTER>
<P>
<P>
To check whether the key pressed or released is one of these keys,
you override <TT><FONT FACE="Courier">keyDown</FONT></TT> or <TT><FONT FACE="Courier">keyUp</FONT></TT>
and compare the value of <TT><FONT FACE="Courier">key</FONT></TT>
to one of the constants. Listing 9.2 contains an example of overriding
<TT><FONT FACE="Courier">keyDown</FONT></TT> to check for the
user pressing one of the arrow keys.
<HR>
<BLOCKQUOTE>
<B>Listing 9.2. Handling arrow key presses in the </B><TT><B><FONT FACE="Courier">keyDown</FONT></B></TT><B>
method.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public boolean keyDown(Event evt, int
key) {<BR>
switch (key) {<BR>
case Event.LEFT:<BR>
// left arrow key pressed<BR>
break;<BR>
case Event.RIGHT:<BR>
// right arrow key pressed<BR>
break;<BR>
case Event.UP:<BR>
// up arrow key pressed<BR>
break;<BR>
case Event.DOWN:<BR>
// down arrow key pressed<BR>
break;<BR>
}<BR>
return true;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -