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

📄 141-146.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:Adding Interactivity</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=4//-->
<!--PAGES=141-146//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="136-141.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="146-151.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>A simple example is shown in Listing 4-7 of an applet that loads two images, and displays them only when they&#146;re done loading.
</P>
<P><B>Listing 4-7</B> Applet to illustrate MediaTracker</P>
<!-- CODE //-->
<PRE>
// simple MediaTracker demo
/////////////////////////////////////////////////////////////////

import java.applet.*;
import java.awt.*;

public class Track extends Applet {
  MediaTracker t;
  Image i,j;

  public void init() {
    setBackground(Color.black);
    t = new MediaTracker(this);
    i = getImage(getCodeBase(),"sushi.gif");
    t.addImage(i,0);
    j = getImage(getCodeBase(),"chef.gif");
    t.addImage(j,0);
    showStatus("loading");

    // wait for all images to finish loading //
    try {
      t.waitForAll();
    }
    catch (InterruptedException e) {
    }

    // check for errors //
    if (t.isErrorAny()) {
      showStatus("error");
    }
    else if (t.checkAll()) {
      showStatus("successfully loaded");
    }

  }

  public void paint(Graphics g) {
    g.drawImage(i,13,17,this);
    g.drawImage(j,203,207,this);

  }
}
</PRE>
<!-- END CODE //-->
<P>When you run this, you will have to wait in the beginning for the images to load, but they&#146;re displayed completely once loading is done.
</P>
<P>Table 4-5 contains a list of some useful MediaTracker methods.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 4-5</B> MediaTracker methods
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="65%" ALIGN="LEFT">java.awt.MediaTracker Method
<TH WIDTH="30%" ALIGN="LEFT">Purpose
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD>public MediaTracker(Component comp);
<TD>Constructor
<TR>
<TD VALIGN="TOP">public void addImage(Image image,int id);
<TD>Loads and tracks the specified image
<TR>
<TD VALIGN="TOP">public boolean checkAll();
<TD>Returns <I>true</I> if all images are loaded
<TR>
<TD VALIGN="TOP">public boolean checkId(int id)
<TD>Returns <I>true</I> if images with the given <I>id</I> have loaded
<TR>
<TD VALIGN="TOP">public synchronized boolean isErrorAny()
<TD>Returns <I>true</I> if any errors occurred in loading
<TR>
<TD VALIGN="TOP">public void waitForAll() throws InterruptedException;
<TD>Waits for all registered images to load
<TR>
<TD VALIGN="TOP">public void waitForId(int id) throws InterruptedException;
<TD>Waits for the image with the given <I>id</I> to load
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>Now let&#146;s define the BitmapLoop sprite class.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading20"></A><FONT COLOR="#000077">Defining the BitmapLoop Class</FONT></H4>
<P>BitmapLoop will derive from BitmapSprite. Here are the definitions of Sprite and BitmapSprite, which we discussed back in Chapter 3, Animating Sprites:
</P>
<!-- CODE //-->
<PRE>
abstract class Sprite {
  protected boolean visible;           // is sprite visible
  protected boolean active;            // is sprite updateable

  // abstract methods:
  abstract void paint (Graphics g);
  abstract void update();

  // accessor methods:
  public boolean isVisible() {
    return visible;
  }

  public void setVisible(boolean b) {
    visible = b;
  }

  public boolean isActive() {
    return active;
  }

  public void setActive(boolean b) {
    active = b;
  }

  // suspend the sprite
  public void suspend() {
    setVisible(false);
    setActive(false);
  }

  // restore the sprite
  public void restore() {
    setVisible(true);
    setActive(true);
  }

}

class BitmapSprite extends Sprite {
  protected int locx;
  protected int locy;

  // image dimensions
  protected int width,height;

  protected Image image;                   // the bitmap
  protected Applet applet;                 // the parent applet

  public BitmapSprite(int x,int y,Image i,Applet a) {
    locx = x;
    locy = y;
    image = i;
    applet = a;
    restore();
  }

  public void setSize(int w,int h) {
    width = w;
    height = h;
  }

  public void update() {

    // do nothing

  }

  public void paint(Graphics g) {
    if (visible) {
      g.drawImage(image,locx,locy,applet);
    }
  }
}
</PRE>
<!-- END CODE //-->
<P>Let&#146;s see how BitmapLoop will extend BitmapSprite. Since BitmapLoop animates a sequence of images, you need to add new instance variables: <I>images</I>, which refers to the array of Images, and <I>currentImage</I>, which tracks the Image that is currently shown.</P>
<!-- CODE SNIP //-->
<PRE>
protected Image images[];       // sequence of bitmaps
protected int currentImage;     // the current bitmap
</PRE>
<!-- END CODE SNIP //-->
<P>We&#146;ll use the inherited <I>image</I> variable to store the background bitmap for the animation. This way, a BitmapLoop will behave like a BitmapSprite if it doesn&#146;t have foreground images to loop (i.e., <I>images[]</I> is empty). Finally, the boolean <I>foreground</I> tells if there are any images in the foreground loop.</P>
<!-- CODE SNIP //-->
<PRE>
protected boolean foreground;   // are there foreground images?
</PRE>
<!-- END CODE SNIP //-->
<P>Figure 4-8 illustrates what happens in a <I>BitmapLoop</I>.</P>
<P><A NAME="Fig8"></A><A HREF="javascript:displayWindow('images/04-08.jpg',562,382 )"><IMG SRC="images/04-08t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/04-08.jpg',562,382)"><FONT COLOR="#000077"><B>Figure 4-8</B></FONT></A>&nbsp;&nbsp;BitmapLoop schematic</P>
<P>Listing 4-8 shows the definition of the BitmapLoop, which also implements the Moveable interface, so we can plug it into our final applet. Of course, you can redefine the Moveable methods to provide the motion you want.
</P>
<P><B>Listing 4-8</B> BitmapLoop class</P>
<!-- CODE //-->
<PRE>
interface Moveable {
  public abstract void setPosition(int x, int y);
  public abstract void setVelocity(int x, int y);
  public abstract void updatePosition();
}

/////////////////////////////////////////////////////////////////
class BitmapLoop extends BitmapSprite implements Moveable{
  protected Image images[];       // sequence of bitmaps
  protected int currentImage;     // the current bitmap
  protected boolean foreground;   // are there foreground images?

  // constructor. Assumes that background image is already
  // loaded. (use MediaTracker)

  public BitmapLoop(int x,int y,Image b,Image f[],Applet a) {
    super(x,y,b,a);
    width = image.getWidth(a); // get size of background
    height = image.getHeight(a);

    images = f;
    currentImage = 0;
    if (images.length == 0) {
      foreground = false;          // nothing in images[]
    }
    else {
      foreground = true;
    }

  }

  // cycle currentImage if sprite is active, and there
  //   are foreground images
  public void update() {
    if (active &#38;&#38; foreground) {
      currentImage = (currentImage &#43; 1) % images.length;
    }
    updatePosition();
  }
  public void paint(Graphics g) {
    if (visible) {       // draw background first
      g.drawImage(image,locx,locy,applet);
      if (foreground) {  // now draw foreground image
      g.drawImage(images[currentImage],locx,locy,applet);
      }
    }
  }

  // implement moveable interface

  public void setPosition(int x,int y) {
    locx = x;
    locy = y;
  }
  protected int vx;
  protected int vy;

  public void setVelocity(int x,int y) {
    vx = x;
    vy = y;
  }

  // update position according to velocity
  public void updatePosition() {
    locx &#43;= vx;
    locy &#43;= vy;
    vx = 0;
    vy = 0;
  }

}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="136-141.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="146-151.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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