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

📄 353-358.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=353-358//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch09/349-352.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="358-363.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 10<BR>Advanced Techniques
</FONT></H2>
<P><I>Joel Fan</I></P>
<P><FONT SIZE="+1"><B>Goals:</B></FONT></P>
<P>Learn advanced techniques you can use in games, including the use of packages, threads, image processing, and data structures in java.util
</P>
<P>In this chapter, you&#146;ll learn a variety of techniques that will be useful in creating games and other applications. For example, you&#146;ll see:</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;How to create packages that organize related collections of classes.
<DD><B>&#149;</B>&nbsp;&nbsp;How to organize games and other applications with threads.
<DD><B>&#149;</B>&nbsp;&nbsp;How to use java.awt.image for simple image processing. In particular, you&#146;ll see how to reduce the download time of a series of bitmaps.
<DD><B>&#149;</B>&nbsp;&nbsp;Data structures in java.util that make writing games easier.
<DD><B>&#149;</B>&nbsp;&nbsp;Tips for creating applets that perform across a wide variety of platforms.
</DL>
<P>Feel free to skim this chapter and adapt whatever you need for your own projects. Let&#146;s get started!
</P>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">Using Packages</FONT></H3>
<P>A <I>package</I> is a group of classes, interfaces, and/or subpackages that share a related purpose. Examples of packages are java.awt.image or java.lang. The <I>fully qualified name</I> of a class</P>
<!-- CODE SNIP //-->
<PRE>
&lt;packagename&gt;.&lt;classname&gt;
</PRE>
<!-- END CODE SNIP //-->
<P>denotes the given class within the package. For example, the fully qualified name of the Graphics class, contained in package java.awt, is java.awt.Graphics.
</P>
<P>All classes in Java belong to some package. Within a class definition, you can refer to another class in the same package by using the class name alone. However, a class must use the fully qualified name when referring to a class outside of its own package.</P>
<H4 ALIGN="LEFT"><A NAME="Heading3"></A><FONT COLOR="#000077">Importing Packages and Classes</FONT></H4>
<P>It&#146;s inconvenient to type the fully qualified name every time you want to refer to a class in another package. By <I>importing</I> a package or a class into your program, you can use class names alone.</P>
<P>Java supports three versions of the <I>import</I> statement:</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;<I>Import a single class</I>. Use the fully qualified name of the class in the <I>import</I> statement. Then, the class name alone will refer to the imported class. For example:
<!-- CODE SNIP //-->
<PRE>
// can refer to 'Graphics' in following code
import java.awt.Graphics;
</PRE>
<!-- END CODE SNIP //-->
<DD><B>&#149;</B>&nbsp;&nbsp;<I>Import all the classes in a package</I>. Use the package name, qualified by <TT>*</TT>, in the <I>import</I> statement. Then, all classes in the packages can be referred to by using only the class names.
<!-- CODE SNIP //-->
<PRE>
// can refer to all classes in java.awt using
//   class name alone, i. e. 'Component', 'Graphics', ...
 import java.awt.*;
</PRE>
<!-- END CODE SNIP //-->
<DD><B>&#149;</B>&nbsp;&nbsp;<I>Import the package</I>. Use the package name in the <I>import</I> statement:
<!-- CODE SNIP //-->
<PRE>
import java.awt;
</PRE>
<!-- END CODE SNIP //-->
<BR>Then to reference a class, use the last component of the package name, followed by the class name, i.e., &#147;awt.Graphics&#148;.
</DL>
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Creating Packages</FONT></H4>
<P>You can create your own packages of classes and interfaces. This is useful for three reasons:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;<I>To organize related classes and interfaces</I>. A package gives related classes and interfaces conceptual unity. It&#146;s a good idea to group libraries of class files into packages that you can import when necessary. An example of a set of related classes is the Sprite class we developed in Chapters 3, 4, and 5. To create a package called &#147;sprite&#148;, place the following statement
<!-- CODE SNIP //-->
<PRE>
package sprite;
</PRE>
<!-- END CODE SNIP //-->
<BR>at the beginning of each file that contains code to a Sprite class. (The package declaration <I>must</I> be the first statement in the file.) If you don&#146;t include a package statement at the start of a file, the classes in the file will be members of the default package.
<DD><B>&#149;</B>&nbsp;&nbsp;<I>To avoid name conflicts</I>. By creating packages, you avoid the possibility of naming conflicts while developing applications. Let&#146;s say you&#146;re writing a program with your friend Ira. By putting all of Ira&#146;s classes into a package called &#147;ira&#148;, and your classes into your own package, there won&#146;t be any problems if you both pick the same names for classes. For example, if each package has a class Shark, the two versions can be distinguished using the fully qualified name:
<!-- CODE SNIP //-->
<PRE>
ira.Shark shark1;  // Shark class in package ira
jon.Shark shark2;  // Shark class in package jon
</PRE>
<!-- END CODE SNIP //-->
<DD><B>&#149;</B>&nbsp;&nbsp;<I>For visibility</I>. The classes in a package have direct access to all variables for which access isn&#146;t specified (for example, <I>public</I>, <I>private</I>, <I>protected</I>), and to all classes within the package. You can use this default level of access to make the classes in a package more efficient, by eliminating unnecessary accessor methods while keeping these variables visible only within a package. Only the <I>public</I> classes and interfaces of a package are accessible outside of the package. Thus, you can create helper classes that are non-<I>public</I>, and invisible outside package boundaries.
</DL>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Nested Packages and Directory Structure</FONT></H4>
<P>A package can be nested within another package. The declaration
</P>
<!-- CODE SNIP //-->
<PRE>
package game.sprite;
</PRE>
<!-- END CODE SNIP //-->
<P>denotes a package &#147;sprite&#148; in a package called &#147;game&#148;. In a similar way, the packages of the Java API, such as java.lang and java.awt, are subpackages of the enclosing &#147;java&#148; package. Thus, packages are organized in hierarchical fashion.
</P>
<P>For many installations, the package hierarchy corresponds to the directory structure of the packages. Thus, class files of the package &#147;game&#148; belong in some directory called game/, while class files of package game.sprite are found in the directory game/sprite/. If you don&#146;t state the package that a source file belongs to, it is considered part of the <I>default package</I>, which corresponds to the directory that the file is in.</P>
<P>When you&#146;re developing a game, it&#146;s easiest to keep your files as part of the default package. But once you&#146;ve built up a library of classes you&#146;ll use in future games, you can arrange these class files in packages and import them when necessary.</P>
<P>Now, let&#146;s move on to threads.</P>
<H3><A NAME="Heading6"></A><FONT COLOR="#000077">Using Threads</FONT></H3>
<P>Threads are one of Java&#146;s most powerful features. In Chapter 8, Implementing a High Score Server on a Network, you were introduced to threads and how they can be used to create networked games and applications. In this chapter, you will see further advantages and pitfalls of working with a &#147;threaded approach&#148; in your applets. First, let&#146;s review the definition of a thread.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch09/349-352.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="358-363.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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