public void checkBoundry(Rectangle rect) { int nx = x+dx; /* caculate new location */ int ny = y+dy; /* check if new location out of boundry */ if ( (nx < rect.x) || (nx >= rect.x+rect.width) ) dx = -dx; if ( (ny < rect.y) || (ny >= rect.y+rect.height) ) dy = -dy; }
The boundry check in mRectangle is slightly different. So we redefine the checkBoundry in mRectangle.
public void checkBoundry(Rectangle rect) { /* overwrite the mPoint's checkBoundry */ /* the rectangle's checkBoundry is different */ 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; }
Class Hierarchy: Applet --- Test mPoint --- mRectangleJAVA Source Code