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

📄 rectangle.java

📁 基于java的一款游戏的故事。基于java的一款游戏的故事。基于java的一款游戏的故事。
💻 JAVA
字号:
package com.thinkenjoy.feitian;

/**
 * 处理边界与碰撞的问题
 */
public class Rectangle {

  /**
   * 矩形的范围
   *
   */
  public Rectangle() {
    this(0, 0, 0, 0);
  }

  /**
   * 矩形的范围
   * @param x 左上角的X坐标
   * @param y 左上角的Y坐标
   * @param width 矩形的宽
   * @param height 矩形的高
   */
  public Rectangle(int x, int y, int width, int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }

  /**
   * 矩形的范围
   * @param width 矩形的宽
   * @param height 矩形的高
   */
  public Rectangle(int width, int height) {
    this(0, 0, width, height);
  }

  /**
   * 矩形的范围
   * @param r 另一个现成的矩形
   */
  public Rectangle(Rectangle r) {
    this(r.x, r.y, r.width, r.height);
  }

  /**
   * 获取左上角X坐标
   * @return
   */
  public int getX() {
    return x;
  }

  /**
   * 获取左上角Y坐标
   * @return
   */
  public int getY() {
    return y;
  }

  /**
   * 设置左上角X坐标
   * @param x
   */
  public void setX(int x) {
    this.x = x;
  }

  /**
   * 设置左上角Y坐标
   * @param y
   */
  public void setY(int y) {
    this.y = y;
  }

  /**
   * 设置起始点坐标
   * @param x
   * @param y
   */
  public void setLocation(int x, int y) {
    this.x = x;
    this.y = y;
  }

  /**
   * 在X方向上移动
   * @param offset
   */
  public void moveX(int offset) {
    x += offset;
  }

  /**
   * 在Y方向上移动
   * @param offset
   */
  public void moveY(int offset) {
    y += offset;
  }

  /**
   * 得到其宽度
   * @return
   */
  public int getWidth() {
    return width;
  }

  /**
   * 得到其高度
   * @return
   */
  public int getHeight() {
    return height;
  }

  /**
   * 设置宽度
   * @param width
   */
  public void setWidth(int width) {
    this.width = width;
  }

  /**
   * 设置高度
   * @param height
   */
  public void setHeight(int height) {
    this.height = height;
  }

  /**
   * 设置宽高
   * @param width
   * @param height
   */
  public void setSize(int width, int height) {
    this.width = width;
    this.height = height;
  }

  /**
   * 设置范围
   * @param x
   * @param y
   * @param width
   * @param height
   */
  public void setBounds(int x, int y, int width, int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }

  /**
   * 判断指定矩形是否包含在此矩形范围中
   * @param r 指定矩形
   * @return
   */
  public boolean contains(Rectangle r) {
    return contains(r.x, r.y, r.width, r.height);
  }

  /**
   * 判断指定范围是否包含在此矩形范围中
   * @param xArg
   * @param yArg
   * @param wArg
   * @param hArg
   * @return
   */
  public boolean contains(int xArg, int yArg, int wArg, int hArg) {
    int w = width;
    int h = height;
    if ((w | h | wArg | hArg) < 0)
      return false;
    int x = this.x;
    int y = this.y;
    if (xArg < x || yArg < y)
      return false;
    w += x;
    wArg += xArg;
    if (wArg <= xArg) {
      if (w >= x || wArg > w)
        return false;
    } else
    if (w >= x && wArg > w)
      return false;
    h += y;
    hArg += yArg;
    if (hArg <= yArg) {
      if (h >= y || hArg > h)
        return false;
    } else
    if (h >= y && hArg > h)
      return false;
    return true;
  }

  /**
   * 判断某个点是否包含在此矩形范围中
   * @param xArg
   * @param yArg
   * @return
   */
  public boolean inside(int xArg, int yArg) {
    int w = width;
    int h = height;
    if ((w | h) < 0)
      return false;
    int x = this.x;
    int y = this.y;
    if (xArg < x || yArg < y) {
      return false;
    } else {
      w += x;
      h += y;
      return (w < x || w > xArg) && (h < y || h > yArg);
    }
  }

  /**
   * 判断指定矩形是否与此矩形相交
   * @param r
   * @return
   */
  public boolean intersects(Rectangle r) {
    int tw = width;
    int th = height;
    int rw = r.width;
    int rh = r.height;
    if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
      return false;
    } else {
      int tx = x;
      int ty = y;
      int rx = r.x;
      int ry = r.y;
      rw += rx;
      rh += ry;
      tw += tx;
      th += ty;
      return (rw < rx || rw > tx) && (rh < ry || rh > ty) &&
        (tw < tx || tw > rx) && (th < ty || th > ry);
    }
  }

  /**
   * 顶点X坐标
   */
  public int x;

  /**
   * 顶点Y坐标
   */
  public int y;

  /**
   * 此矩形的宽度
   */
  public int width;

  /**
   * 此矩形的高度
   */
  public int height;
}

⌨️ 快捷键说明

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