1   // joi/3/shapes/Screen.java
2   //
3   //
4   // Copyright 2003 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 3
12   */
13  
14  public class Screen
15  {
16      /**
17       * The character used to paint the screen's frame.
18       */
19  
20      private static final char FRAMECHAR = '+';
21      private static final char BLANK = ' ';
22      private int width;
23      private int height;
24      private char[][] pixels;
25     
26      /** 
27       * Construct a Screen.
28       *
29       * @param width the number of pixels in the x direction.
30       * @param height the number of pixels in the y direction.
31       */
32  
33      public Screen( int width, int height )
34      {
35          this.width  = width;
36          this.height = height;
37          pixels = new char[width][height];
38          clear();
39      }
40  
41      /**
42       * Clear the Screen, painting a blank at every pixel.
43       */
44      
45      public void clear()
46      {
47          for (int x  = 0; x < width; x++) {
48              for ( int y = 0; y < height; y++ ) {
49                  pixels[x][y] = BLANK;
50              }
51          }
52      }
53  
54      /**
55       * Paint a character pixel at position (x,y).
56       *
57       * @param c the character to be painted.
58       * @param x the (horizontal) x position.
59       * @param y the (vertical) y position.
60       */
61  
62      public void paintAt( char c, int x, int y )
63      {
64          if ( 0 <= x && x < width &&
65               0 <= y && y < height) {
66              pixels[x][y] = c;
67          }
68          // Otherwise off the Screen - nothing is painted.
69      }
70      
71      /**
72       * How wide is this Screen?
73       *
74       * @return the width.
75       */
76  
77      public int getWidth() 
78      {
79          return width;
80      }
81  
82      /**
83       * How high is this Screen?
84       *
85       * @return the height.
86       */
87  
88      public int getHeight() 
89      {
90          return height;
91      }
92  
93      /**
94       * Draw this Screen on a Terminal.
95       *
96       * @param t the Terminal on which to draw this Screen.
97       */
98  
99      public void draw( Terminal t )
100     {
101         for ( int col = -1; col < width+1 ; col++ ) {  // top edge
102             t.print(FRAMECHAR);
103         }
104         t.println();                               
105         for ( int row = 0; row < height; row++ ) {  
106             t.print(FRAMECHAR);                      // left edge
107             for ( int col = 0; col < width; col++ ) {
108                 t.print( pixels[col][row] );
109             }
110             t.println( FRAMECHAR );                  // right edge
111         }
112         for ( int col = -1; col < width+1 ; col++ ) {  // bottom edge
113             t.print(FRAMECHAR);
114         }
115         t.println();
116     }
117 }
118