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

📄 363-368.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=363-368//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="358-363.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="368-373.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">Creating a Simple Multithreaded Animation</FONT></H4>
<P>Let&#146;s create a multithreaded animation applet. Although this example is simple, it&#146;ll highlight the differences of programming with threads.
</P>
<P>First of all, let&#146;s define an abstract class, Actor, that specifies the essential methods and variables of objects that move independently on the screen. This class is shown in Listing 10-1.</P>
<P><B>Listing 10-1</B> Actor class</P>
<!-- CODE SNIP //-->
<PRE>
abstract class Actor extends Thread &#123;
  Applet applet;
  protected boolean drawNow;
  abstract void paint(Graphics g);
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>Each Actor object is a separate thread that runs independently. When it needs to paint itself (for example, after a change in color or position), it will set <I>drawNow</I> to <I>true</I>, and notify the mother applet. For example, here is the run() method of an Actor subclass, called CycleColorActor, which paints a rectangle that changes colors every few milliseconds:</P>
<!-- CODE //-->
<PRE>
public void run() &#123;
  while (true) &#123;
    drawNow = true;
    applet.repaint();
    updateColor();
    try &#123;
     sleep(delay);
    &#125;
    catch (InterruptedException e) &#123;
    &#125;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>The repaint() method of the applet eventually triggers a call to each Actor&#146;s individual paint() method. Here&#146;s the paint() of the CycleColorActor:
</P>
<!-- CODE SNIP //-->
<PRE>
public void paint(Graphics g) &#123;
  if (drawNow) &#123;
    g.setColor(new Color(red,green,blue));
    g.fillRect(locx,locy,73,73);
    drawNow = false;
  &#125;
&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>As you see, only those Actors that need to paint will draw themselves.
</P>
<P>The full definition of the CycleColorActor is shown in Listing 10-2. The constructor allows you to specify the location of the rectangle, the delay, and the starting (<I>r,g,b</I>) triple.</P>
<P><B>Listing 10-2</B> CycleColorActor class</P>
<!-- CODE //-->
<PRE>
class CycleColorActor extends Actor &#123;
  protected int locx,locy;        // location
  protected int red,green,blue;   // color
  protected int delay;            // delay in ms

  public CycleColorActor(Applet a,int x,int y,
               int r,int g,int b,int d) &#123;
    // initialize inherited variables
    applet = a;
    drawNow = false;

    // initialize other variables
    locx = x;
    locy = y;
    red = r;
    green = g;
    blue = b;
    delay = d;
    start();            // start thread running!
  &#125;

  // override run() -- each thread executes this
  //    when it's alive
  public void run() &#123;
    while (true) &#123;
      drawNow = true;
      applet.repaint();
      updateColor();
      try &#123;
       sleep(delay);
      &#125;
      catch (InterruptedException e) &#123;
      &#125;
    &#125;
  &#125;

  public void updateColor() &#123;
    red = (red &#43; 4) % 256;
    green = (green &#43; 4) % 256;
    blue = (blue &#43; 4) % 256;
  &#125;

  // provide implementation of abstract methods:

  public void paint(Graphics g) &#123;
    if (drawNow) &#123;
      g.setColor(new Color(red,green,blue));
      g.fillRect(locx,locy,73,73);
      drawNow = false;
    &#125;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>Finally, Listing 10-3 shows the applet (ActorTest) that will define three Actors and set them running. Figure 10-5 shows how this applet works in conjunction with the Actors.
</P>
<P><A NAME="Fig5"></A><A HREF="javascript:displayWindow('images/10-05.jpg',600,408 )"><IMG SRC="images/10-05t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/10-05.jpg',600,408)"><FONT COLOR="#000077"><B>Figure 10-5</B></FONT></A>&nbsp;&nbsp;ActorTest applet execution</P>
<P>ActorTest doesn&#146;t implement Runnable, as our previous animation applets did. Study the following code so you understand how things work!
</P>
<P><B>Listing 10-3</B> ActorTest applet</P>
<!-- CODE //-->
<PRE>
import java.applet.*;
import java.awt.*;

public class ActorTest extends Applet &#123;

  Actor actors[] = new Actor[4];

  public void init() &#123;
    int width = size().width;
    int height = size().height;
    setBackground(Color.black);
    // define 4 Actors:
    // this changes color every 50 ms
    actors[0] = new CycleColorActor(this,0,0,
                            90,150,0,50);
    // this changes color every 100 ms
    actors[1] = new CycleColorActor(this,width-73,0,
                            90,150,0,100);
    // this changes color every 200 ms
    actors[2] = new CycleColorActor(this,0,height-73,
                            90,150,0,200);
    // this changes color every 400 ms
    actors[3] = new CycleColorActor(this,width-73,height-73,
                            90,150,0,400);

  &#125;

  // override update so it doesn't erase screen
  public void update(Graphics g) &#123;
    paint(g);
  &#125;

  public void paint(Graphics g) &#123;

    for (int i=0; i&lt;actors.length; i&#43;&#43;) &#123;
      actors[i].paint(g);     // paint each rectangle
    &#125;

  &#125;

&#125;
</PRE>
<!-- END CODE //-->
<P>Next, let&#146;s see how you can structure games with threads. The ActorTest applet contains the basic idea.
</P>
<H3><A NAME="Heading13"></A><FONT COLOR="#000077">Structuring Games with Multiple Threads</FONT></H3>
<P>First, let&#146;s review how we&#146;ve written games without using multiple threads. The key to our previous approach is the Video Game Loop, diagrammed in Figure 10-6.
</P>
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/10-06.jpg',497,496 )"><IMG SRC="images/10-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/10-06.jpg',497,496)"><FONT COLOR="#000077"><B>Figure 10-6</B></FONT></A>&nbsp;&nbsp;The Video Game Loop revisited</P>
<P>In this scheme, the run() loop contained in the applet acts as the master clock, telling each of the classes used in the game when to update and paint.
</P>
<P>With multiple threads, a different approach is possible. Each Actor in the game will update at an individual rate, based on its own internal clock. When it&#146;s ready to paint, it notifies the mother applet. The mother applet passes the graphics context to the individual Actors, which paint if necessary. This scheme is diagrammed in Figure 10-7.</P>
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/10-07.jpg',600,747 )"><IMG SRC="images/10-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/10-07.jpg',600,747)"><FONT COLOR="#000077"><B>Figure 10-7</B></FONT></A>&nbsp;&nbsp;Multithreaded Video Game structure<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="358-363.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="368-373.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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