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

📄 452-455.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:Building 3D Applets with App3Dcore</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=12//-->
<!--PAGES=452-455//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="450-452.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="455-458.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">The initiateClasses() Method</FONT></H4>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;This method sees to it that all classes used in this world are initiated. It also retrieves a parameter, the gravity, from the bouncing boxes applet file (see Listing 12-3).
</DL>
<P><B>Listing 12-3</B> The bouncing boxes applet</P>
<!-- CODE //-->
<PRE>
public class BouncingBoxes extends NoFlickerApplet implements Runnable &#123;
   //-- the world and the camera
   BouncingBoxWorld world;
   fCamera camera;

   //-- standard-fare applet stuff
   Thread myThread;
   boolean alive;

   public void init () &#123;
      //-- construct the world
      world=new BouncingBoxWorld(this);
      //-- construct the camera
      double viewangle=Math.PI/3;
      double viewdist=100;
      fPoint3d campos=new fPoint3d(0,15,30);
      fAngle3d camagl=new fAngle3d(0,0,0);
      double gridsize=15;
      double fading=0;
      camera=new fMagicCamera(world,viewangle,viewdist,campos,
                              camagl,gridsize,fading);

      //-- applet stuff
      myThread=new Thread(this);
      myThread.start();
      alive=true;
   &#125;

   protected long lastUpdate;

   public void run () &#123;
      lastUpdate=System.currentTimeMillis();
      while(alive)&#123;
         long currentTime=System.currentTimeMillis();
         long dtmillis=currentTime-lastUpdate;
         double dt=(double)dtmillis/1000;
           lastUpdate=currentTime;
           //-- make sure the update doesn't take to large "steps"
         if(dt&gt;0.2) dt=0.2;

         world.update(dt);
         camera.update(dt);

         //-- to speed up the repaint we tell the Applet to update
         //-- directly instead of calling repaint
         if(getPeer()==null) return;
         Graphics g=getGraphics();
         if(g==null) return;
         Dimension dim=size();
         if(dim.width==0 || dim.height==0) return;
         update(g);
      &#125;
   &#125;

   public void start () &#123;
      if(myThread==null)&#123;
         myThread=new Thread(this);
         myThread.start();
         alive=true;
      &#125;
   &#125;


   public void paint(Graphics g)&#123;
      //-- update the screen size for the camera
      camera.setScreenSize(size().width,size().height);
      //-- erase the screen
      g.clearRect(0,0,size().width,size().height);
      //-- paint the world
      camera.paint(g);
   &#125;

   public void stop () &#123;
      if(myThread!=null)&#123;
         myThread.stop();
         myThread=null;
         alive=false;
      &#125;
   &#125;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">The init() Method</FONT></H4>
<P>The first line of code constructs a bouncing boxes world. After that, a magic camera is constructed, through which we will view the world.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">The run() Method</FONT></H4>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;In the run() method we keep track of how long a frame takes to render. If a frame takes more than 0.2 seconds to render, then the hardware is too slow or the OS is too busy, so we set the update time to 0.2 seconds.
<DD><B>&#149;</B>&nbsp;&nbsp;The world and the camera are updated.
<DD><B>&#149;</B>&nbsp;&nbsp;The next thing we would like to do is paint the world as seen through the camera. To accelerate the rendering, we will ignore the repaint() request and simply tell Java&#146;s AWT to paint. This method will drastically increase the rendering speed but can also cause problems due to the inner workings of Java. To insure that no major malfunction occurs, a couple of checks are done: Is there an actual window to paint on? If so, is the window real or a bogus window with no dimension?
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading15"></A><FONT COLOR="#000077">The paint() Method</FONT></H4>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;In the paint() method we ensure that the camera has the right screen dimensions and then tell it to paint.
</DL>
<P>As you can see in Listing 12-4, the HTML file contains quite a few parameters that can be changed to customize the applet. You can experiment by changing the <I>ourDefaultPolyhedron</I> parameter, telling the bouncing box to use some other 3D model, or you can change the gravity of the world if you like.</P>
<P><B>Listing 12-4</B> The HTML file</P>
<!-- CODE //-->
<PRE>
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Bouncing Boxes 96 - Calin Tenitchi&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;center&gt;&lt;HR&gt;
&lt;applet code="BouncingBoxes.class" width=400 height=400&gt;
&lt;param name=BouncingBox_ourDefaultPolyhedron value=cube.f3d&gt;
&lt;param name=BouncingBox_ourBounciness        value=0.6&gt;
&lt;param name=BouncingBox_scalex               value=2&gt;
&lt;param name=BouncingBox_scaley               value=4&gt;
&lt;param name=BouncingBox_scalez               value=1&gt;
&lt;param name=BouncingBoxWorld_gravity         value=9.82&gt;
&lt;/applet&gt;
&lt;hr&gt;&lt;/center&gt;
Bouncing boxes by Calin Tenitchi
</PRE>
<!-- END CODE //-->
<P>As you may have guessed, the HTML file for the game will be VERY large, with hundreds of parameters that can be changed at will, changing the behavior of the game without any additional coding.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="450-452.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="455-458.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

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