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

📄 127-132.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=127-132//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="123-127.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="132-136.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>For example, here&#146;s how you could test if a mouse click occurred with the &lt;SHIFT&gt; and &lt;ALT&gt; keys down:
</P>
<!-- CODE SNIP //-->
<PRE>
public boolean mouseDown(Event e,int x,int y) {
  if ((e.modifiers &#38; Event.ALT_MASK) != 0 &#38;&#38;
      (e.modifiers &#38; Event.SHIFT_MASK) != 0) {
     // handle shift-alt-click
    ...
}
</PRE>
<!-- END CODE SNIP //-->
<P>On a mouse with multiple buttons, depressing the right button is equivalent to a mouseDown event with the &lt;META&gt; key down, and clicking the middle button corresponds to a mouseDown event with the &lt;ALT&gt; key down. Thus, you can write a game that utilizes the input from a multiple button mouse, and works with a single button mouse as well.
</P>
<P>The Event class is a crucial part of Java&#146;s AWT. In the next section, you&#146;ll get the big picture of the AWT, and how the AWT passes Event objects around.</P>
<H3><A NAME="Heading9"></A><FONT COLOR="#000077">Event Handling in the AWT</FONT></H3>
<P>The java.awt package in the API provides GUI functionality&#151;windows and scrollbars, list boxes and menu bars&#151;so that users can interact with Java applications in an eye-pleasing way. In this section, we&#146;ll focus on how the AWT handles events. In Chapter 7, Creating Customizable Games with the AWT, you&#146;ll see how to create graphical user interfaces with the AWT.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Overview of Component classes</FONT></H4>
<P>The heart of the AWT derives from the abstract class Component. Figure 4-3 shows some of the classes that have Component as a superclass.
</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/04-03.jpg',561,380 )"><IMG SRC="images/04-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/04-03.jpg',561,380)"><FONT COLOR="#000077"><B>Figure 4-3</B></FONT></A>&nbsp;&nbsp;Classes derived from Component</P>
<P>The Component class defines many methods&#151;paint(), repaint(), update(), setBackground(); event handlers such as mouseUp() and keyDown(), and so on&#151;that the other AWT classes inherit or override. You&#146;ve used or overridden some of these methods before, since the Applet class, which is part of the java.applet package, also derives from Component.
</P>
<P>Right below Component is Container, which is another abstract class. A Container, as its name implies, can contain other Components. For example, an Applet could contain a Button object and a Checkbox object. The Applet is known as the <I>parentcontainer</I> of the button and checkbox. A Frame (another Container class) could contain this Applet and another Button object. Figure 4-4 depicts the containment hierarchy for the components we&#146;ve just described.</P>
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/04-04.jpg',467,399 )"><IMG SRC="images/04-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/04-04.jpg',467,399)"><FONT COLOR="#000077"><B>Figure 4-4</B></FONT></A>&nbsp;&nbsp;Example of a containment hierarchy</P>
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">How the AWT Handles Events</FONT></H4>
<P>When an event such as a mouseDown occurs in a Container, there might be several AWT objects that could possibly handle the event. For example, a click that occurs in an applet might be of use to the window that contains it; the applet&#146;s event handler needs to pass the mouseDown event to the event handler for the window. Java uses the following convention to achieve this. When the event handler of a Component, such as an Applet, returns <I>false</I>, the event is passed to the handler in the parent Container. A return value of <I>true</I> indicates that the event should not be propagated. Thus, events are passed from the event handler of the &#147;innermost&#148; component (the component that triggered the event), up the containment hierarchy, to the handler of the &#147;outermost&#148; component (the root of the containment hierarchy). Figure 4-5 illustrates how an event is passed from component to component.</P>
<P><A NAME="Fig5"></A><A HREF="javascript:displayWindow('images/04-05.jpg',561,382 )"><IMG SRC="images/04-05t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/04-05.jpg',561,382)"><FONT COLOR="#000077"><B>Figure 4-5</B></FONT></A>&nbsp;&nbsp;Event propagation</P>
<P>All components, including Applet, use the method handleEvent() as the default event handler. handleEvent(), in turn, dispatches events to the &#147;helper&#148; handlers you&#146;ve seen above, such as keyDown(), mouseDrag(), and mouseMove(). By overriding handleEvent(), you can test for GUI events that don&#146;t have helper methods. Although this isn&#146;t necessary until Chapter 7, Creating Customizable Games with the AWT, which describes GUIs, let&#146;s see how it&#146;s done.
</P>
<P>The handleEvent() method is called with an argument of class Event. The Event instance variable <I>id</I> stores the type of event that has occurred; you&#146;ll compare <I>id</I> with static constants defined in the Event class. (A complete list of these constants appears in Chapter 7.) The Event instance variables <I>x</I> and <I>y</I> store the coordinates of the event.</P>
<P>Here&#146;s an example of using the handleEvent() method:</P>
<!-- CODE //-->
<PRE>
// this code appears in a subclass of Component
public boolean handleEvent(Event e) {
  switch (e.id) {
  case Event.MOUSE_UP:
    // handle mouse up. Now, mouseUp() is NOT called
    if (e.x == e.y)      // compare x and y coords of event
      ...
    break;
  case Event.WINDOW_DEICONIFY:
    // no helper method for this event, so this is
    //   the only way to check for it
    break;
  case Event.WINDOW_MOVED:
    // no helper method for this event, so this is
    //   the only way to check for it
    ...
}
</PRE>
<!-- END CODE //-->
<P>When you override handleEvent(), helper methods such as mouseUp() and keyDown() aren&#146;t called, unless you pass events to these methods explicitly. Another way of resuming the default event handling, even when you&#146;ve overridden handleEvent(), is to call the superclass handleEvent() at some point in the overriding method:
</P>
<!-- CODE SNIP //-->
<PRE>
super.handleEvent(e);
</PRE>
<!-- END CODE SNIP //-->
<P>Now that you understand how Java handles events, you&#146;re ready to create interactive applets. First, we&#146;ll take a brief digression and show how to display text, which we&#146;ll need for applets later on in this chapter.
</P>
<H3><A NAME="Heading12"></A><FONT COLOR="#000077">Displaying Text</FONT></H3>
<P>An easy way to show text is by using the static method System.out.println(), which is in the class java.lang.System. This method is overloaded, so it works with many different types of arguments, such as numeric types or Strings. If you use it, the output appears on the standard output stream, or on the &#147;Java console&#148; if you&#146;re running a Web browser.
</P>
<P>Another way of displaying text is to use the method showStatus(String), defined in the class java.applet.Applet. This Applet method shows the String on the status line of the applet. (If you run appletviewer, the status line appears at the bottom of the applet window.)</P>
<P>To display text within the applet, there are three steps involved: defining the text string, choosing a font, and drawing the text to the screen. Let&#146;s cover each part in turn.</P>
<H4 ALIGN="LEFT"><A NAME="Heading13"></A><FONT COLOR="#000077">Defining Strings</FONT></H4>
<P>A String in Java is an object, and not an array of char. In addition, a String object is immutable, which means that you can&#146;t change its contents. If you want a string of characters that can be modified, create a StringBuffer object.
</P>
<P>The easiest way of creating a String object is actually shorthand for invoking the String constructor:</P>
<!-- CODE SNIP //-->
<PRE>
String helloString = "hello";
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="123-127.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="132-136.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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