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 extends ShapeBasics implements RectangleInterface
 11: {
 12:     private int height;
 13:     private int width;
 14:     
 15:     public Rectangle( )
 16:     {
 17:         super( );
 18:         height = 0;
 19:         width = 0;
 20:     }
 21:     
 22:     public Rectangle(int theOffset, int theHeight, int theWidth)
 23:     {
 24:         super(theOffset);
 25:         height = theHeight;
 26:         width = theWidth;
 27:     }
 28:     
 29:     public void set(int newHeight, int newWidth)
 30:     {
 31:         height = newHeight;
 32:         width = newWidth;
 33:     }
 34:     
 35:     /** Draws the shape at the current line. */
 36:     public void drawHere( )
 37:     {
 38:         drawHorizontalLine( );
 39:         drawSides( );
 40:         drawHorizontalLine( );
 41:     }
 42: 
 43:     private void drawHorizontalLine( )
 44:     {
 45:         skipSpaces(getOffset( )); 
 46:         for (int count = 0; count < width; count++)
 47:              System.out.print('-');
 48:         System.out.println( );        
 49:     }
 50:     
 51:     private void drawSides( )
 52:     {
 53:         for (int count = 0; count < (height - 2); count++)
 54:             drawOneLineOfSides( );
 55:     }
 56:     
 57:     private void drawOneLineOfSides( )
 58:     {
 59:         skipSpaces(getOffset( )); 
 60:         System.out.print('|');
 61:         skipSpaces(width - 2);        
 62:         System.out.println('|');
 63:     }
 64:     
 65:     //Writes the indicated number of spaces.
 66:     private static void skipSpaces(int number)
 67:     {
 68:         for (int count = 0; count < number; count++)
 69:             System.out.print(' ');
 70:     }
 71: }
 72: