1 import java.awt.*; 2 3 public class mRectangle extends mPoint { 4 5 // Instance variables: 6 int w, h; 7 8 // Constructor: 9 public mRectangle(int _x, int _y, int _w, int _h) { 10 // Invoke the constructor of the superclass mpoint: 11 super(_x, _y); 12 w = _w; h = _h; 13 } 14 15 // Encapsulate the instance variables: 16 public void setDimension(int _w, int _h) { 17 w = _w; h = _h; 18 } 19 20 // Override mPoint's checkBoundary method: 21 public void checkBoundary(Rectangle rect) { 22 // Calculate new location: 23 int nx = x + dx, ny = y + dy; 24 // Check if new location out of bounds: 25 if ( (nx < rect.x) || (nx + w >= rect.x + rect.width) ) dx = -dx; 26 if ( (ny < rect.y) || (ny + h >= rect.y + rect.height) ) dy = -dy; 27 } 28 29 // Override java.awt.Component.paint: 30 public void paint(Graphics g) { 31 // This is *not* mPoint's setColor method: 32 g.setColor(super.color); 33 g.fillRect(x, y, w, h); 34 } 35 36 } // end of mRectangle class 37