📄 380-383.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:Advanced Techniques</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=10//-->
<!--PAGES=380-383//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="377-380.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="383-386.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading25"></A><FONT COLOR="#000077">The Stack Class</FONT></H4>
<P>A Stack implements last-in, first-out storage (as in a stack of dirty dinner plates to be cleaned!). Stacks are fundamental data structures that come in handy in numerous programming situations. You can push() an element onto the top of the Stack, or remove the top element, using pop(). To look at the top element, without removing it, use peek(). empty() returns <I>true</I> if there are no elements on the Stack. Table 10-4 shows the methods in the Stack class.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 10-4</B> Methods in java.util.Stack
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="50%" ALIGN="LEFT">Class
<TH WIDTH="50%" ALIGN="LEFT">Methods
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP">Stack
<TD>public Stack();
<TR>
<TD>
<TD>public boolean empty();
<TR>
<TD>
<TD>public Object peek() throws EmptyStackException;
<TR>
<TD>
<TD>public Object pop() throws
<TR>
<TD>
<TD>EmptyStackException;
<TR>
<TD>
<TD>public Object push(Object item);
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>Here’s an example using a Stack:
</P>
<!-- CODE SNIP //-->
<PRE>
Stack s = new Stack();
// obj1, obj2 are arbitrary objects
s.push(obj1);
s.push(obj2);
Object obj3 = s.pop();
Object obj4 = s.peek();
boolean isEmpty = s.empty();
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading26"></A><FONT COLOR="#000077">The Hashtable Class</FONT></H4>
<P>A Hashtable stores pairs of keys and objects. You can retrieve the object using the key. A key can be any arbitrary object, so you might consider a Hashtable as an array that allows arbitrary objects to serve as indices.
</P>
<P>Hashtables are a convenient and natural way to store dictionary data, such as a word and its definition. For example, you could use a Hashtable to record a player’s name, and his high scores.</P>
<P>Here’s an example. First, instantiate the Hashtable:</P>
<!-- CODE SNIP //-->
<PRE>
Hashtable h = new Hashtable();
</PRE>
<!-- END CODE SNIP //-->
<P>Keys can be arbitrary objects. To associate an object with a particular key in the Hashtable, use
</P>
<!-- CODE SNIP //-->
<PRE>
// keys key1, key2 are arbitrary objects
// objects obj1, obj2 are arbitrary objects also
h.put(key1, obj1); // associate obj1 with key1
h.put(key2, obj2); // associate obj2 with key2
</PRE>
<!-- END CODE SNIP //-->
<P>Now you can look up the entries in the table by using get() for a given key:
</P>
<!-- CODE SNIP //-->
<PRE>
Object obj3 = h.get(key1);
</PRE>
<!-- END CODE SNIP //-->
<P>You can also remove the element corresponding to a given key:
</P>
<!-- CODE SNIP //-->
<PRE>
h.remove(key2);
</PRE>
<!-- END CODE SNIP //-->
<P>The methods in the Hashtable class are shown in Table 10-5.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 10-5</B> Methods in java.util.Hashtable
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="50%" ALIGN="LEFT">Class
<TH WIDTH="50%" ALIGN="LEFT">Methods
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP">Hashtable
<TD>public Hashtable();
<TR>
<TD>
<TD>public synchronized Object get(Object key);
<TR>
<TD>
<TD>public synchronized Object put(Object key,Object value) throws NullPointerException;
<TR>
<TD>
<TD>public synchronized Object remove(Object key);
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading27"></A><FONT COLOR="#000077">The Random Class</FONT></H4>
<P>Random number generators are used to give games variety and unpredictability. The Random class provides several methods for producing <I>pseudo-random</I> values. The numbers generated aren’t truly random, but computed with a formula that takes a seed value.</P>
<P>Here’s how. To create a Random object, seeded with the current time, use:</P>
<!-- CODE SNIP //-->
<PRE>
// seeded with current time
Random r1 = new Random();
</PRE>
<!-- END CODE SNIP //-->
<P>You can specify the seed (a long) in the constructor, or using the instance method setSeed(long):
</P>
<!-- CODE SNIP //-->
<PRE>
// seed in constructor
Random r3 = new Random(1317L);
r1.setSeed(1713L);
</PRE>
<!-- END CODE SNIP //-->
<P>By specifying a seed, you can fix the sequence of random numbers, which is invaluable in debugging.
</P>
<P>Here’s how to get random numbers of different types:</P>
<!-- CODE //-->
<PRE>
// get int between Integer.MIN_VAL and Integer.MAX_VALUE
int i = r1.nextInt();
// get long between Long.MIN_VAL and Long.MAX_VALUE
long l = r3.nextLong();
// get float between 0.0f and 1.0f
float f = r1.nextFloat();
// get double between 0.0 and 1.0
double d = r3.nextDouble();
</PRE>
<!-- END CODE //-->
<P>Another way to get a random double between 0.0 and 1.0 is to use the static method Math.random():
</P>
<!-- CODE SNIP //-->
<PRE>
double d2 = Math.random();
</PRE>
<!-- END CODE SNIP //-->
<P>The methods in the Random class are shown in Table 10-6.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 10-6</B> Methods in java.util.Random
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="50%" ALIGN="LEFT">Class
<TH WIDTH="50%" ALIGN="LEFT">Methods
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP">Random
<TD>public Random();
<TR>
<TD>
<TD>public Random(long seed);
<TR>
<TD>
<TD>public double nextDouble();
<TR>
<TD>
<TD>public float nextFloat();
<TR>
<TD>
<TD>public int nextInt();
<TR>
<TD>
<TD>public long nextLong();
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>In the next section, you’ll get some tips for writing game applets that perform across different platforms.
</P>
<H3><A NAME="Heading28"></A><FONT COLOR="#000077">Writing for Multiple Platforms</FONT></H3>
<P>The Java environment is designed to exhibit similar behavior across a wide range of platforms. However, there are times when your code might run properly on one platform, only to crash and burn on another. Here are some things to keep in mind.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading29"></A><FONT COLOR="#000077">Yield Threads</FONT></H4>
<P>You’ve already seen how time-slicing systems execute two threads of equal priority differently from non-time-slicing systems. If your code depends on one form of scheduling, you shouldn’t be surprised when a friend of yours downloads your applet and tells you it doesn’t work on his machine! When you have a program with multiple threads, it’s wise to explicitly yield() at natural intervals. For example, if the run() method of some thread has a loop, you might want to yield() every iteration:
</P>
<!-- CODE SNIP //-->
<PRE>
// the run() method of some thread
public void run() {
for (int i = 0; i < 1713; i++) {
... // some code here
yield(); // yield the thread
}
}
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="377-380.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="383-386.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -