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

📄 107-111.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:Animating Sprites</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=3//-->
<!--PAGES=107-111//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="102-107.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="111-116.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>This should look familiar, since it&#146;s basically the same animation driver you have seen before. It&#146;s easy to incorporate new sprite types into this animation. First, define Sprite2D subclasses for any of the graphics elements supported by java.awt.Graphics, such as lines, ovals, or polygons, by following what we have done for rectangles. Then, instantiate and initialize the sprites in initSprites(), and you have your own animation!
</P>
<P>You are not limited to the drawing primitives in Java&#146;s Graphics class, of course. In the next section, you will see how you can incorporate <I>bitmaps</I> into the same animation.</P>
<H3><A NAME="Heading21"></A><FONT COLOR="#000077">Using Bitmaps</FONT></H3>
<P>In this section, you will learn about bitmaps and how they are handled in Java. Then you will create bitmap sprites that you can plug right into our standard animation applet!
</P>
<H4 ALIGN="LEFT"><A NAME="Heading22"></A><FONT COLOR="#000077">Bitmaps in Java</FONT></H4>
<P>A bitmap is an image that you can paint directly to the screen. Figure 3-6 shows a bitmap that you will use in the next video game to represent your ship.
</P>
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/03-06.jpg',115,115 )"><IMG SRC="images/03-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-06.jpg',115,115)"><FONT COLOR="#000077"><B>Figure 3-6</B></FONT></A>&nbsp;&nbsp;Ship bitmap</P>
<P>You can create bitmaps by using the many paint programs that are written for your computer. For example, Macintosh users can use MacPaint, UNIX users might try xpaint, PC users can use the Paint tool that comes with Windows. When you are creating bitmaps to use in Java applets, save them as GIF files, since installations of Java will definitely use this format. Other common formats, such as BMP or TIFF, might not be understandable to the particular system.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">Loading and Drawing a Bitmap Image</FONT></H4>
<P>There are only three steps needed to load and draw a bitmap, or Image, as Java calls it.
</P>
<DL>
<DD><B>1.</B>&nbsp;&nbsp;First, import the java.awt.Image class, by using one of the following:
<!-- CODE SNIP //-->
<PRE>
import java.awt.Image;
</PRE>
<!-- END CODE SNIP //-->
<BR>or
<!-- CODE SNIP //-->
<PRE>
import java.awt.*;
</PRE>
<!-- END CODE SNIP //-->
<DD><B>2.</B>&nbsp;&nbsp;Load and create an Image object with the Applet method getImage(). The following section will explain how to specify the correct location of the image.
<DD><B>3.</B>&nbsp;&nbsp;Now you&#146;re ready to draw the image. Use the drawImage() method defined in the Graphics class:
<!-- CODE SNIP //-->
<PRE>
public void paint(Graphics g) &#123;
  g.drawImage(alien,13,17,this); // draw alien image at screen
                                 // location (13,17)
&#125;
</PRE>
<!-- END CODE SNIP //-->
</DL>
<P>If this looks familiar, it should! The offscreen image used in double-buffering is drawn to the screen in the same way. The upper-left-hand corner of the bitmap is placed at the x and y coordinates specified in drawImage().
</P>
<P>Another version of the drawImage() method allows you to scale the bitmap to the desired proportions:</P>
<!-- CODE SNIP //-->
<PRE>
// scale image to the specified width and height,
//   and draw it at (x,y)
g.drawImage(image,x,y,width,height,this);
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">Specifying the Location of a Bitmap Image</FONT></H4>
<P>Since your applet could be running anywhere across the Internet, you need to tell the applet where to find the right image. There are three ways:
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Use the URL of the image file. If the bitmap is located at a URL, such as <A HREF="http://www.somewhere.com/images/alien.gif">http://www.somewhere.com/images/alien.gif</A>, you can load the image into your applet by using the following syntax:
<!-- CODE SNIP //-->
<PRE>
Image alien = getImage(
  new URL("http://www.somewhere.com/images/alien.gif"));
</PRE>
<!-- END CODE SNIP //-->
</DL>
<P>This works by creating a URL object with the desired location, and loading the image found there. In general, however, you should avoid hardcoding URLs, because your applets will break if you move the image to a different location. As a result, the next options are preferable.
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Find the Image relative to the HTML document. Let&#146;s say, for instance, that the HTML document that includes your game applet is at <A HREF="http://www.somewhere.com/game/game.html">http://www.somewhere.com/game/game.html</A>. Then, if the image is in the same directory as game.html, use the following syntax:
<!-- CODE SNIP //-->
<PRE>
Image alien = getImage(getDocumentBase(),"alien.gif");
</PRE>
<!-- END CODE SNIP //-->
</DL>
<P>If the subdirectory &#147;bitmaps&#148; is in the same place as game.html, and alien.gif is found in &#147;bitmaps,&#148; use
</P>
<!-- CODE SNIP //-->
<PRE>
Image alien = getImage(getDocumentBase(),"bitmaps/alien.gif");
</PRE>
<!-- END CODE SNIP //-->
<P>The last option is similar.
</P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;Find the Image relative to the applet class. If the image is in the same directory as the applet class, then use
<!-- CODE SNIP //-->
<PRE>
Image alien = getImage(getCodeBase(),"alien.gif");
</PRE>
<!-- END CODE SNIP //-->
<DD><B>&#149;</B>&nbsp;&nbsp;You can find images in subdirectories, as in the previous case.
</DL>
<P>Now you&#146;re ready to create bitmap sprites.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading25"></A><FONT COLOR="#000077">Creating Bitmap Sprites</FONT></H4>
<P>Bitmap sprites are completely different from the sprites you have already seen, which are subclassed from Sprite2D, so let&#146;s derive, in Listing 3-7, BitmapSprite from the root Sprite class.
</P>
<P><B>Listing 3-7</B> BitmapSprite class</P>
<!-- CODE //-->
<PRE>
class BitmapSprite extends Sprite &#123;
  protected int locx;
  protected int locy;

  // image dimensions
  protected int width,height;

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

  public BitmapSprite(int x,int y,Image i,Applet a) &#123;
    locx = x;
    locy = y;
    image = i;
    applet = a;
    restore();
  &#125;

  // set the size of the bitmap
  public void setSize(int w,int h) &#123;
    width = w;
    height = h;

  &#125;

  public void update() &#123;

    // do nothing

  &#125;

  public void paint(Graphics g) &#123;
    if (visible) &#123;
      g.drawImage(image,locx,locy,applet);
    &#125;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>This follows the basic outline used for RectSprite. The instance variables <I>locx</I> and <I>locy</I> track where the bitmap should be painted, and <I>image</I> points to the bitmap itself. The variable <I>applet</I> is needed to call the drawImage() method, and it refers to the applet that the BitmapSprite object is in.</P>
<P>Now let&#146;s create a BouncingBitmap sprite. This class derives from BitmapSprite, and implements the Moveable interface. It resembles the BouncingRect class you saw earlier. The definition of the BouncingBitmap is shown in Listing 3-8.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="102-107.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="111-116.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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