1   // joi/5/shapes/Screen.java                         
2   //                                                            
3   //                                                            
4   // Copyright 1998-2001 Bill Campbell and Ethan Bolker                         
5                                                               
6   /**
7    * A Screen is a (width*height) grid of (character) 'pixels'
8    * on which we may paint various shapes.  It can be drawn to
9    * a Terminal.
10   *
11   * @version 5
12   */
13  
14  public class Screen
15  {
16      private static final char FRAMECHAR = '+';
17      private static final char BLANK = ' ';
18      private int       width;
19      private int       height;
20      private char[][] pixels;
21     
22      /** 
23       * Construct a screen.
24       *
25       * @param width the number of pixels in the x direction.
26       * @param height the number of pixels in the y direction.
27       */
28  
29      public Screen( int width, int height )
30      {
31          this.width  = width;
32          this.height = height;
33          pixels = new char[width][height];
34          clear();
35      }
36  
37      /**
38       * Clear the Screen, painting a blank at every pixel.
39       */
40      
41      public void clear()
42      {
43          for (int x  = 0; x < width; x++)
44              for ( int y = 0; y < height; y++ )
45                  pixels[x][y] = BLANK;
46      }
47  
48      /**
49       * Paint a character pixel at position (x,y).
50       *
51       * @param c the character to be painted.
52       * @param x the (horizontal) x position.
53       * @param y the (vertical) y position.
54       */
55  
56      public void paintAt( char c, int x, int y )
57      {
58          if ( 0 <= x && x < width &&
59               0 <= y && y < height)
60              pixels[x][y] = c;
61          // otherwise off the Screen - nothing is painted
62      }
63      
64      /**
65       * Draw this Screen on a Terminal.
66       *
67       * @param t the Terminal on which to draw this Screen.
68       */
69  
70      public void draw( Terminal t )
71      {
72          for ( int col = -1; col < width+1 ; col++ )  // top edge
73              t.print(FRAMECHAR);        
74          t.println();                               
75          for ( int row = 0; row < height; row++ ) {  
76              t.print(FRAMECHAR);                      // left edge
77              for ( int col = 0; col < width; col++ )
78                  t.print( pixels[col][row] );
79              t.println( FRAMECHAR );                  // right edge
80          }
81          for ( int col = -1; col < width+1 ; col++ )  // bottom edge
82              t.print(FRAMECHAR);
83          t.println();
84      }
85  }
86