import java.applet.*; import java.awt.*; // See also: Board.java, screenObject.java /****************************************************************************\ | Particle Board version 1.0 | | Programmed by Trevor Stone for CSCI 1300, Spring 1997, Karl Winklman prof. | | Programmed 100% in BBEdit Lite on a Macintosh, no program generated code! | | Compiled on Sun's Java Developer's Kit 1.0.2 Available on the web at | | http://robin.ml.org/~tstone/particle/ tstone@Colorado.EDU | \****************************************************************************/ // Similar to Karl's Coords class. Handles things like object positions public class Screen { // quasi-enummeration public static int LEFT = 0; public static int UP = 1; public static int RIGHT = 2; public static int DOWN = 3; private int displayWidth; // pixels private int displayHeight; private int displayCols; // rows/cols, 7 pixels each private int displayRows; private int squareSize; private int zeroX; // position of top left of grid private int zeroY; private int holderLength; // holding pins at bottom private screenObject [] [] screen_objects; // where it's all stored // constructor Screen (int width, int height) { squareSize = 7; // nice number, allows for good drawing //location of (0,0) if it existed zeroX = 0; zeroY = 30; //to leave space for menus, buttons, etc. displayWidth = width; displayHeight = height; displayCols = displayWidth /squareSize - zeroX/squareSize; displayRows = displayHeight/squareSize - zeroY/squareSize; while (displayCols*squareSize +zeroX > displayWidth) displayCols--; while (displayRows*squareSize +zeroY > displayHeight) displayRows--; // holding pen length (8th of screen works well) holderLength = displayRows / 8 + 1; screen_objects = new screenObject [displayRows] [displayCols]; } public void initializeBoard (Graphics g, Screen s) // called by init() { g.setColor (Color.lightGray); g.fillRect (0, 0, displayWidth, displayHeight); int i, j; for (i = 1; i <= displayRows; i++) for (j = 1; j <= displayCols; j++) { screen_objects [i-1][j-1] = new screenObject (i, j, "Blank", s, g); screen_objects [i-1][j-1].type ("Blank"); //doesn't like above init screen_objects [i-1][j-1].draw (); } // board borders g.setColor (Color.green); g.drawRect (zeroX, zeroY, displayCols()*squareSize()-1, displayRows()*squareSize()); } /******** Coordinate to row/col. Almost identical to Karl's ********/ public int x (int col) // return mouse x coord { return zeroX + squareSize * col; } public int y (int row) // return mouse y coord { return zeroY + squareSize * row; } public int row (int y) // given mouse y, find row { if (y > zeroY) return (y - zeroY + squareSize / 2) / squareSize; else return - ((zeroY - y + squareSize / 2) / squareSize); } public int col (int x) // given mouse x, find col { if (x > zeroX) return (x - zeroX + squareSize / 2) / squareSize; else return - ((zeroX - x + squareSize / 2) / squareSize); } /************** Functions dealing with variables ****************/ public int displayWidth() { return displayWidth; } public int displayHeight() { return displayHeight; } public int displayCols() { return displayCols; } public int displayRows() { return displayRows; } public int squareSize () // how big are squares? { return squareSize; } public int holderLength () { return holderLength; } /************************ Functions for objects *********************/ public screenObject getScreenObject (int row, int col) { return screen_objects [row -1] [col -1]; } public void putScreenObject (int row, int col, String obj) { if (onGrid (row, col) && !obstructed (row, col)) screen_objects [row -1] [col -1].type(obj); } public void removeScreenObject (int row, int col) { if (onGrid (row, col)) screen_objects [row -1] [col -1].type("Blank"); } public boolean onGrid (int row, int col) { return 1 <= row && row <= displayRows && 1 <= col && col <= displayCols; } public boolean obstructed (int row, int col) { if (getScreenObject (row, col).type().equals("Blank")) return false; else return true; } }