📄 510-514.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:Building the JAVAroids Game</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=13//-->
<!--PAGES=510-514//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="503-510.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="514-519.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">Choosing a Parent for the PolygonSprite Class</FONT></H4>
<P>What is the best parent class for a PolygonSprite? This would be the most specific class in our Sprite hierarchy that provides functionality that we can build upon. We’re not going to use bitmaps to represent the polygons, so the BitmapSprite or BitmapLoop provides unnecessary functionality. On the other hand, a RectSprite displays rectangles, so it’s a bit too specific. The best choice of parent class for a PolygonSprite is Sprite2D, which encapsulates sprites that rely on methods defined in the Graphics class. Listing 13-1 shows the definition of Sprite2D and its parent, Sprite.
</P>
<P><B>Listing 13-1</B> Sprite and Sprite2D classes</P>
<!-- CODE //-->
<PRE>
abstract class Sprite {
protected boolean visible; // is sprite visible
protected boolean active; // is sprite updatable
// 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);
}
}
abstract class Sprite2D extends Sprite {
protected int locx; // x location
protected int locy; // y location
protected Color color; // the color of sprite
protected boolean fill; // filled or not
public boolean getFill() {
return fill;
}
public void setFill(boolean b) {
fill = b;
}
public void setColor(Color c) {
color = c;
}
public Color getColor() {
return color;
}
}
</PRE>
<!-- END CODE //-->
<P>After deriving PolygonSprite, we’ll subclass MoveablePolygon to represent the firing and the enemy ships, and RotateablePolygon to display the player’s ship and the asteroids.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Defining PolygonSprite</FONT></H4>
<P>To implement PolygonSprite, we need some way of representing a polygon. Fortunately, java.awt provides a Polygon class that’s convenient to use.
</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="100%" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>java.awt.Polygon</B></FONT>
<P>Variables
</P>
<DL>
<DT><B><I>npoints</I>:
</B>
<DD>The number of points in the polygon
<DT><B><I>xpoints</I>:
</B>
<DD>The array of x coordinates
<DT><B><I>ypoints</I>:
</B>
<DD>The array of y coordinates
</DL>
<P>Selected Methods
</P>
<DL>
<DT><B>Polygon():</B>
<DD>Creates an empty polygon
<DT><B>Polygon(int xpoints[], int ypoints[], int npoints):</B>
<DD>Creates and initializes a polygon with the specified parameters
<DT><B>addPoint(int x, int y):</B>
<DD>Appends point (x,y) to polygon
</DL>
</TABLE>
<P>To paint a Polygon <I>p</I> in Graphics context <I>g</I>, use</P>
<!-- CODE SNIP //-->
<PRE>
g.drawPolygon(p);
</PRE>
<!-- END CODE SNIP //-->
<P>or
</P>
<!-- CODE SNIP //-->
<PRE>
g.fillPolygon(p);
</PRE>
<!-- END CODE SNIP //-->
<P>Of course, it’s possible to draw a polygon by using a sequence of g.drawLine() commands, but it’s definitely faster to use the polygon-draw from the Graphics class. Let’s use java.awt.Polygon to help us with the PolygonSprite class, shown in Listing 13-2.
</P>
<P><B>Listing 13-2</B> PolygonSprite class</P>
<!-- CODE //-->
<PRE>
/////////////////////////////////////////////////////////////////
//
// PolygonSprite class: encapsulates polygon, position
// it appears, visibility
//
/////////////////////////////////////////////////////////////////
class PolygonSprite extends Sprite2D {
protected Polygon p;
// methods:
/////////////////////////////////////////////////////////////////
// constructor: take absolute coords
/////////////////////////////////////////////////////////////////
public PolygonSprite(int x[], int y[], int n, Color c) {
p = new Polygon(x,y,n);
color = c;
visible = true;
fill = false; // don't fill polygon
locx = locy = 0;
}
/////////////////////////////////////////////////////////////////
// add point to the polygon
/////////////////////////////////////////////////////////////////
public void addPoint(int x, int y) {
p.addPoint(x,y);
}
/////////////////////////////////////////////////////////////////
// paint polygon based on variables from Sprite and Sprite2D
/////////////////////////////////////////////////////////////////
public void paint(Graphics g) {
if (visible) {
g.setColor(color);
if (fill)
g.fillPolygon(p);
else
g.drawPolygon(p);
}
}
/////////////////////////////////////////////////////////////////
// no update operation
/////////////////////////////////////////////////////////////////
public void update() {
}
}
</PRE>
<!-- END CODE //-->
<P>As you see, it’s pretty straightforward! Now the MoveablePolygon can be derived from PolygonSprite. In Listing 13-3, we’ll use the Moveable interface (defined in Chapter 3, Animating Sprites), which specifies methods to move a Spriteclass.
</P>
<P><B>Listing 13-3</B> Moveable interface</P>
<!-- CODE SNIP //-->
<PRE>
public interface Moveable {
public abstract void setPosition(int x, int y);
public abstract void setVelocity(int x, int y);
public abstract void updatePosition();
}
</PRE>
<!-- END CODE SNIP //-->
<P>Remember that a class implements an interface by providing the code for methods specified in the interface. One way of moving the polygon (as required by updatePosition()) is to store the polygon in <I>local</I> coordinates, and compute the <I>screen</I> coordinates before painting. Using a local coordinate system is also going to make rotating a polygon really easy, since we can store the points in <I>polar</I> coordinates. Did you catch all that? If not, here’s a little review...</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="503-510.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="514-519.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -