亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? ch9.htm

?? Java游戲開發
?? HTM
?? 第 1 頁 / 共 2 頁
字號:
<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&amp;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>
&nbsp;&nbsp;switch (evt.id) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;...<BR>
&nbsp;&nbsp;case Event.KEY_PRESS:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;return keyDown(evt, evt.key);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;...<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;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>
&nbsp;&nbsp;switch (key) {<BR>
&nbsp;&nbsp;case Event.LEFT:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// left arrow key pressed<BR>
&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
&nbsp;&nbsp;case Event.RIGHT:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// right arrow key pressed<BR>
&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
&nbsp;&nbsp;case Event.UP:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// up arrow key pressed<BR>
&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
&nbsp;&nbsp;case Event.DOWN:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;// down arrow key pressed<BR>
&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>
&nbsp;&nbsp;}<BR>
&nbsp;&nbsp;return true;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
91麻豆国产精品久久| 久久天堂av综合合色蜜桃网| 成人性色生活片| 欧美亚洲一区二区在线| 欧美电影免费观看高清完整版在线| 久久久久久97三级| 日韩精品每日更新| 色偷偷一区二区三区| 国产色综合一区| 国模少妇一区二区三区| 欧美日韩国产色站一区二区三区| 国产精品欧美一级免费| 精品一区二区三区免费观看| 欧美日韩视频在线第一区| 国产精品久久久久久久久快鸭| 国产主播一区二区三区| 欧美一区二区三区在| 婷婷一区二区三区| 欧美午夜精品一区二区蜜桃| 亚洲欧美一区二区三区久本道91| 国产精品资源网站| 久久综合久久综合久久综合| 免费成人在线视频观看| 欧美男人的天堂一二区| 亚洲精品高清在线观看| 91网址在线看| 亚洲欧美视频在线观看| 91美女片黄在线观看| 国产欧美久久久精品影院| 国产精品一品二品| 国产欧美一区二区精品久导航| 国产一区欧美二区| 久久综合九色综合97婷婷女人| 久久国产精品72免费观看| 日韩欧美一区二区视频| 久久电影网站中文字幕| 欧美一区二区成人| 卡一卡二国产精品 | 亚洲午夜免费视频| 91久久精品网| 亚洲成精国产精品女| 欧美日韩国产中文| 视频一区二区不卡| 欧美大片拔萝卜| 国产不卡在线视频| 亚洲日穴在线视频| 欧美乱熟臀69xxxxxx| 免费在线观看视频一区| 久久综合视频网| 成人福利电影精品一区二区在线观看| 国产色综合一区| 91蜜桃在线观看| 午夜欧美电影在线观看| 日韩欧美在线不卡| 成人国产精品免费观看| 一区二区三区在线视频观看58 | 色综合久久综合网97色综合| 亚洲一区二区三区四区在线| 91精选在线观看| 国产成人午夜精品5599| 亚洲日本电影在线| 欧美电影在哪看比较好| 国产精品911| 亚洲一区二区三区小说| 精品成人一区二区三区| 91麻豆精品在线观看| 日韩av电影天堂| 中文字幕久久午夜不卡| 欧美精品在线视频| 国产精品1024久久| 亚洲国产精品欧美一二99| 精品成人私密视频| 欧美在线观看禁18| 国产一区二区三区在线观看精品| 亚洲欧洲综合另类| 久久久一区二区三区| 欧美日韩中文国产| av在线不卡观看免费观看| 日韩激情视频网站| 亚洲欧美日韩在线不卡| 久久夜色精品国产噜噜av| 欧美性欧美巨大黑白大战| 精品一区二区三区在线观看国产| 亚洲精品乱码久久久久久| www激情久久| 欧美猛男男办公室激情| 9l国产精品久久久久麻豆| 九九久久精品视频| 亚洲一二三四区不卡| 国产精品久久久久久一区二区三区| 欧美一二三区在线观看| 日本乱人伦aⅴ精品| www.亚洲国产| 国产一区激情在线| 日韩中文字幕区一区有砖一区 | 日韩中文字幕区一区有砖一区 | 国产精品丝袜91| 日韩美女在线视频| 欧美喷潮久久久xxxxx| 色婷婷综合久久久久中文一区二区| 国产福利不卡视频| 国产黄色精品视频| 国产麻豆精品久久一二三| 精品一区二区三区免费视频| 日韩黄色一级片| 男人操女人的视频在线观看欧美| 午夜精品成人在线| 丝袜美腿高跟呻吟高潮一区| 亚洲国产欧美在线人成| 一二三四社区欧美黄| 亚洲综合免费观看高清完整版在线| 国产精品成人网| 亚洲一区二区三区国产| 成人国产精品免费观看动漫| 韩国毛片一区二区三区| 精品一区二区三区视频| 精品夜夜嗨av一区二区三区| 免费av成人在线| 裸体健美xxxx欧美裸体表演| 久久激情五月激情| 国产精品一区一区| 国产91色综合久久免费分享| 成人久久久精品乱码一区二区三区| 东方aⅴ免费观看久久av| 国产成人日日夜夜| 成人av电影免费在线播放| 处破女av一区二区| 色狠狠av一区二区三区| 欧美日韩在线三级| 欧美一级生活片| 精品盗摄一区二区三区| 成人av电影免费观看| heyzo一本久久综合| 91麻豆国产香蕉久久精品| 欧美少妇bbb| 欧美大度的电影原声| 国产精品无遮挡| 亚洲午夜精品一区二区三区他趣| 奇米色777欧美一区二区| 美腿丝袜在线亚洲一区 | 国产自产2019最新不卡| 成人国产精品免费观看视频| 欧美视频一区二区在线观看| 91精品国模一区二区三区| 欧美成人激情免费网| 国产精品无遮挡| 视频在线观看91| 国产一区二区不卡| 91福利在线观看| 欧美精品一区二区三区在线播放| 亚洲欧洲日韩女同| 日韩精品国产欧美| 成人精品视频网站| 欧美肥妇bbw| 亚洲欧美日韩久久| 蜜桃91丨九色丨蝌蚪91桃色| 99久久99久久免费精品蜜臀| 91精品午夜视频| 成人欧美一区二区三区在线播放| 琪琪一区二区三区| 一本色道亚洲精品aⅴ| 亚洲精品一区二区三区四区高清 | 91久久精品日日躁夜夜躁欧美| 日韩欧美一级二级| 艳妇臀荡乳欲伦亚洲一区| 精品午夜一区二区三区在线观看 | 日韩国产高清在线| 99久久国产综合色|国产精品| 日韩视频永久免费| 亚洲一二三区视频在线观看| 国产乱码精品一品二品| 91精品黄色片免费大全| 一个色妞综合视频在线观看| 国产成人av影院| 日韩三级免费观看| 天天综合色天天综合| 91浏览器打开| 中文一区二区在线观看| 国产资源精品在线观看| 日韩三级视频在线看| 亚洲6080在线| 欧美色综合久久| 亚洲一卡二卡三卡四卡无卡久久 | 高清不卡一区二区| 精品福利在线导航| 蜜臀a∨国产成人精品| 欧美日韩三级在线| 一区二区三区四区蜜桃| 99精品久久免费看蜜臀剧情介绍| 国产三级精品视频| 国产精品乡下勾搭老头1| 日韩一区二区三区三四区视频在线观看 | 亚洲视频在线一区二区| 懂色av一区二区三区免费观看| 久久色.com| 成人丝袜高跟foot| 国产精品天干天干在观线| 懂色av中文一区二区三区| 国产精品美女一区二区三区 | 中文字幕视频一区|