📄 097-100.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, 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=3//-->
<!--PAGES=097-100//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="093-097.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="100-102.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><B>Listing 3-3</B> RectSprite class</P>
<!-- CODE //-->
<PRE>
class RectSprite extends Sprite2D {
protected int width, height; // dimensions of rectangle
public RectSprite(int x,int y,int w,int h,Color c) {
locx = x;
locy = y;
width = w;
height = h;
color = c;
fill = false; // default: don't fill
restore(); // restore the sprite
}
// provide implementation of abstract methods:
public void update() {
// does nothing
}
// check if sprite's visible before painting
public void paint(Graphics g) {
if (visible) {
g.setColor(color);
if (fill) {
g.fillRect(locx,locy,width,height);
}
else {
g.drawRect(locx,locy,width,height);
}
}
}
}
</PRE>
<!-- END CODE //-->
<P>RectSprite’s update() method is empty, but it is no longer abstract. RectSprite’s paint() method checks the values of the inherited booleans <I>visible</I> and <I>fill</I> before displaying the rectangle. By allowing instance variables defined in the Sprite and Sprite2D superclasses to control painting and other behaviors, you lay the groundwork for consistent, predictable semantics across the entire Sprite hierarchy. For example, if r is a RectSprite instance, then invoking</P>
<!-- CODE SNIP //-->
<PRE>
r.setVisible(false); // Sprite method
</PRE>
<!-- END CODE SNIP //-->
<P>prevents r from painting, and calling
</P>
<!-- CODE SNIP //-->
<PRE>
r.setFill(true); // Sprite2D method
</PRE>
<!-- END CODE SNIP //-->
<P>causes r to paint as a solid rectangle. Other Sprite2D subclasses that implement paint() in a similar way will also function in a predictable way that is controllable by methods defined in Sprite and Sprite2D.
</P>
<P>Now let’s discuss the new Mondrian class. We are not going to actually implement it here, since it is practically the same as Listing 2-3 of the previous chapter. Instead, here is a summary of the three modifications needed to use Sprite classes.</P>
<P>First, instead of an array of DancingRect, declare an array of Sprite:</P>
<!-- CODE SNIP //-->
<PRE>
Sprite sprites[]; // sprite array
</PRE>
<!-- END CODE SNIP //-->
<P>Now modify the initRectangles() method. You might want to rename it initSprites() to initialize the <I>sprites</I> array, and instantiate the RectSprite objects. For example:</P>
<!-- CODE SNIP //-->
<PRE>
public void initSprites() {
sprites = new Sprite[NUM_SPRITES]; // init sprite array
// create RectSprite objects
sprites[0] = new RectSprite(0,0,90,90,Color.yellow);
...
}
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, you will need to use the <I>sprites</I> array in all of the new methods used in Mondrian. For example, let’s examine the loop in Mondrian’s paint() method:</P>
<!-- CODE SNIP //-->
<PRE>
for (int i=0; i<sprites.length; i++) {
sprites[i].paint(offscreen); // paint each rectangle
}
</PRE>
<!-- END CODE SNIP //-->
<P>This loop enforces a notion of priority among the sprites by painting the <I>sprites</I> array from the lowest index to the highest. Thus, a sprite of a given index will be painted later than sprites of lower indices, occluding those that occupy the same spot on the screen. The priority feature of sprites is implemented in this simple manner. For example, a sprite with index 0 will appear behind every other sprite.</P>
<P>This applet is only a simple demonstration of our Sprite classes, so here’s an easy assignment for you. Derive a BoogieRectSprite (as defined in Chapter 2, Using Objects for Animation) from RectSprite. You will only need to rename the danceStep() method of BoogieRect to update(), and the BoogieRectSprite will be ready to dance.</P>
<P>Now let’s create a fancier applet, with moving sprites, by using another abstraction that Java provides—<I>interfaces</I>.</P>
<H3><A NAME="Heading14"></A><FONT COLOR="#000077">Using Interfaces</FONT></H3>
<P>You’ve already seen an informal definition of an interface. Now you’ll see how Java lets you create interfaces to highlight relationships and similarities among classes. In this section, you will learn about Java interfaces and how they help in designing programs. Then, you will apply your knowledge in creating a Moveable interface for Sprite subclasses.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">What Is an Interface?</FONT></H4>
<P>Interfaces form a bridge between the internals of an object and external objects. More precisely, an interface is the set of method specifications that external objects can use to send messages to an object. In the nonabstract classes that you’ve seen, the set of public methods provide the public interface to objects of that class. For example, Figure 3-4 illustrates the interplay between a RectSprite’s internals, its interface, and the outside world.
</P>
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/03-04.jpg',463,316 )"><IMG SRC="images/03-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-04.jpg',463,316)"><FONT COLOR="#000077"><B>Figure 3-4</B></FONT></A> RectSprite internals, interface, and external environment</P>
<P>Interfaces highlight a key principle of software engineering: the separation of specification from implementation. The public interface, or specification, is all the outside world must know to interact with the object. No knowledge of the implementation is needed, or even desired. By separating specification from implementation, you can write code that relies on classes that have not yet been implemented. This is a powerful paradigm for creating programs, because classes can be implemented independently of one another (for example, by different programmers) once the design of the classes is settled.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="093-097.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="100-102.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -