import java.awt.*; public class mRectangle extends mPoint { // Instance variables: int w, h; // Constructor: public mRectangle(int _x, int _y, int _w, int _h) { // Invoke the constructor of the superclass mpoint: super(_x, _y); w = _w; h = _h; } // Encapsulate the instance variables: public void setDimension(int _w, int _h) { w = _w; h = _h; } // Override mPoint's checkBoundary method: public void checkBoundary(Rectangle rect) { // Calculate new location: int nx = x + dx, ny = y + dy; // Check if new location out of bounds: if ( (nx < rect.x) || (nx + w >= rect.x + rect.width) ) dx = -dx; if ( (ny < rect.y) || (ny + h >= rect.y + rect.height) ) dy = -dy; } // Override java.awt.Component.paint: public void paint(Graphics g) { // This is *not* mPoint's setColor method: g.setColor(super.color); g.fillRect(x, y, w, h); } } // end of mRectangle class