📄 291-293.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, 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=8//-->
<!--PAGES=291-293//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="287-290.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="293-296.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading26"></A><FONT COLOR="#000077">Creating the HighScoreManager Class</FONT></H3>
<P>Now it’s time to write the object that will plug into the client applet: the HighScoreManager class. The first thing we must do is create a file called HighScoreManager.java. In it, place the basic code for initializing a new class:
</P>
<!-- CODE //-->
<PRE>
import java.awt.*; // These are all java components we will eventually ⇐
need
import java.util.*;
import java.lang.*;
public class HighScoreManager extends Object {
HighScoreManager() { // The object initialization routine - does nothing⇐
for now
}
}
</PRE>
<!-- END CODE //-->
<P>In order to test this class without being connected to a server (and without having written the server software yet), let’s add a class that pretends it retrieved some data from a server:
</P>
<!-- CODE SNIP //-->
<PRE>
String getScoreData() {
return "Eric & 1000 | Dave & 200 | Jane & 0 | Mary & 24235";
}
</PRE>
<!-- END CODE SNIP //-->
<P>If we had really obtained a server connection, this is an example of the type of data we might receive. Recall that our data will be in the form “Name & score | Name & score.” Using relatively rare characters like “&” and “|” to separate our data makes it easier to parse. Later on, the getScoreData() method will actually do some work. Another thing we should do is get a new HighScoreList. We can make a new one in the initialization routine for HighScoreManager:
</P>
<!-- CODE SNIP //-->
<PRE>
HighScoreManager(int max) {
NUM_SCORES=max;
L= new HighScoreList(NUM_SCORES);
}
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading27"></A><FONT COLOR="#000077">The getScores() Method</FONT></H4>
<P>Now we want to write a method that will take the data from getScoreData() and add it to the list. Here is the code for the getScores() method:
</P>
<!-- CODE //-->
<PRE>
void getScores() {
String str;
int x=0;
str=getScoreData();
if (str==null || str == "none") return ;
/* If there are no scores to parse, we're done */
L = new HighScoreList(NUM_SCORES, str);
}
</PRE>
<!-- END CODE //-->
<P>Notice how the HighScoreList really does all the work, and all we have to do is send it the data! Finally, it’s time to start providing some methods for interacting with the user. The next method is one that will be called (eventually) from our applet’s paint() method.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading28"></A><FONT COLOR="#000077">The paintScores() Method</FONT></H4>
<P>The paintScores() method will be passed a Graphics context and will be expected to draw the high scores on it. Chief among design considerations for this method is the fact that the high score drawing area may be a rectangle of any size. However, the applet may not want us to draw on the entire rectangle available to us. The Graphics context may be “clipped” to a smaller region—we must account for this. The paintScores() method is passed two arguments: the Graphics context <I>g</I>, and a Rectangle <I>r</I>. The Rectangle defines the total area available for us to draw the scores onto. Here are some Java methods we will use:</P>
<!-- CODE //-->
<PRE>
new Rectangle (int x, int y, int width, int height);
/* create a new Rectangle at (x,y) */
r.intersects (Rectangle);
/* Return true if the two rectangles intersect */
Graphics g.getClipRect();
/* Returns the current "clipping area" of the Graphics context */
fillRect( int x, int y, int width, int height);
/* Fills a rectangle with the current color */
</PRE>
<!-- END CODE //-->
<P>Some other methods you will need have to do with Colors and Fonts. These are two more built-in classes in Java.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading29"></A><FONT COLOR="#000077">Methods for Creating Colors</FONT></H4>
<P>To create a color, you can access it by name (for instance, Color.blue) or create a new color based on red, green, and blue values. The RGB scale uses three integers to represent colors. To create a really neat effect, we are going to divide the screen into a random number (3–53) of even rectangles. We will choose a color for the first rectangle, and then slowly “dissolve” the blue out of each successive rectangle until the last rectangle has no blue in it at all. This will provide us with a very snazzy backdrop for our high scores! Here is the first half of the paintScores() code that handles the background color dissolve:
</P>
<!-- CODE //-->
<PRE>
void paintScores(Graphics g, Rectangle r) {
int x;
int b=255;
for(x=r.x ; x < r.width + r.x ;x += (int)(r.width/num)) {
b-=(int)(255/num);
if (b<0) b=0;
g.setColor(new Color( red , green , b));
if (g.getClipRect().intersects( new Rectangle(x,r.y,r.width,r.height)))
g.fillRect ( x , r.y , r.width , r.height);
}
</PRE>
<!-- END CODE //-->
<P>Of course, this code does not create or initialize the variables <I>red</I>, <I>green</I>, and <I>num</I>. For that, we need to declare three more global ints, and create a simple function for choosing random values:</P>
<!-- CODE //-->
<PRE>
private int red,green,num;
...
public void newColors() {
red=(int)(Math.random()*200)+56 ;
green= (int)(Math.random()*200)+56;
num= (int)(Math.random()*50)+2;
}
</PRE>
<!-- END CODE //-->
<P>We should also add a call to HighScoreManager() (our init routine, remember?) that calls newColors(). Also, notice that <I>red</I>, <I>green</I>, and <I>num</I> are all <I>private</I> variables. This means that they are not accessible by any class outside the HighScoreManager. However, the newColors() routine is public, so our applet can choose to change the background color.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="287-290.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="293-296.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -