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

📄 383-386.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=383-386//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="380-383.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch11/387-393.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading30"></A><FONT COLOR="#000077">Aligning Text</FONT></H4>
<P>Each windowing toolkit has a different look and feel, and GUI components take up different amounts of space, depending on the particular toolkit. One way to lay out text in a window (such as an Applet or a Frame) is to define the text String, and write it to the window at the given coordinates:
</P>
<!-- CODE SNIP //-->
<PRE>
// g is Graphics context
// draw textString at (130,170)
g.drawString(textString, 130, 170);
</PRE>
<!-- END CODE SNIP //-->
<P>Although this is the simplest way to lay out text, the text might not align properly across all platforms. If alignment is important, it&#146;s better to define Labels and use the appropriate LayoutManager. For example, you can center or right-justify text in this way. If the user resizes the window, the LayoutManager will automatically center the label for the new screen size.
</P>
<P>LayoutManagers and Labels don&#146;t provide the fine-grained control that you sometimes need. In this case, use the java.awt.FontMetrics class to determine the height and width of the particular string under a particular font, and position it accordingly.</P>
<H4 ALIGN="LEFT"><A NAME="Heading31"></A><FONT COLOR="#000077">Determining System Properties</FONT></H4>
<P>Let&#146;s face it&#150;not all systems are created equal. You might be slaving away in front of a 13-inch monitor, while your more fortunate brethren are basking in the glow of huge Sony Trinitrons and tremendous screen resolution. Thus, a game that looks just right on the latter system might take up too much space on the former; likewise, an applet that looks fine on the 13-inch screen seems invisible on bigger ones. What to do?
</P>
<H4 ALIGN="CENTER"><A NAME="Heading32"></A><FONT COLOR="#000077">Determining Screen Parameters</FONT></H4>
<P>One solution is to pick a size for your game that seems to work well across different installations. This is a bit approximate, but it&#146;s the simplest alternative.
</P>
<P>The second solution takes a bit more work. The API allows you to determine the screen size, screen resolution, and other system properties as well. Then, you can adjust parameters in your games at runtime, depending on characteristics of the particular system.</P>
<P>Here&#146;s how you can determine the screen size and resolution that your applet&#146;s running on by using the class java.awt.Toolkit. First, use the Component method getToolkit() (you can put this in an applet, for example):</P>
<!-- CODE SNIP //-->
<PRE>
Toolkit t= getToolkit();
</PRE>
<!-- END CODE SNIP //-->
<P>Then, the Toolkit method getScreenResolution() returns the screen resolution in dots per inch:
</P>
<!-- CODE SNIP //-->
<PRE>
int dpi = t.getScreenResolution();
</PRE>
<!-- END CODE SNIP //-->
<P>and the method getScreenSize() returns the screen dimensions:
</P>
<!-- CODE SNIP //-->
<PRE>
Dimension d = t.getScreenSize();
int screen_width = d.width;
int screen_height = d.height;
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="CENTER"><A NAME="Heading33"></A><FONT COLOR="#000077">Obtaining System Properties</FONT></H4>
<P>It&#146;s also possible to determine other system properties as well. For example, your game applet can determine the architecture of the system that it&#146;s running on, and adjust parameters accordingly. (For slower systems, your game might perform fewer computations.)
</P>
<P>The first step is to get the table of system Properties:</P>
<!-- CODE SNIP //-->
<PRE>
Properties p = System.getProperties();
</PRE>
<!-- END CODE SNIP //-->
<P>Properties is a class defined in java.util that extends Hashtable. To look up values in a Properties object, use the instance method getProperty(String). Table 10-7 shows some keys available in the Properties object returned by the System class.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 10-7</B> System property keys
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="50%" ALIGN="LEFT">Key
<TH WIDTH="50%" ALIGN="LEFT">Property
<TR>
<TD COLSPAN="2"><HR>
<TR>
<TD>&#147;java.version&#148;
<TD>Java interpreter version
<TR>
<TD>&#147;java.class.version&#148;
<TD>Java API version
<TR>
<TD>&#147;os.arch&#148;
<TD>Host architecture
<TR>
<TD>&#147;os.name&#148;
<TD>Host operating system
<TR>
<TD>&#147;os.version&#148;
<TD>Operating system version
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>For example, here&#146;s how your game can determine the API version of the host installation:
</P>
<!-- CODE SNIP //-->
<PRE>
Properties p = System.getProperties();
String version = p.getProperties(&#147;java.class.version&#148;);
</PRE>
<!-- END CODE SNIP //-->
<P>Then, your applet can proceed accordingly, perhaps taking advantage of features found in newer versions of the API.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading34"></A><FONT COLOR="#000077">Ask the Player</FONT></H4>
<P>Finally, you should not overlook the simplest way to determine system properties, which is to ask the player. You can have an introductory screen that allows the player to select the platform the applet is running on. Another option is to place the list of platforms on the HTML page. The platform that the player chooses runs the applet with a certain set of applet parameters, or even runs a different applet altogether.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading35"></A><FONT COLOR="#000077">Allowing Customization</FONT></H4>
<P>Each user has a particular range of preferences, such as preferred colors, preferred sounds, and level of difficulty. By allowing your game to be customizable, not only can the user adjust the applet to the platform it&#146;s running on, he or she can tailor it to his or her particular preferences. This is one of the best ways of smoothing out bumps as your applet travels from computer to computer, and we cover it in Chapter 7, Creating Customizable Games with the AWT.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading36"></A><FONT COLOR="#000077">Testing Your Games on Multiple Platforms</FONT></H4>
<P>This is an important step to take before unleashing your applet on the world. Have your friends across the Web test your game, if possible. They can tell you how long it takes to download, how it looks, and how it runs. Be prepared for some surprises&#151;and another round of modifications&#151;before you&#146;re done!
</P>
<H3><A NAME="Heading37"></A><FONT COLOR="#000077">Suggestion Box</FONT></H3>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Create a graphics simulation of bouncing objects by using a separate thread for each. You can start by extending the Actor class, which we defined above. You can also model other real-time systems using threads, such as a nuclear power plant, a city, or an airplane.
<DD><B>&#149;</B>&nbsp;&nbsp;MemoryImageSource is an ImageProducer that allows you to specify the image data, pixel by pixel. Use this class to create a color wheel that displays the possible combinations of red, green, and blue.
<DD><B>&#149;</B>&nbsp;&nbsp;RGBImageFilter is an ImageFilter that allows you to change the colors in an image. See how easy it is to swap the red and green component of each pixel in an Image. Then experiment with various filtering formulas to create cool-looking images.
<DD><B>&#149;</B>&nbsp;&nbsp;Allow your next graphics applet to adjust automatically to the screen size and resolution of the computer that it&#146;s executing on.
</DL>
<H3><A NAME="Heading38"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>In this chapter, you&#146;ve learned some finer points of creating multithreaded games in Java. Multiple threads allow you to model independent, concurrent processes in your games. You&#146;ve learned how to filter images and speed up the process of loading image data into a game application. You&#146;ve also covered important classes in java.util and have seen ways of making your applets as portable as possible.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="380-383.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch11/387-393.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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