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

📄 tij0187.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  <font color="#0000ff">public</font> Point position() { 
    <font color="#0000ff">return</font> <font color="#0000ff">new</font> Point(x, y);
  }
  <font color="#009900">// Beasts know how to draw themselves:</font>
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> draw(Graphics g) {
    g.setColor(color);
    <font color="#0000ff">int</font> directionInDegrees = (<font color="#0000ff">int</font>)(
      (currentDirection * 360) / (2 * Math.PI));
    <font color="#0000ff">int</font> startAngle = directionInDegrees - 
      FieldOBeasts.halfFieldOfView;
    <font color="#0000ff">int</font> endAngle = 90;
    g.fillArc(x, y, GSIZE, GSIZE, 
      startAngle, endAngle);
  }
}

<font color="#0000ff">public</font> <font color="#0000ff">class</font> FieldOBeasts <font color="#0000ff">extends</font> Applet 
    <font color="#0000ff">implements</font> Runnable {
  <font color="#0000ff">private</font> Vector beasts;
  <font color="#0000ff">static</font> <font color="#0000ff">float</font> 
    fieldOfView = 
      (<font color="#0000ff">float</font>) (Math.PI / 4), <font color="#009900">// In radians</font>
    <font color="#009900">// Deceleration % per second:</font>
    decayRate = 1.0f, 
    minimumDistance = 10f; <font color="#009900">// In pixels</font>
  <font color="#0000ff">static</font> <font color="#0000ff">int</font>
    halfFieldOfView = (<font color="#0000ff">int</font>)(
      (fieldOfView * 360) / (2 * Math.PI)),
    xExtent = 0,
    yExtent = 0,
    numBeasts = 50,
    maxSpeed = 20; <font color="#009900">// Pixels/second</font>
  <font color="#0000ff">boolean</font> uniqueColors = <font color="#0000ff">true</font>;
  Thread thisThread;
  <font color="#0000ff">int</font> delay = 25;
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> init() {
    <font color="#0000ff">if</font> (xExtent == 0 &amp;&amp; yExtent == 0) {
      xExtent = Integer.parseInt(
        getParameter("xExtent"));
      yExtent = Integer.parseInt(
        getParameter("yExtent"));
    }
    beasts = 
      makeBeastVector(numBeasts, uniqueColors);
    <font color="#009900">// Now start the beasts a-rovin':</font>
    thisThread = <font color="#0000ff">new</font> Thread(<font color="#0000ff">this</font>);
    thisThread.start();
  }
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> run() {
    <font color="#0000ff">while</font>(<font color="#0000ff">true</font>) {
      <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; beasts.size(); i++){
        Beast b = (Beast) beasts.elementAt(i);
        b.step();
      }
      <font color="#0000ff">try</font> {
        thisThread.sleep(delay);
      } <font color="#0000ff">catch</font>(InterruptedException ex){}
      repaint(); <font color="#009900">// Otherwise it won't update</font>
    }
  }
  Vector makeBeastVector(
      <font color="#0000ff">int</font> quantity, <font color="#0000ff">boolean</font> uniqueColors) {
    Vector newBeasts = <font color="#0000ff">new</font> Vector();
    Random generator = <font color="#0000ff">new</font> Random();
    <font color="#009900">// Used only if uniqueColors is on:</font>
    <font color="#0000ff">double</font> cubeRootOfBeastNumber = 
      Math.pow((<font color="#0000ff">double</font>)numBeasts, 1.0 / 3.0);
    <font color="#0000ff">float</font> colorCubeStepSize = 
      (<font color="#0000ff">float</font>) (1.0 / cubeRootOfBeastNumber);
    <font color="#0000ff">float</font> r = 0.0f;
    <font color="#0000ff">float</font> g = 0.0f;
    <font color="#0000ff">float</font> b = 0.0f;
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; quantity; i++) {
      <font color="#0000ff">int</font> x = 
        (<font color="#0000ff">int</font>) (generator.nextFloat() * xExtent);
      <font color="#0000ff">if</font>(x &gt; xExtent - Beast.GSIZE) 
        x -= Beast.GSIZE;
      <font color="#0000ff">int</font> y = 
        (<font color="#0000ff">int</font>) (generator.nextFloat() * yExtent);
      <font color="#0000ff">if</font>(y &gt; yExtent - Beast.GSIZE) 
        y -= Beast.GSIZE;
      <font color="#0000ff">float</font> direction = (<font color="#0000ff">float</font>)(
        generator.nextFloat() * 2 * Math.PI);
      <font color="#0000ff">int</font> speed = (<font color="#0000ff">int</font>)(
        generator.nextFloat() * (<font color="#0000ff">float</font>)maxSpeed);
      <font color="#0000ff">if</font>(uniqueColors) {
        r += colorCubeStepSize;
        <font color="#0000ff">if</font>(r &gt; 1.0) {
          r -= 1.0f;
          g += colorCubeStepSize;
          <font color="#0000ff">if</font>( g &gt; 1.0) {
            g -= 1.0f;
            b += colorCubeStepSize;
            <font color="#0000ff">if</font>(b &gt; 1.0) 
              b -= 1.0f;
          }
        }
      }
      newBeasts.addElement(
        <font color="#0000ff">new</font> Beast(<font color="#0000ff">this</font>, x, y, direction, speed, 
          <font color="#0000ff">new</font> Color(r,g,b)));
    }
    <font color="#0000ff">return</font> newBeasts;
  }
  <font color="#0000ff">public</font> Vector beastListInSector(Beast viewer) {
    Vector output = <font color="#0000ff">new</font> Vector();
    Enumeration e = beasts.elements();
    Beast aBeast = (Beast)beasts.elementAt(0);
    <font color="#0000ff">int</font> counter = 0;
    <font color="#0000ff">while</font>(e.hasMoreElements()) {
      aBeast = (Beast) e.nextElement();
      <font color="#0000ff">if</font>(aBeast != viewer) {
        Point p = aBeast.position();
        Point v = viewer.position();
        <font color="#0000ff">float</font> bearing = 
          aBeast.bearingFromPointAlongAxis(
            v.x, v.y, viewer.currentDirection);
        <font color="#0000ff">if</font>(Math.abs(bearing) &lt; fieldOfView / 2)
         output.addElement(aBeast);
      }
    }
    <font color="#0000ff">return</font> output;
  }
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> paint(Graphics g)  {
    Enumeration e = beasts.elements();
    <font color="#0000ff">while</font>(e.hasMoreElements()) {
      ((Beast)e.nextElement()).draw(g);
    }
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args)   {
    FieldOBeasts field = <font color="#0000ff">new</font> FieldOBeasts();
    field.xExtent = 640;
    field.yExtent = 480;
    Frame frame = <font color="#0000ff">new</font> Frame("Field 'O Beasts");
    <font color="#009900">// Optionally use a command-line argument</font>
    <font color="#009900">// for the sleep time:</font>
    <font color="#0000ff">if</font>(args.length &gt;= 1)
      field.delay = Integer.parseInt(args[0]);
    frame.addWindowListener(
      <font color="#0000ff">new</font> WindowAdapter() {
        <font color="#0000ff">public</font> <font color="#0000ff">void</font> windowClosing(WindowEvent e) {
          System.exit(0);
        }
      });
    frame.add(field, BorderLayout.CENTER);
    frame.setSize(640,480);
    field.init();
    field.start();
    frame.setVisible(<font color="#0000ff">true</font>);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Although
this isn&#8217;t a perfect reproduction of the behavior in Craig
Reynold&#8217;s &#8220;Boids&#8221; example, it exhibits its own fascinating
characteristics, which you can modify by adjusting the numbers. You can find
out more about the modeling of flocking behavior and see a spectacular 3-D
version of Boids at Craig Reynold&#8217;s page 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>http://www.hmt.com/cwr/boids.html</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
run this program as an applet, put the following applet tag in an HTML file:
</FONT><P></DIV>

<font color="#990000"><PRE>&lt;applet
code=FieldOBeasts
width=640
height=480&gt;
&lt;param name=xExtent value = "640"&gt;
&lt;param name=yExtent value = "480"&gt;
&lt;/applet&gt;</PRE></font><DIV ALIGN=LEFT><a name="_Toc408018815"></a><P></DIV>

<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0186.html">Prev</a> | <a href="tij0188.html">Next</a>
</div>
</body></html>

⌨️ 快捷键说明

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