We can define a new class mRectangle that extends mPoint where (x,y,color) from mPoint are lower left hand corner/color and we add width and height
|
public class mRectangle extends mPoint {
-
int w, h;
-
public mRectangle(int _x, int _y, int _w, int _h) {
-
super(_x, _y); /* call mPoint's constructor */
-
w = _w;
-
h = _h;
-
} /* End mRectangle Constructor */
|
/* overwrite the mPoint's checkBoundry method */
-
public void checkBoundry(Rectangle rect) {
-
int nx = x+dx;
-
int ny = y+dy;
-
if ( (nx < rect.x) || (nx+w >= rect.x+rect.width) ) dx = -dx;
-
if ( (ny < rect.y) || (ny+h >= rect.y+rect.height) ) dy = -dy;
-
}
-
public void paint(Graphics g) { /* Override mPoint Method */
-
g.setColor(color);
-
g.drawRect(x, y, w, h);
-
}
|
}
|