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

📄 299-301.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=299-301//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="296-299.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="301-303.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>This adds a Button with the label Change Color to the Panel <I>p</I>. Since we didn&#146;t specify a Layout for Panel <I>p</I>, the Button is, by default, placed in the center. If you want, compile and display testApp and click away! Unfortunately, this doesn&#146;t do very much. Although some users may find a bogus button amusing, it won&#146;t entertain them for long. In order to add some functionality to the Button, we have to intercept the event message that it sends to the applet. Java provides us with many, many ways of dealing with events, but for this we are going to use the action() method that is built into the Applet class, just for this purpose. The action() method is declared like this:</P>
<!-- CODE SNIP //-->
<PRE>
public boolean action(Event evt, Object arg) &#123;
</PRE>
<!-- END CODE SNIP //-->
<P>Different AWT components cause different types of arguments to be passed to an Applet. Buttons pass String objects with the name of the Button embedded in them. All we have to do, then, is to check and see if the String we were passed matches a String we were expecting, and, if so, take the proper action (in this case, change the color and repaint). Always remember that in event-driven methods we must always return <I>true</I> if we handled the specified event, or <I>false</I> if we did not. This is important because it allows the event to continue to be processed in another method if it is not the one we are looking for. The code looks like this:</P>
<!-- CODE //-->
<PRE>
public boolean action(Event evt, Object arg) &#123;

       if("Change Color".equals(arg)) &#123;
              HS.newColors();
              repaint();
       return true;
       &#125;
       return false;
    &#125;
</PRE>
<!-- END CODE //-->
<P>The AWT classes are so powerful precisely because they allow you to add a great deal of functionality with very little effort. Compile again, and enjoy all the pretty colors! When you&#146;re done, we can proceed on to some really useful stuff. This time, we are going to add a TextField object. A TextField is a class of objects designed to allow the user to input/edit a single line of text. We are going to now allow the user to input a name to be added to the high scores list. Since we aren&#146;t really playing a game, we will just assign the name a random-number &#147;score&#148; and then let our HighScoreManager handle it. First, we need a global TextField, which we will call <I>F</I>. To initialize a TextField, you have to specify the number of columns of text it will hold. We will start with 10 for now, since the user will probably not need more than that (and even if they do, the TextField scrolls!). We will also want a button that will allow the user to add the name currently in the TextField to the high scores list. Here&#146;s how it&#146;s done:</P>
<!-- CODE //-->
<PRE>
TextField F; // Global
...
F = new TextField(10);  // Add these to init()
p.add(F);
...
// Add this to action() :
       if ("Add".equals(arg)) &#123;
              HS.addScore(F.getText(),(int)(Math.random() * 1000));
              F.setText(null);
              repaint();
          return true;
       &#125;
</PRE>
<!-- END CODE //-->
<P>Congratulations! Our testApp is done! Compile and see for yourself. Type in some text, press the Add button, and, voil&#224;! Instant additions.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading35"></A><FONT COLOR="#000077">Threading HighScoreManager</FONT></H4>
<P>We are almost ready to start writing the network-related code. However, we must first arrange to have HighScoreManager check periodically for new scores. To do this we must use Threads.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading36"></A><FONT COLOR="#000077">Using Threads</FONT></H4>
<P>Much of the hype surrounding Java and many of today&#146;s newest operating systems is that they are <I>multithreaded</I>. This is yet another computer jargon term designed to intimidate you into submission. Don&#146;t worry, the concept is actually pretty simple. A <I>thread</I> is like a program in and of itself. The neat thing about multithreading is that you can have multiple tasks going on at once. Way back in the old days of DOS, you could only have one program&#151;or thread&#151;running at a time. Now you can have as many threads as your computer can handle, all operating concurrently.</P>
<P>In order to periodically check for new scores from the server, we are going to have to have a loop that runs continuously as long as the program is alive. Instead of having our program loop infinitely and freeze up the system forever (which might make our user unhappy), we create a thread to do this in the background. The thread will check for new scores, get them, and then <I>sleep</I>. While the thread is asleep, it does not use up precious system resources, and it allows the rest of the computer to run unhindered.</P>
<P>Luckily for us, making our HighScoreManager a thread is not too hard. In Java there are two ways to use threads: you can either <I>extendThread</I>, as we have already done with Object and Applet, or you can <I>implement</I> <I>Runnable</I>, which is an <I>interface</I>.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="296-299.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="301-303.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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