Basic HTML version of Foils prepared May 12 1996

Foil 62 Inheritance or Subclassing

From CRPC Lectures on Java Language Applets Graphics CRPC Annual Meeting Tutorial -- May 14,1996. by Geoffrey C. Fox


1 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
2 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 */
3 /* 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);
  • }
4 }

in Table To:


Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Dec 14 1997