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

📄 296-299.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:Implementing a High Score Server on a Network</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=8//-->
<!--PAGES=296-299//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="293-296.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="299-301.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading32"></A><FONT COLOR="#000077">Creating a Testing Applet</FONT></H4>
<P>Now that our HighScoreManager is written, we need an applet that can test it. Because it doesn&#146;t actually track any scores over the Internet yet, we can set up a very simple applet to demonstrate how the HighScoreManager works. If you already have a game ready for the HighScoreManager, read this section, and then go ahead and plug the HighScoreManager into your game. Otherwise, we will walk through the creation of a sample applet to see how it&#146;s done.
</P>
<P>Let&#146;s start by making a new file, called testApp.java. In it, we will put the very minimum required for an applet:</P>
<!-- CODE //-->
<PRE>
import java.applet.*; // Our misc. Java classes we will need
import java.awt.*;
import HighScoreManager;  // Don't forget to include this!

public class testApp extends Applet &#123;

void init() &#123; // Does nothing for now
&#125;

&#125;
</PRE>
<!-- END CODE //-->
<P>Because our HighScoreManager class does all of the work required, we can test it out by adding a very minimal amount of code. The first thing to do is to create a new HighScoreManager and initialize it:
</P>
<!-- CODE SNIP //-->
<PRE>
HighScoreManager HS;

void init() &#123;
       HS = new HighScoreManager(10);  // Let's have it track 10 high &#8656;
scores
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Next, we need to tell the applet how to draw itself. This is accomplished via the paint() method. Because we created a paintScores() method in our HighScoreManager, all we have to do is pass along some information: a Rectangle indicating where we would like the HighScoreManager to draw the scores. Because this is just a sample applet, we can let the HighScoreManager paint all over it, so we pass a Rectangle that &#147;bounds&#148; the entire applet. In case our applet gets resized, we make sure our method queries the current size, via the size() method. The HighScoreManager class will do all the work for us! Let&#146;s define our paint method like this:
</P>
<!-- CODE SNIP //-->
<PRE>
void paint(Graphics g) &#123;
Rectangle r;

r = new Rectangle( 0 , 0 , size().width, size().height);

HS.paintScores(g,r);
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>That&#146;s it! Go ahead and compile testApp. It should look something like Figure 8-3. Have fun resizing the applet, and move some other windows in front of it to see how it handles redrawing the screen. It should do pretty well, but you will notice that the screen flickers a lot, despite all of the work paintScores() does to try to eliminate this.
</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/08-03.jpg',694,333 )"><IMG SRC="images/08-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/08-03.jpg',694,333)"><FONT COLOR="#000077"><B>Figure 8-3</B></FONT></A>&nbsp;&nbsp;testApp.java, first edition</P>
<H4 ALIGN="CENTER"><A NAME="Heading33"></A><FONT COLOR="#000077">Double-Buffering the Testing Applet</FONT></H4>
<P>In order to eliminate flicker, we will use double-buffered graphics. This technique was discussed in Chapter 2, Using Objects for Animations, so you should already know how it works.
</P>
<!-- CODE //-->
<PRE>
Image im;     // Declare these globally!
Graphics g;

public void update(Graphics bg) &#123;

// Notice that we changed the name of the local Graphics context from g &#8656;
to bg!
       Rectangle r= new Rectangle(0,0,size().width,size().height);

       im=createImage(size().width,size().height);  // Create a new Image
       g=im.getGraphics();   // Associate the Graphics context
       HS.paintScores(g,r);  // Do the work

paint(bg);   // Pass it to the paint() method
&#125;

public void paint(Graphics bg) &#123;
       if(im!=null)  // Make sure we have something to draw!
       bg.drawImage(im,0,0,null);   // Do the drawing

&#125;
</PRE>
<!-- END CODE //-->
<P>Notice that no drawing happens on the screen until the drawImage() command, even though we call several drawing methods in paintScores().
</P>
<P>Because the HighScoreManager currently uses bogus data, it displays the same thing every time. To demonstrate how easy it is to use our HighScoreManager class, we should allow the user of the testApp to enter his/her name and then display a random &#147;score.&#148;</P>
<H4 ALIGN="CENTER"><A NAME="Heading34"></A><FONT COLOR="#000077">The testApp GUI</FONT></H4>
<P>Because this chapter is more concerned with networking than with Abstract Windowing Toolkit (AWT) fundamentals, we won&#146;t spend a lot of time discussing them. For more information, see Chapter 4, Adding Interactivity, and Chapter 7, Creating Customizable Games with the AWT.
</P>
<P>Getting back to our testApp applet, let&#146;s add some AWT components to it right now. Figure 8-4 shows the results.</P>
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/08-04.jpg',648,264 )"><IMG SRC="images/08-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/08-04.jpg',648,264)"><FONT COLOR="#000077"><B>Figure 8-4</B></FONT></A>&nbsp;&nbsp;testApp with AWT components</P>
<!-- CODE //-->
<PRE>
Panel p = new Panel();  // This is a global variable

public void init() &#123;

HS = new HighScoreManager(10);

setLayout(new BorderLayout());  // Assign a BorderLayout to the applet

       add("South", p);  // Add Panel p to the "south" region of our &#8656;
applet
&#125;
</PRE>
<!-- END CODE //-->
<P>If you want, you can compile and run the applet at this point. You should notice two things: First, the applet has a small gray area below the drawing with nothing in it, and second, this area has obscured part of our beautiful high score artwork! Let&#146;s deal with the latter effect first. Because the &#147;southern&#148; part of our Applet panel is assigned to a different Panel, the normal drawing did not cover it. Therefore, we need to tell the HighScoreManager <I>not</I> to attempt to draw anything in the part of the Applet obscured by Panel <I>p</I>. To accomplish this, think back to when we required the applet to pass a Rectangle specifying where the drawing should take place. Because of this, our applet can now tell the HighScoreManager to only draw in a rectangle that does not contain the new Panel. We are therefore going to need to know the height of Panel <I>p</I> so we can subtract it from the overall height of the Rectangle. To do this, we use the preferredSize() method in the Panel class. By querying the <I>height</I> value of the preferredSize() method, we can obtain the height of the Panel. To put this to work for us, change this line of code:</P>
<!-- CODE SNIP //-->
<PRE>
Rectangle r= new Rectangle(0,0,size().width,size().height);
</PRE>
<!-- END CODE SNIP //-->
<P>to this:
</P>
<!-- CODE SNIP //-->
<PRE>
int panel_height = p.preferredSize().height;
Rectangle r= new Rectangle(0,0,size().width,size().height-panel_height);
</PRE>
<!-- END CODE SNIP //-->
<P>Compile and view the applet again just to prove to yourself that we have solved the problem.
</P>
<P>The other problem we discussed earlier was the matter of this really ugly gray area in our applet that does nothing. The only way we can fix this problem is by adding some functionality to that gray area. Let&#146;s start with a Button.</P>
<P>Creating the Button is easy, and we add it to the Panel the same way we added the Panel <I>p</I> to our applet. Add this line to testApp&#146;s init() method:</P>
<!-- CODE SNIP //-->
<PRE>
p.add( new Button("Change Color"));
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="293-296.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="299-301.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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