Source of Rectangle.java


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