📄 267-272.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:Creating Customizable Games with the AWT</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, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</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=7//-->
<!--PAGES=267-272//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="264-267.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch08/273-281.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Table 7-15 lists the cursors that are available.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-15</B> Cursor types
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="50%" ALIGN="LEFT">Cursor
<TH WIDTH="50%" ALIGN="LEFT" VALIGN="BOTTOM">Appearance
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>Frame.CROSSHAIR_CURSOR
<TD><IMG SRC="images/07-01i.jpg"><TR>
<TD>Frame.DEFAULT_CURSOR
<TD><IMG SRC="images/07-02i.jpg"><TR>
<TD>Frame.E_RESIZE_CURSOR
<TD><IMG SRC="images/07-03i.jpg"><TR>
<TD>Frame.HAND_CURSOR
<TD><IMG SRC="images/07-04i.jpg"><TR>
<TD>Frame.MOVE_CURSOR
<TD><IMG SRC="images/07-05i.jpg"><TR>
<TD>Frame.NE_RESIZE_CURSOR
<TD><IMG SRC="images/07-06i.jpg"><TR>
<TD>Frame.NW_RESIZE_CURSOR
<TD><IMG SRC="images/07-07i.jpg"><TR>
<TD>Frame.N_RESIZE_CURSOR
<TD><IMG SRC="images/07-08i.jpg"><TR>
<TD>Frame.SE_RESIZE_CURSOR
<TD><IMG SRC="images/07-09i.jpg"><TR>
<TD>Frame.SW_RESIZE_CURSOR
<TD><IMG SRC="images/07-10i.jpg"><TR>
<TD>Frame.S_RESIZE_CURSOR
<TD><IMG SRC="images/07-11i.jpg"><TR>
<TD>Frame.TEXT_CURSOR
<TD><IMG SRC="images/07-12i.jpg"><TR>
<TD>Frame.WAIT_CURSOR
<TD><IMG SRC="images/07-13i.jpg"><TR>
<TD>Frame.W_RESIZE_CURSOR
<TD><IMG SRC="images/07-14i.jpg"><TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading38"></A><FONT COLOR="#000077">Menu, MenuBar, and MenuItem</FONT></H4>
<P>Table 7-16 lists selected methods for Menu, MenuBar, and MenuItem.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-16</B> Menu, MenuBar, and MenuComponent
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="40%" ALIGN="LEFT">Class
<TH WIDTH="60%" ALIGN="LEFT" VALIGN="BOTTOM">Methods
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>Menu
<TD>public Menu(String label);
<TR>
<TD>
<TD>public synchronized MenuItemadd(MenuItem mi);
<TR>
<TD>
<TD>public synchronized void
<TR>
<TD>
<TD>remove(MenuComponent item);
<TR>
<TD>MenuBar
<TD>public MenuBar();
<TR>
<TD>
<TD>public synchronized Menu add(Menu m);
<TR>
<TD>
<TD>public synchronized void
<TR>
<TD>
<TD>remove(MenuComponent item);
<TR>
<TD>MenuItem
<TD>public MenuItem(String label);
<TR>
<TD>
<TD>public void disable();
<TR>
<TD>
<TD>public void enable();
<TR>
<TD>
<TD>public void enable(boolean cond);
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading39"></A><FONT COLOR="#000077">The Event Class</FONT></H4>
<P>Table 7-17 lists instance variables of the Event class.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-17</B> Instance variables of the Event class
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="50%" ALIGN="LEFT">Event Instance Variables
<TH WIDTH="50%" ALIGN="LEFT" VALIGN="BOTTOM">Purpose
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>public Object arg;
<TD>Argument that depends on event type
<TR>
<TD VALIGN="TOP">public int clickCount;
<TD>The number of consecutive mouse clicks<BR>(e.g., clickCount = 2 for a double-click)
<TR>
<TD VALIGN="TOP">public Event evt;
<TD>The next event (used to create a linked list of events)
<TR>
<TD>public int id;
<TD>The type of event
<TR>
<TD>public int key;
<TD>The key (used for key events)
<TR>
<TD>public int modifiers;
<TD>Modifier keys used during event
<TR>
<TD>public Object target;
<TD>The object that triggered the event
<TR>
<TD>public long when;
<TD>Time that event occurred
<TR>
<TD>public int x,y;
<TD>x and y coordinates of event
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>The <I>id</I> variable tells you the type of the event. Table 7-18 shows the values that <I>id</I> can take on. These values are defined as static constants in the Event class.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-18</B> Event types
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="65%" ALIGN="LEFT">Event.id Value
<TH WIDTH="35%" ALIGN="LEFT" VALIGN="BOTTOM">Interpretation
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>ACTION_EVENT
<TD>Action event
<TR>
<TD VALIGN="TOP">GOT_FOCUS, LOST_FOCUS
<TD>Component got/lost input focus
<TR>
<TD>KEY_ACTION, KEY_ACTION_RELEASE,
<TD>
<TR>
<TD>KEY_PRESS, KEY_RELEASE
<TD>Key event
<TR>
<TD>LIST_SELECT, LIST_DESELECT
<TD>List event
<TR>
<TD>LOAD_FILE, SAVE_FILE
<TD>File event
<TR>
<TD>MOUSE_DOWN, MOUSE_UP, MOUSE_DRAG
<TD>
<TR>
<TD>MOUSE_MOVE, MOUSE_ENTER, MOUSE_EXIT
<TD>Mouse event
<TR>
<TD>SCROLL_ABSOLUTE,
<TD>
<TR>
<TD>SCROLL_LINE_UP, SCROLL_LINE_DOWN
<TD>
<TR>
<TD>SCROLL_PAGE_UP, SCROLL_PAGE_DOWN
<TD>Scrollbar event
<TR>
<TD>WINDOW_DESTROY, WINDOW_EXPOSE,
<TD>
<TR>
<TD>WINDOW_DEICONIFY, WINDOW_ICONIFY
<TD>
<TR>
<TD>WINDOW_MOVED
<TD>Window event
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>The interpretation of the Object argument to the action() method depends on the type of component that triggered the event. Table 7-19 associates the component (stored in the <I>target</I> variable) with the Object argument.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-19</B> Interpreting arguments to action(Event evt, Object obj)
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="55%" ALIGN="LEFT">If evt.target Is an Instance of
<TH WIDTH="45%" ALIGN="LEFT" VALIGN="BOTTOM">Then <I>obj</I> Is an Instance of
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>Button
<TD>String (the button label)
<TR>
<TD>Checkbox
<TD>boolean (the checkbox state)
<TR>
<TD>Choice
<TD>String (the chosen item)
<TR>
<TD>MenuItem
<TD>String (the chosen item)
<TR>
<TD>TextField
<TD>String (the input text)
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H3><A NAME="Heading40"></A><FONT COLOR="#000077">Suggestion Box</FONT></H3>
<DL>
<DD><B>•</B> Allow the player to customize other features of the game, such as the bitmaps for the missile launcher, the aliens, or the explosions. Other possibilities for customization are the speed of the missiles, the scoring, and the color (or bitmap) for the background.
<DD><B>•</B> Explore the other widgets in the AWT. Here are the ones we didn’t cover in this chapter: Choice, List, Scrollbar, and TextArea. These aren’t hard to learn, and the pattern of creating interfaces remains the same.
<DD><B>•</B> Learn how to use GridBagLayout. This LayoutManager is the most powerful that the AWT provides, and you will use it in Java projects to come!
</DL>
<H3><A NAME="Heading41"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>In this chapter, you’ve seen how Java’s AWT allows players to interact with and customize your games in an easy, intuitive fashion. By allowing customization, your games can appeal to the broadest audience possible. And by using the AWT, your applications and games can have graphical front ends that are portable to any platform that runs Java.
</P>
<P>In the next chapter, you’ll see how to use networking in your games!</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="264-267.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch08/273-281.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -