📄 250-255.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=250-255//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="246-250.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="255-260.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Frames are Containers, so you can call setLayout() and add(), as with Panels. However, Frames provide additional functionality, and in the last example in this chapter, you’ll see how to create menu bars for them. The basic Frame methods are listed in Table 7-9.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-9</B> Basic Frame methods
<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>Frame
<TD>public Frame();
<TR>
<TD>
<TD>public Frame(String title);
<TR>
<TD>
<TD>public synchronized void dispose(); //
<TR>
<TD>
<TD>overrides dispose() from Window
<TR>
<TD>
<TD>public MenuBar getMenuBar();
<TR>
<TD>
<TD>public boolean isResizable();
<TR>
<TD>
<TD>public void setCursor(int cursorType);
<TR>
<TD>
<TD>public synchronized void
<TR>
<TD>
<TD>setMenuBar(MenuBar mb);
<TR>
<TD>
<TD>public void setResizable(boolean b);
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>Finally, let’s discuss Dialog.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">Dialogs</FONT></H4>
<P>Although a Dialog creates its own window, like a Frame, it doesn’t provide certain features, such as menu bars or iconification. Dialogs are somewhat like telephone calls, interrupting the normal flow of activity, and appearing and disappearing when necessary. For example, you might use a Dialog box to confirm a file deletion or to alert the user of an error condition. A Dialog can also be <I>modal</I>, which means that it blocks input to all other windows while it is on the screen. In other words, you might say that a modal Dialog is like a telephone call you must answer before doing anything else!</P>
<P>A Dialog always has a parent Frame. For example, the following creates a Dialog with the title A Dialog Box:</P>
<!-- CODE SNIP //-->
<PRE>
Frame parent = new Frame("A Frame");
Dialog dialog = new Dialog(parent, "A Dialog Box", false);
</PRE>
<!-- END CODE SNIP //-->
<P>The last argument to the Dialog constructor, a boolean, indicates whether the Dialog is modal (<I>true</I>) or not (<I>false</I>).</P>
<P>As with the other Containers, you can use setLayout() and add components to a Dialog window. The Component methods show() and hide(), are of course available too:</P>
<!-- CODE SNIP //-->
<PRE>
dialog.show(); // show dialog box
dialog.hide(); // hide dialog box
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, since Dialogs are used on an interim basis, it’s nice to free up system resources when they’re done:
</P>
<!-- CODE SNIP //-->
<PRE>
dialog.dispose();// frees resources used by the dialog
</PRE>
<!-- END CODE SNIP //-->
<P>The basic Dialog methods are listed in Table 7-10.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 7-10</B> Basic Dialog methods
<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>Dialog
<TD>public Dialog(Frame parent,boolean modal);
<TR>
<TD>
<TD>public Dialog(Frame parent,String title,boolean modal);
<TR>
<TD>
<TD>public synchronized void dispose(); //
<TR>
<TD>
<TD>inherited from Window
<TR>
<TD>
<TD>public boolean isModal();
<TR>
<TD>
<TD>public boolean isResizable();
<TR>
<TD>
<TD>public void setResizable(boolean b);
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>We’ll create a Dialog box in the next section; peek ahead to Figure 7-15 to see what a Dialog object looks like.
</P>
<P>Now, let’s use what you’ve learned about the AWT to create a new graphical interface that allows the player to customize the Alien Landing game. You might want to review the design of the game in Chapter 5, Building a Video Game (see the section Dividing Responsibility Among Functional Units).</P>
<H3><A NAME="Heading24"></A><FONT COLOR="#000077">Customizing Alien Landing</FONT></H3>
<P>We are going to do three things here.
</P>
<DL>
<DD><B>•</B> First, we will put the Alien Landing applet into a Frame, so it can take advantage of the cooler-looking crosshair cursor! The idea is to derive a new class, GameFrame, that we will add (using add()) the applet to.
<DD><B>•</B> Second, we will create a menu bar, menus, and menu items for the GameFrame, so that the player can choose a new game, abort a game, or exit the game applet completely. The GameFrame will send messages to the GameManager class depending on the player’s selection. In addition, the Options menu will select the customization dialog.
<DD><B>•</B> Finally, we will define a subclass of Dialog called OptionsDialog which obtains the player’s preferences. The OptionsDialog object will then tell the GameManager to modify the appropriate game parameters.
</DL>
<P>Figure 7-13 shows the communication that takes place between the new classes we will create and the GameManager class.
</P>
<P><A NAME="Fig13"></A><A HREF="javascript:displayWindow('images/07-13.jpg',562,382 )"><IMG SRC="images/07-13t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-13.jpg',562,382)"><FONT COLOR="#000077"><B>Figure 7-13</B></FONT></A> Communication between classes</P>
<P>Let’s get started.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading25"></A><FONT COLOR="#000077">Defining the GameFrame Container</FONT></H4>
<P>GameFrame will derive from Frame. GameFrame’s constructor will take the applet and its width and height as arguments:
</P>
<!-- CODE SNIP //-->
<PRE>
public GameFrame(Applet app,int width,int height) {
</PRE>
<!-- END CODE SNIP //-->
<P>The first thing the GameFrame constructor does is call the Frame constructor:
</P>
<!-- CODE SNIP //-->
<PRE>
super("Alien Landing");
</PRE>
<!-- END CODE SNIP //-->
<P>Let’s set the size of the Frame, and change the cursor as well:
</P>
<!-- CODE SNIP //-->
<PRE>
resize(width+13,height+65);
setResizable(false);
setCursor(Frame.CROSSHAIR_CURSOR);
</PRE>
<!-- END CODE SNIP //-->
<P>Now insert the applet into a Panel, and add this Panel to the GameFrame:
</P>
<!-- CODE SNIP //-->
<PRE>
p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER));
p.add(app);
setLayout(new BorderLayout());
add("Center",p);
</PRE>
<!-- END CODE SNIP //-->
<P>You will also need to construct GameFrame in the init() method of the Alien Landing applet:
</P>
<!-- CODE //-->
<PRE>
GameFrame f;
public void init() {
...
width = 240;
height = 270;
...
f = new GameFrame(this,width,height);
...
}
</PRE>
<!-- END CODE //-->
<P>If you want, you can test these changes right now. But let’s go further, by adding a MenuBar to the GameFrame.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading26"></A><FONT COLOR="#000077">Creating a Menu Bar</FONT></H4>
<P>A MenuBar is the strip at the top of the Frame that lists possible choices. For example, Figure 7-14 shows the menu bar we will create for the Alien Landing GameFrame.
</P>
<P><A NAME="Fig14"></A><A HREF="javascript:displayWindow('images/07-14.jpg',243,326 )"><IMG SRC="images/07-14t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/07-14.jpg',243,326)"><FONT COLOR="#000077"><B>Figure 7-14</B></FONT></A> GameFrame MenuBar</P>
<P>Let’s create this MenuBar. All of the following code goes into GameFrame’s constructor.
</P>
<P>First, you must instantiate a MenuBar object:</P>
<!-- CODE SNIP //-->
<PRE>
MenuBar menubar = new MenuBar();
</PRE>
<!-- END CODE SNIP //-->
<P>Then, create each Menu. There are two of them: Game and Options.
</P>
<!-- CODE SNIP //-->
<PRE>
Menu m1 = new Menu("Game");
Menu m2 = new Menu("Options");
</PRE>
<!-- END CODE SNIP //-->
<P>At this point, both Menus are empty. If you click on the Game Menu, for example, there aren’t any MenuItems to select. You need to insert individual MenuItems into each Menu, using add(). For the Game Menu, you will allow three choices: New Game, Abort Game, and Exit:
</P>
<!-- CODE SNIP //-->
<PRE>
// newItem,abortItem are MenuItems
newItem = new MenuItem("New Game");
m1.add(newItem);
abortItem = new MenuItem("Abort Game");
abortItem.disable();
m1.add(abortItem);
m1.add(new MenuItem("Exit"));
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="246-250.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="255-260.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -