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

📄 377-380.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: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,&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=10//-->
<!--PAGES=377-380//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="373-377.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="380-383.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading23"></A><FONT COLOR="#000077">The Date Class</FONT></H4>
<P>The Date class encapsulates methods that manipulate time in a platform-independent manner. If you&#146;re writing a game or application with real-time requirements, such as a video game, you can use this class to access the system clock. For example, the following fragment creates a Date object with the current time and date:
</P>
<!-- CODE SNIP //-->
<PRE>
// contains the current time and date
Date d = new Date();
</PRE>
<!-- END CODE SNIP //-->
<P>You can also initialize a Date to a given value:
</P>
<!-- CODE SNIP //-->
<PRE>
// initialized to the date April 9, 1969
Date e = new Date(69,4,9);
</PRE>
<!-- END CODE SNIP //-->
<P>To compare dates, use the instance methods after(Date), before(Date), and equals(Date), all of which return a <I>boolean</I>:</P>
<!-- CODE SNIP //-->
<PRE>
boolean b1 = d.after(e);  // true
boolean b2 = d.before(e); // false
boolean b3 = d.equals(e); // false
</PRE>
<!-- END CODE SNIP //-->
<P>Here&#146;s how you can access the various components of the Date object:
</P>
<!-- CODE SNIP //-->
<PRE>
int year = d.getYear();      // get years since 1900
int month = d.getMonth();    // get month (1..12)
int date = d.getDate();      // get date in month (1..31)
int hours = d.getHours();    // get hour (0..23)
int minutes = d.getMinutes();// get minute (0..59)
int seconds = d.getSeconds();// get second (0..59)
</PRE>
<!-- END CODE SNIP //-->
<P>You can set these components using the corresponding methods: setYear(int), set Month(int), setDate(int), setHours(int), setMinutes(int), and setSeconds(int).
</P>
<P>Finally, to get the time in milliseconds since GMT 12:00:00 <TT>A.M.</TT> of January 1, 1970, use getTime():</P>
<!-- CODE SNIP //-->
<PRE>
long time = d.getTime();
</PRE>
<!-- END CODE SNIP //-->
<P>You can use this method to dynamically measure the performance of your games. For example:
</P>
<!-- CODE SNIP //-->
<PRE>
long before = new Date().getTime();  // time before
...                                  // some code here
long after = new Date().getTime();   // time after
long elapsed = after - before;       // elapsed time
</PRE>
<!-- END CODE SNIP //-->
<P>Then, your program might alter some parameters, based on the value of <I>elapsed</I>. For example, if <I>elapsed</I> is too high, your game might reduce the number of objects it&#146;s tracking.</P>
<P>An equivalent to the getTime() method is currentTimeMillis() in the java.lang.System class. Table 10-2 shows the methods in the Date class.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 10-2</B> Methods in java.util.Date
<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">Date
<TD>public Date();
<TR>
<TD>
<TD>public Date(int year, int month, int date);
<TR>
<TD>
<TD>public boolean after(Date when);
<TR>
<TD>
<TD>public boolean before(Date when);
<TR>
<TD>
<TD>public boolean equals(Object obj);
<TR>
<TD>
<TD>public long getTime();
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading24"></A><FONT COLOR="#000077">The Vector Class</FONT></H4>
<P>A Vector stores an ordered collection of objects that can grow or shrink dynamically. As a result, it offers more flexibility than an array, which requires that the number of elements be known prior to allocation. You can use a Vector for game situations in which the number of objects to be stored changes as the game progresses. Keep in mind, though, that a Vector is more complex than an array, and it&#146;s correspondingly slower.
</P>
<P>It&#146;s easy to create a Vector:</P>
<!-- CODE SNIP //-->
<PRE>
Vector v = new Vector();
</PRE>
<!-- END CODE SNIP //-->
<P>Here&#146;s how you add objects to and remove objects from the Vector:
</P>
<!-- CODE SNIP //-->
<PRE>
// obj1, obj2, obj3 are arbitrary objects
v.addElement(obj1);
v.addElement(obj2);
v.addElement(obj3);
v.removeElement(obj2);
</PRE>
<!-- END CODE SNIP //-->
<P>You can address the elements of a Vector directly, using an index starting from 0:
</P>
<!-- CODE SNIP //-->
<PRE>
Object obj4 = v.elementAt(0);  // get the element at 0
v.removeElementAt(1);          // delete the element at 1
</PRE>
<!-- END CODE SNIP //-->
<P>To figure out the number of objects stored in the Vector, use the instance method size():
</P>
<!-- CODE SNIP //-->
<PRE>
int size = v.size();  // num elements in v
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, here&#146;s how you read the elements of the Vector, using the Enumeration interface. The methods declared by the Enumeration interface allow you to iterate through the elements of a collection class, such as a Vector. An object that implements Enumeration refers to a set of elements that you can step through, one at a time, without using an explicit index. There are two Enumeration methods: nextElement(), which returns the next object in the Enumeration, and hasMoreElements(), which returns <I>true</I> if there are elements left.</P>
<P>Here&#146;s an example. The first step is to define an Enumeration that refers to the elements of the Vector. The Vector method elements() does just that:</P>
<!-- CODE SNIP //-->
<PRE>
Enumeration e = v.elements();
</PRE>
<!-- END CODE SNIP //-->
<P>Now, the following loop
</P>
<!-- CODE SNIP //-->
<PRE>
while (e.hasMoreElements()) &#123;
  Object obj = e.nextElement();
  ...
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>will set the variable <I>obj</I> to each element of the Vector, for each iteration of the loop.</P>
<P>Table 10-3 shows the methods in the Enumeration interface and the Vector class.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 10-3</B> Methods in java.util.Enumeration and java.util.Vector
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="50%" ALIGN="LEFT">Class/Interface
<TH WIDTH="50%" ALIGN="LEFT">Methods
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD VALIGN="TOP">Enumeration
<TD>public abstract boolean hasMoreElements();
<TR>
<TD>
<TD>public abstract Object nextElement() throws NoSuchElementException;
<TR>
<TD VALIGN="TOP">Vector
<TD>public new Vector();
<TR>
<TD>
<TD>public final synchronized void addElement(Object obj);
<TR>
<TD>
<TD>public final synchronized Enumeration elements();
<TR>
<TD>
<TD>public final synchronized Object elementAt(int index) throws ArrayOutOfBoundsException
<TR>
<TD>
<TD>public final synchronized boolean removeElement(Object obj);
<TR>
<TD>
<TD>public final synchronized void removeElementAt(int index) throws ArrayOutOfBoundsException
<TR>
<TD>
<TD>public final int size();
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="373-377.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="380-383.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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