package username.analysis; /** * A simple representation of the text screen with support for updating * before printing. * * @author Samuel A. Rebelsky * @version 1.0 of April 2006 */ public class Screen { // +------------------+------------------------------------- // | Design Decisions | // +------------------+ /* (1) The screen is represented as an array of StringBuffers, rather than a two-dimensional array of characters, to support what I think will be easier printing. (2) Entry 0 in the array is the bottom row, entry 1 is the next row up, and so on and so forth. This choice makes it easier to set values, but we'll need to be careful, when printing, to print the end of the array first. (3) We store the height and width of the screen in fields, even though we can easily determine them from the other fields. */ // +--------+----------------------------------------------- // | Fields | // +--------+ /** The height of the screen. */ int height; /** The width of the screen. */ int width; /** The characters on the screen itself. */ StringBuffer[] screen; // +--------------+----------------------------------------- // | Constructors | // +--------------+ /** * Build a screen of specified height and width. */ public Screen(int _width, int _height) { this.width = _width; this.height = _height; // Create a string filled with spaces of the appropriate width. String spaces = " "; while (spaces.length() < this.width) { spaces = spaces.concat(spaces); } spaces = spaces.substring(0,this.width); // Create an array of string buffers, each of which has spaces this.screen = new StringBuffer[this.height]; for (int i = 0; i < this.height; i++) { this.screen[i] = new StringBuffer(spaces); } // for } // Screen(int, int) // +------------------+------------------------------------- // | Standard Methods | // +------------------+ /** * Convert to a string (e.g., for printing). */ public String toString() { String result = ""; for (int i = 0; i < this.height; i++) { result = this.screen[i].toString() + "\n" + result; } return result; } // toString() // +----------------+--------------------------------------- // | Public Methods | // +----------------+ /** * Determine the height of the screen. */ public int getHeight() { return this.height; } // getHeight() /** * Determine the width of the screen. */ public int getWidth() { return this.width; } // getWidth() /** * Set a particular character. If x and y are not within the * bounds of the screen, ignores them. */ public void setCharAt(int x, int y, char c) { if ((0 <= x) && (x < this.width) && (0 <= y) && (y < this.height)) { this.screen[y].setCharAt(x,c); } // if } // setCharAt(int, int, char) } // class Screen