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

📄 boid.java

📁 自己做的几个j2me程序例子。。只要在Jbuilder里open project就行。。大家看看很有代表性
💻 JAVA
字号:
package example.boids;

import javax.microedition.lcdui.*;

class Boid implements BoidsConstants {
  private int x;
  private int y;
  private int vx;
  private int vy;
  private int speed;
  private final int red;
  private final int green;
  private final int blue;
  private final BoidsCanvas canvas;

  private static final BoidRule gatherRule = new GatherRule();
  private static final BoidRule repelRule = new RepelRule();
  private static final BoidRule alignRule = new AlignRule();

  private static final int MAX_SPEED = 5 << FIXED_POINT_SHIFT;
  private static final int MAX_FLAP = 10;
  private static final int LENGTH = 10 << FIXED_POINT_SHIFT;
  private static final int EDGE_REPEL_DISTANCE = 20 << FIXED_POINT_SHIFT;
  private static final int EDGE_REPEL_FACTOR = (1 << FIXED_POINT_SHIFT) / 4;

  Boid(int x, int y,
       int vx, int vy,
       int red, int green, int blue,
       BoidsCanvas canvas) {
    this.x = x;
    this.y = y;
    this.vx = vx;
    this.vy = vy;
    this.red = red;
    this.green = green;
    this.blue = blue;
    this.canvas = canvas;
  }

  int getX() {
    return x;
  }

  int getY() {
    return y;
  }

  int getVx() {
    return vx;
  }

  int getVy() {
    return vy;
  }

  void tick() {
    gatherRule.reset(this);
    canvas.applyRule(gatherRule, this);
    repelRule.reset(this);
    canvas.applyRule(repelRule, this);
    alignRule.reset(this);
    canvas.applyRule(alignRule, this);

    vx += gatherRule.getVx() + repelRule.getVx() + alignRule.getVx();
    vy += gatherRule.getVy() + repelRule.getVy() + alignRule.getVy();

    repelFromEdges();

    speed = BoidsUtils.squareRoot(vx * vx + vy * vy);
    if (speed > MAX_SPEED) {
      int factor = (MAX_SPEED << FIXED_POINT_SHIFT) / speed;
      vx = (vx * factor) >> FIXED_POINT_SHIFT;
      vy = (vy * factor) >> FIXED_POINT_SHIFT;
      speed = MAX_SPEED;
    }

    x += vx;
    y += vy;
  }

  private void repelFromEdges() {
    if (x < EDGE_REPEL_DISTANCE) {
      vx += ( (EDGE_REPEL_DISTANCE - x) * EDGE_REPEL_FACTOR)
          >> FIXED_POINT_SHIFT;
    }
    else {
      int width = canvas.getWidth() << FIXED_POINT_SHIFT;
      if (x > width - EDGE_REPEL_DISTANCE) {
        vx -= ( (x - (width - EDGE_REPEL_DISTANCE)) * EDGE_REPEL_FACTOR)
            >> FIXED_POINT_SHIFT;
      }
    }

    if (y < EDGE_REPEL_DISTANCE) {
      vy += ( (EDGE_REPEL_DISTANCE - y) * EDGE_REPEL_FACTOR)
          >> FIXED_POINT_SHIFT;
    }
    else {
      int height = canvas.getHeight() << FIXED_POINT_SHIFT;
      if (y > height - EDGE_REPEL_DISTANCE) {
        vy -= ( (y - (height - EDGE_REPEL_DISTANCE)) * EDGE_REPEL_FACTOR)
            >> FIXED_POINT_SHIFT;
      }
    }
  }

  void draw(Graphics g) {
     // calculate lengths; for simplicity, don't draw
     // stationary boids (how would we show their direction?)
    if (speed != 0) {
      int lx = vx * LENGTH / speed;
      int ly = vy * LENGTH / speed;
      int xHead = (x + (lx >> 1)) >> FIXED_POINT_SHIFT;
      int yHead = (y + (ly >> 1)) >> FIXED_POINT_SHIFT;
      int xEye = x >> FIXED_POINT_SHIFT;
      int yEye = y >> FIXED_POINT_SHIFT;
      int xTail = (x - (lx >> 2)) >> FIXED_POINT_SHIFT;
      int yTail = (y - (ly >> 2)) >> FIXED_POINT_SHIFT;
      int xLeft = (x - ( (lx + ly) >> 1)) >> FIXED_POINT_SHIFT;
      int yLeft = (y - ( (ly - lx) >> 1)) >> FIXED_POINT_SHIFT;
      int xRight = (x - ( (lx - ly) >> 1)) >> FIXED_POINT_SHIFT;
      int yRight = (y - ( (ly + lx) >> 1)) >> FIXED_POINT_SHIFT;
      g.setColor(red, green, blue);
      g.drawLine(xLeft, yLeft, xTail, yTail);
      g.drawLine(xRight, yRight, xTail, yTail);
      g.drawLine(xLeft, yLeft, xHead, yHead);
      g.drawLine(xRight, yRight, xHead, yHead);
      g.setColor(0, 0, 0);
      g.drawLine(xEye, yEye, xEye, yEye);
    }
  }
}

⌨️ 快捷键说明

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