友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
一世书城 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

Java编程思想第4版[中文版](PDF格式)-第206章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




import java。awt。*;  

import java。awt。event。*;  

import java。applet。*;  

import java。util。*;  

  

class Beast {  



                                                                        643 


…………………………………………………………Page 645……………………………………………………………

  int  

    x; y;            // Screen position  

    currentSpeed;    // Pixels per second  

  float currentDirection;  // Radians  

  Color color;      // Fill color  

  FieldOBeasts field; // Where the Beast roams  

  static final int GSIZE = 10; // Graphic size  

  

  public Beast(FieldOBeasts f; int x; int y;   

      float cD; int cS; Color c) {  

    field = f;  

    this。x = x;  

    this。y = y;  

    currentDirection = cD;  

    currentSpeed = cS;  

    color = c;  

  }  

  public void step()  {  

    // You move based on those within your sight:  

    Vector seen = field。beastListInSector(this);  

    // If you're not out in front  

    if(seen。size() 》 0) {  

      // Gather data on those you see  

      int totalSpeed = 0;  

      float totalBearing = 0。0f ;  

      float distanceToNearest = 100000。0f;  

      Beast nearestBeast =   

        (Beast)seen。elementAt(0);  

      Enumeration e = seen。elements();  

      while(e。hasMoreElements()) {  

        Beast aBeast = (Beast) e。nextElement();  

        totalSpeed += aBeast。currentSpeed;  

        float bearing =   

          aBeast。bearingFromPointAlongAxis(  

            x; y; currentDirection);  

        totalBearing += bearing;  

        float distanceToBeast =   

          aBeast。distanceFromPoint(x; y);  

        if(distanceToBeast 《 distanceToNearest) {  

          nearestBeast = aBeast;  

          distanceToNearest = distanceToBeast;  

        }  

      }  

      // Rule 1: Match average speed of those   

      // in the list:  

      currentSpeed = totalSpeed / seen。size();  

      // Rule 2: Move towards the perceived  

      // center of gravity of the herd:  

      currentDirection =   

        totalBearing / seen。size();  

      // Rule 3: Maintain a minimum distance   

      // from those around you:  



                                                                                          644 


…………………………………………………………Page 646……………………………………………………………

      if(distanceToNearest  field。maxSpeed) {  

          currentSpeed = field。maxSpeed;  

        }  

      }  

    }   

    else {  // You are in front; so slow down  

      currentSpeed =   

        (int)(currentSpeed * field。decayRate);  

    }  

    // Make the beast move:  

    x += (int)(Math。cos(currentDirection)   

               * currentSpeed);  

    y += (int)(Math。sin(currentDirection)  

               * currentSpeed);  

    x %= field。xExtent;  

    y %= field。yExtent;  

    if(x 《 0)  

      x += field。xExtent;  

    if(y 《 0)  

      y += field。yExtent;  

  }  

  public float bearingFromPointAlongAxis (  

      int originX; int originY; float axis) {  

    // Returns bearing angle of the current Beast  

    // in the world coordiante system  

    try {  

      double bearingInRadians =   

        Math。atan(  

          (this。y originY) /   

          (this。x originX));  

      // Inverse tan has two solutions; so you   

      // have to correct for other quarters:  

      if(x 《 originX) {    

        if(y 《 originY) {  

          bearingInRadians += (float)Math。PI;  

        }   

        else {  

          bearingInRadians =   

            (float)Math。PI bearingInRadians;  

        }  

      }  

      // Just subtract the axis (in radians):  

      return (float) (axis bearingInRadians);  

    } catch(ArithmeticException aE) {  

      // Divide by 0 error possible on this  

      if(x 》 originX) {  

          return 0;  



                                                                                        645 


…………………………………………………………Page 647……………………………………………………………

      }   

      else  

        return (float) Math。PI;  

    }  

  }  

  public float distanceFromPoint(int x1; int y1){  

    return (float) Math。sqrt(  

      Math。pow(x1 x; 2) +   

      Math。pow(y1 y; 2));  

  }  

  public Point position() {   

    return new Point(x; y);  

  }  

  // Beasts know how to draw themselves:  

  public void draw(Graphics g) {  

    g。setColor(color);  

    int directionInDegrees = (int)(  

      (currentDirection * 360) / (2 * Math。PI));  

    int startAngle = directionInDegrees   

      FieldOBeasts。halfFieldOfView;  

    int endAngle = 90;  

    g。fillArc(x; y; GSIZE; GSIZE;   

      startAngle; endAngle);  

  }  

}  

  

public class FieldOBeasts extends Applet   

    implements Runnable {  

  private Vector beasts;  

  static float   

    fieldOfView =   

      (float) (Math。PI / 4); // In radians  

    // Deceleration % per second:  

    decayRate = 1。0f;   

    minimumDistance = 10f; // In pixels  

  static int  

    halfFieldOfView = (int)(  

      (fieldOfView * 360) / (2 * Math。PI));  

    xExtent = 0;  

    yExtent = 0;  

    numBeasts = 50;  

    maxSpeed = 20; // Pixels/second  

  boolean uniqueColors = true;  

  Thread thisThread;  

  int delay = 25;  

  public void init() {  

    if (xExtent == 0 && yExtent == 0) {  

      xExtent = Integer。parseInt(  

        getParameter(〃xExtent〃));  

      yExtent = Integer。parseInt(  

        getParameter(〃yExtent〃));  

    }  



                                                                                             646 


…………………………………………………………Page 648……………………………………………………………

    beasts =   

      makeBeastVector(numBeasts; uniqueColors);  

    // Now start the beasts a…rovin':  

    thisThread = new Thread(this);  

    thisThread。start();  

  }  

  public void run() {  

    while(true) {  

      for(int i = 0; i 《 beasts。size(); i++){  

        Beast b = (Beast) beasts。elementAt(i);  

        b。step();  

      }  

      try {  

        thisThread。sleep(delay);  

      } catch(InterruptedException ex){}  

      repaint(); // Otherwise it won't update  

    }  

  }  

  Vector makeBeastVector(  

      int quantity; boolean uniqueColors) {  

    Vector newBeasts = new Vector();  

    Random generator = new Random();  

    // Used only if uniqueColors is on:  

    double cubeRootOfBeastNumber =   

      Math。pow((double)numBeasts; 1。0 / 3。0);  

    float colorCubeStepSize =   

      (float) (1。0 / cubeRootOfBeastNumber);  

    float r = 0。0f;  

    float g = 0。0f;  

    float b = 0。0f;  

    for(int i = 0; i 《 quantity; i++) {  

      int x =   

        (int) (generator。nextFloat() * xExtent);  

      if(x 》 xExtent Beast。GSIZE)   

        x …= Beast。GSIZE;  

      int y =   

        (int) (generator。nextFloat() * yExtent);  

      if(y 》 yExtent Beast。GSIZE)   

        y …= Beast。GSIZE;  

      float direction = (float)(  

        generator。nextFloat() * 2 * Math。PI);  

      int speed = (int)(  

        generator。nextFloat() * (float)maxSpeed);  

      if(uniqueColors) {  

        r += colorCubeStepSize;  

        if(r 》 1。0) {  

          r …= 1。0f;  

          g += colorCubeStepSize;  

          if( g 》 1。0) {  

            g …= 1。0f;  

            b += colorCubeStepSize;  

            if(b 》 1。0)   



                                                                                        647 


…………………………………………………………Page 649……………………………………………………………

              b …= 1。0f;  

          }  

        }  

      }  

      newBeasts。addElement(  

        new Beast(this; x; y; direction; speed;   

          new Color(r;g;b)));  
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!