Source of Rectangle.java


  1: //Rectangle.java
  2: 
  3: /**
  4:  * Class for drawing rectangles on the screen using keyboard
  5:  * characters. Each character is higher than it is wide, so
  6:  * these rectangles will look taller than you might expect.
  7:  * Inherits getOffset, setOffset, and drawAt from the class
  8:  * ShapeBasics.
  9:  */
 10: public class Rectangle
 11:     extends ShapeBasics
 12:     implements RectangleInterface
 13: {
 14:     private int height;
 15:     private int width;
 16: 
 17:     public Rectangle()
 18:     {
 19:         super();
 20:         height = 0;
 21:         width = 0;
 22:     }
 23: 
 24:     public Rectangle
 25:     (
 26:         int theOffset,
 27:         int theHeight,
 28:         int theWidth
 29:     )
 30:     {
 31:         super(theOffset);
 32:         height = theHeight;
 33:         width = theWidth;
 34:     }
 35: 
 36:     public void set
 37:     (
 38:         int newHeight,
 39:         int newWidth
 40:     )
 41:     {
 42:         height = newHeight;
 43:         width = newWidth;
 44:     }
 45: 
 46:     /**
 47:      * Draws the shape at the current line.
 48:      */
 49:     public void drawHere()
 50:     {
 51:         drawHorizontalLine();
 52:         drawSides();
 53:         drawHorizontalLine();
 54:     }
 55: 
 56:     private void drawHorizontalLine()
 57:     {
 58:         skipSpaces(getOffset());
 59:         for (int count = 0; count < width; count++)
 60:             System.out.print('-');
 61:         System.out.println();
 62:     }
 63: 
 64:     private void drawSides()
 65:     {
 66:         for (int count = 0; count < (height - 2); count++)
 67:             drawOneLineOfSides();
 68:     }
 69: 
 70:     private void drawOneLineOfSides()
 71:     {
 72:         skipSpaces(getOffset());
 73:         System.out.print('|');
 74:         skipSpaces(width - 2);
 75:         System.out.println('|');
 76:     }
 77: 
 78:     //Writes the indicated number of spaces.
 79:     private static void skipSpaces
 80:     (
 81:         int number
 82:     )
 83:     {
 84:         for (int count = 0; count < number; count++)
 85:             System.out.print(' ');
 86:     }
 87: }