⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 123-127.html

📁 java game programming e-book
💻 HTML
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:Adding Interactivity</TITLE>
<!-- HEADER --><STYLE type="text/css">  <!-- A:hover  { 	color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) {         var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></script><SCRIPT><!--function popUp(url) {        var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) {  /* get the query value */  var i = escape(fm.query.value);  if (i == "") {      alert('Please enter a search word or phrase');      return false;  }                  /* query is blank, dont run the .jsp file */  else return true;  /* execute the .jsp file */}//--></script></HEAD><BODY> 
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
    <font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
    <br>
    <font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
    <br>
    Sams,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</td>
</tr>
</table>
<P>

<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=4//-->
<!--PAGES=123-127//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="117-123.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="127-132.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Each of these methods is called with the actual Event object, and the screen position of the mouse (relative to the upper-left corner of the applet&#146;s bounding rectangle). This way, you can customize behavior depending on where the mouse is or how much it moves, as you&#146;ll soon see.
</P>
<P>Let&#146;s start with a simple example. If you want to intercept a mouseDown event, you&#146;ll need to override the default mouseDown() by putting the following in the applet definition:</P>
<!-- CODE SNIP //-->
<PRE>
public boolean mouseDown(Event e,int x,int y) {
  // Your mouseDown handler here
  ...
  return true;                   // or return false
}
</PRE>
<!-- END CODE SNIP //-->
<P>Listing 4-1 shows an applet that responds to mouse events by echoing the event that&#146;s occurred. Try it now. It really illustrates how mouse events work!
</P>
<P><B>Listing 4-1</B> MouseTest applet</P>
<!-- CODE //-->
<PRE>
import java.applet.*;
import java.awt.*;

/////////////////////////////////////////////////////////////////
public class MouseTest extends Applet {

  public boolean mouseDown(Event e,int x,int y) {
    System.out.println("mouseDown at (" &#43; x &#43; "," &#43; y &#43; ")" );
    return true;
  }

  public boolean mouseUp(Event e,int x,int y) {
    System.out.println("mouseUp at (" &#43; x &#43; "," &#43; y &#43; ")" );
    return true;
  }

  public boolean mouseMove(Event e,int x,int y) {
    System.out.println("mouseMove at (" &#43; x &#43; "," &#43; y &#43; ")" );
    return true;
  }

  public boolean mouseDrag(Event e,int x,int y) {
    System.out.println("mouseDrag at (" &#43; x &#43; "," &#43; y &#43; ")" );
    return true;
  }

  public boolean mouseEnter(Event e,int x,int y) {
    System.out.println("mouseEnter at (" &#43; x &#43; "," &#43; y &#43; ")" );
    return true;
  }

  public boolean mouseExit(Event e,int x,int y) {
    System.out.println("mouseExit at (" &#43; x &#43; "," &#43; y &#43; ")" );
    return true;
  }
}
</PRE>
<!-- END CODE //-->
<P>Two points should be made here:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;First, each event handler returns a boolean. When the handler returns <I>true</I>, this tells the AWT that the Event has been handled and doesn&#146;t need to be passed to another AWT object. If the handler returns <I>false</I>, the Event is propagated by the AWT. You&#146;ll understand the reason for this when we discuss the AWT below.
<DD><B>&#149;</B>&nbsp;&nbsp;Second, you might be wondering what all the &#43; signs are doing. They are performing String concatenation, and the text delimited by double quotes are String objects. Strings are explained later in this chapter.
</DL>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Keyboard Events</FONT></H4>
<P>Keyboard events work in the same way as mouse events. For example, if you press a key, the keyDown() method inherited by the Applet class gets invoked. Unlike mouseDown, a keyDown event triggers again and again while the key is depressed, due to the repeating nature of the keyboard. keyUp happens when the key&#146;s released. Table 4-2 shows these keyboard events, and the event handlers that are called in response.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 4-2</B> Keyboard events
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="55%" ALIGN="LEFT">Event
<TH WIDTH="45%" ALIGN="LEFT">Method
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD>keyDown (key is pressed)
<TD>public boolean
<TR>
<TD>
<TD>keyDown(Event e, int key)
<TR>
<TD>keyUp (key is released)
<TD>public boolean
<TR>
<TD>
<TD>keyUp(Event e, int key)
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>You can examine the key passed into the keyUp() or keyDown() methods to take the appropriate action. For example, you might compare the variable <I>key</I> with character literals:</P>
<!-- CODE //-->
<PRE>
public boolean keyDown(Event e, int key) {
  if (key == 'S') {      // if S pressed
  ...                    // do something
  }
  else if (key == 'F') { // if F pressed
  ...                    // do something else
  }
  return true;
}
</PRE>
<!-- END CODE //-->
<P>To test for function keys, you&#146;ll need to use constants defined in the Event class. Let&#146;s examine this class and some of the functionality it provides.
</P>
<H3><A NAME="Heading6"></A><FONT COLOR="#000077">The Event Class</FONT></H3>
<P>The Event class is defined in the java.awt package, and it&#146;s used to wrap and pass events among the various components of the AWT. For example, the object that is passed into event handlers is an instance of the Event class. To decipher some Event objects, you need to use constants and methods defined within Event. Let&#146;s see how this is done.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Handling Function Keys</FONT></H4>
<P>Event defines static constants that represent the special keys you will find on your keyboard&#151;function keys, arrow keys, and the &lt;HOME&gt;, &lt;END&gt;, &lt;PAGE UP&gt;, and &lt;PAGE DOWN&gt; keys. You can use these constants in your key handlers. For example:
</P>
<!-- CODE //-->
<PRE>
public boolean keyDown(Event e, int key) {
  if (key == Event.UP) {    // if up arrow pressed
  ...                       // do something
  }
  else if (key == Event.F7) { // if F7 pressed
  ...                         // do something else
  }
  return true;
}
</PRE>
<!-- END CODE //-->
<P>Table 4-3 lists the static constants defined for keys in the Event class.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 4-3</B> Constants for function keys in the Event class
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="45%" ALIGN=LEFT>Keys
<TH WIDTH="55%" ALIGN=LEFT>Static constants defined in Event
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD>Arrow keys
<TD>UP, DOWN, LEFT, RIGHT
<TR>
<TD VALIGN="TOP">Function keys
<TD>F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12
<TR>
<TD>Movement keys
<TD>HOME, END, PGUP, PGDN
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Handling Modifier Keys</FONT></H4>
<P>The Event class provides a way of testing for the presence of modifier keys, such as &lt;CTRL&gt;, &lt;SHIFT&gt;, or &lt;META&gt;, during an event. For example, you can use the Event methods controlDown(), metaDown(), or shiftDown() to provide three types of mouse clicks:
</P>
<!-- CODE //-->
<PRE>
public boolean mouseDown(Event e,int x,int y) {
  if (e.shiftDown()) {
    // handle shift-click
    ...
  }
  else if (e.controlDown()) {
    // handle control-click
    ...
  }
  else if (e.metaDown()) {
    // handle meta-click
    ...
  }
  ...
  return true;            // or return false
}
</PRE>
<!-- END CODE //-->
<P>Another way of checking if a modifier key is pressed is to examine the <I>modifiers</I> instance variable of the Event object. You&#146;ll need to bitwise-AND the appropriate bitmask defined in Event with the <I>modifiers</I> variable. If the result is not equal to 0, the associated modifier key is down. Table 4-4 lists the modifier keys, along with their bitmasks defined in the Event class.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 4-4</B> Bitmasks for modifier keys defined in the Event class
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="45%" ALIGN="LEFT">Modifier Key
<TH WIDTH="55%" ALIGN="LEFT">Static bitmask defined in Event
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD>&lt;ALT&gt;
<TD>ALT_MASK
<TR>
<TD>&lt;CTRL&gt;
<TD>CTRL_MASK
<TR>
<TD>&lt;META&gt;
<TD>META_MASK
<TR>
<TD>&lt;SHIFT&gt;
<TD>SHIFT_MASK
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="117-123.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="127-132.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -