?? rectangle.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 另一個現(xiàn)成的矩形
*/
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;
}
/**
* 設(shè)置左上角X坐標
* @param x
*/
public void setX(int x) {
this.x = x;
}
/**
* 設(shè)置左上角Y坐標
* @param y
*/
public void setY(int y) {
this.y = y;
}
/**
* 設(shè)置起始點坐標
* @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;
}
/**
* 設(shè)置寬度
* @param width
*/
public void setWidth(int width) {
this.width = width;
}
/**
* 設(shè)置高度
* @param height
*/
public void setHeight(int height) {
this.height = height;
}
/**
* 設(shè)置寬高
* @param width
* @param height
*/
public void setSize(int width, int height) {
this.width = width;
this.height = height;
}
/**
* 設(shè)置范圍
* @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;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -