Source of Box.java


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