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:     /**
 36:      Draws the shape at the current line.
 37:     */
 38:     public void drawHere( )
 39:     {
 40:         drawHorizontalLine( );
 41:         drawSides( );
 42:         drawHorizontalLine( );
 43:     }
 44: 
 45:     private void drawHorizontalLine( )
 46:     {
 47:         skipSpaces(getOffset( ));
 48:         for (int count = 0; count < width; count++)
 49:              System.out.print('-');
 50:         System.out.println( );
 51:     }
 52: 
 53:     private void drawSides( )
 54:     {
 55:         for (int count = 0; count < (height - 2); count++)
 56:             drawOneLineOfSides( );
 57:     }
 58: 
 59:     private void drawOneLineOfSides( )
 60:     {
 61:         skipSpaces(getOffset( ));
 62:         System.out.print('|');
 63:         skipSpaces(width - 2);
 64:         System.out.println('|');
 65:     }
 66: 
 67:     //Writes the indicated number of spaces.
 68:     private static void skipSpaces(int number)
 69:     {
 70:         for (int count = 0; count < number; count++)
 71:             System.out.print(' ');
 72:     }
 73: }