1 /* File: mRectangle.java */ 2 3 import java.awt.*; 4 5 public class mRectangle extends mPoint 6 { 7 // Instance variables: 8 protected int w, h; // width and height 9 10 // Constructor: 11 public mRectangle( int new_x, int new_y, int new_w, int new_h ) 12 { 13 // Invoke the constructor of the superclass mPoint: 14 super( new_x, new_y ); 15 w = new_w; h = new_h; 16 } 17 18 // Implement mPoint.paint( Graphics ): 19 public void paint( Graphics g ) 20 { 21 g.setColor( color ); 22 g.fillRect( x, y, w, h ); 23 } 24 25 // Implement mPoint.checkBoundary( Rectangle ): 26 public void checkBoundary( Rectangle r ) 27 { 28 // Calculate new location: 29 int nx = x + dx, ny = y + dy; 30 // Check if new location out of bounds: 31 if ( (nx < r.x) || (nx + w > r.x + r.width) ) dx = -dx; 32 if ( (ny < r.y) || (ny + h > r.y + r.height) ) dy = -dy; 33 } 34 35 // Implement mPoint.isInside( int, int ): 36 public boolean isInside( int some_x, int some_y ) 37 { 38 Rectangle r = new Rectangle( x, y, w, h ); 39 return r.contains( some_x, some_y ); 40 } 41 42 } // end of mRectangle class 43