📄 079-082.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:Using Objects for Animations</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=2//-->
<!--PAGES=079-082//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="075-079.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch03/083-087.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><B>Listing 2-7</B> Woogie.java</P>
<!-- CODE //-->
<PRE>
import java.applet.*;
import java.awt.*;
// run this applet with width=300 height=300
public class Woogie extends Applet implements Runnable {
Thread animation;
Graphics offscreen;
Image image;
static final int NUM_RECTS = 9; // in ms
static final int REFRESH_RATE = 100; // in ms
DancingRect r[];
public void init() {
System.out.println(">> init <<");
setBackground(Color.black);
initRectangles();
image = createImage(300,300);
offscreen = image.getGraphics();
}
public void initRectangles() {
// allocate dancing rectangles
r = new DancingRect[NUM_RECTS];
r[0] = new DancingRect(0,0,90,90,Color.yellow);
r[1] = new BoogieRect(250,0,40,190,Color.yellow);
r[2] = new WaltzRect(200,55,60,135,Color.yellow);
r[3] = new BoogieRect(80,200,220,90,Color.blue);
r[4] = new WaltzRect(100,10,90,80,Color.blue);
r[5] = new BoogieRect(80,100,110,90,Color.lightGray);
r[6] = new WaltzRect(200,0,45,45,Color.red);
r[7] = new WaltzRect(0,100,70,200,Color.red);
r[8] = new BoogieRect(200,55,60,135,Color.magenta);
}
public void start() {
System.out.println(">> start <<");
animation = new Thread(this);
if (animation != null) {
animation.start();
}
}
// update each rectangle's position.
// DYNAMIC METHOD BINDING OCCURS HERE!
public void updateRectangles() {
for (int i=0; i<NUM_RECTS; i++) {
r[i].danceStep(); // each rectangles dance step
}
}
// override update so it doesn't erase screen
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
offscreen.setColor(Color.black);
offscreen.fillRect(0,0,300,300); // clear buffer
for (int i=0; i<NUM_RECTS; i++) {
r[i].paint(offscreen); // paint each rectangle
}
g.drawImage(image,0,0,this);
}
public void run() {
while (true) {
repaint();
updateRectangles();
try {
Thread.sleep (REFRESH_RATE);
} catch (Exception exc) { };
}
}
public void stop() {
System.out.println(">> stop <<");
if (animation != null) {
animation.stop();
animation = null;
}
}
}
</PRE>
<!-- END CODE //-->
<H3><A NAME="Heading28"></A><FONT COLOR="#000077">Suggestion Box</FONT></H3>
<DL>
<DD><B>•</B> Create new types of dancing rectangles, and add them to the Woogie applet. Try a ChaChaChaRect. How would you implement the delay between the dance steps? One solution is to bump an internal counter each time danceStep() is called; when the counter reaches a certain value, update the rectangle’s position.
<DD><B>•</B> Change the width and height of the rectangles as part of the danceStep().
<DD><B>•</B> Add new shapes to Woogie, such as Ovals or Arcs. Can you think of a good way to alter the inheritance hierarchy to easily allow new shapes? The answer is in the next chapter, but here’s a hint: You might want to create a superclass of DancingRect, and move some functionality of DancingRect to the superclass.
<DD><B>•</B> Make a gravity simulation of bouncing rectangles. This will look cool, and it just takes a new formula in the danceStep() routine!
<DD><B>•</B> Right now, the coordinates used to define new rectangles are hardcoded into Woogie. Use the Applet method bounds() (which returns the dimensions of the applet) to compute the coordinates of the rectangles, so that they adjust automatically to the applet size.
</DL>
<H3><A NAME="Heading29"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>As usual, this chapter’s chock-full of information that you’re going to need in writing a video game. You’ve learned how to create animations in Java by using the Universal Animation Loop, and that the applet methods you override execute in conjunction with the surrounding environment. You’ve seen how to use double-buffering to improve the quality and performance of your animations.
</P>
<P>Finally, you learned about three cornerstones of an object-oriented language such as Java:</P>
<DL>
<DD><B>•</B> Objects
<DD><B>•</B> Inheritance
<DD><B>•</B> Subtyping with dynamic method binding
</DL>
<P>These are important keys to creating animations, games, and other applications in Java.
</P>
<P>In the following chapter, you’re going to learn about sprite and bitmap animation.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="075-079.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch03/083-087.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -