import java.applet.*; import java.awt.*; // screenObject is the class of objects. See also Board.java and Screen.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 | \****************************************************************************/ public class screenObject { // quasi-enummeration public final static int LEFT = 0; public final static int UP = 1; public final static int RIGHT = 2; public final static int DOWN = 3; private int row; // location private int col; private String type; // what kind private Screen screen; // so we can deal with Screen functions private Graphics graph; // so we can draw the objects // constructor public screenObject (int _row, int _col, String _type, Screen _screen, Graphics _graph) { row = _row; col = _col; type = _type; screen = _screen; graph = _graph; } /******************** Variable related functions ********************/ public String type () // what type of object? { return type; } public void type (String _type) // lets others change type { type = _type; } public int row () { return row; } public int col () { return col; } /******************** Movement related functions ********************/ public void move (int dir) // move one step { switch (dir) { case LEFT : if (check (LEFT)) { erase(); screen.putScreenObject (row, --col, type); draw(); } else bounce(LEFT); break; case UP : if (check (UP)) { erase(); screen.putScreenObject (--row, col, type); draw(); } else bounce(UP); break; case RIGHT : if (check (RIGHT)) { erase(); screen.putScreenObject (row, ++col, type); draw(); } else bounce(RIGHT); break; case DOWN : if (check (DOWN)) { erase(); screen.putScreenObject (++row, col, type); draw(); } else bounce (DOWN); break; } } public boolean check (int dir) // can I move this way? { switch (dir) { case LEFT : return (screen.onGrid (row, col -1) && !screen.obstructed (row, col -1)); case UP : return (screen.onGrid (row -1, col) && !screen.obstructed (row -1, col)); case RIGHT : return (screen.onGrid (row, col +1) && !screen.obstructed (row, col +1)); case DOWN : return (screen.onGrid (row +1, col) && !screen.obstructed (row +1, col)); default : return false; } } private void bounce (int direction) // something in the way, go left/right { double dir = Math.round(Math.random()); // go direction 0 or 1? switch (direction) { case LEFT : if (dir == 0) { if (check(DOWN)) move (DOWN); else //do nothing if something in the way return; } else if (dir == 1) { if (check(UP)) move (UP); else return; } break; case UP : if (dir == 0) { if (check(LEFT)) move (LEFT); else return; } else if (dir == 1) { if (check(RIGHT)) move (RIGHT); else return; } break; case RIGHT : if (dir == 0) { if (check(UP)) move (UP); else return; } else if (dir == 1) { if (check(DOWN)) move (DOWN); else return; } break; case DOWN : if (dir == 0) { if (check(RIGHT)) move (RIGHT); else return; } else if (dir == 1) { if (check(LEFT)) move (LEFT); else return; } break; default : break; } } /******************** Drawing related functions *********************/ public char objChar () // take type and make a char so switch() works { if (type().equals("Blank")) return ' '; else if (type().equals("Pin")) return '.'; else if (type().equals("Particle")) return 'o'; else if (type().equals("vLine")) return '|'; else if (type().equals("hLine")) return '-'; else return '_'; // something wrong, will display as a black square } public void draw () // display on screen { int size = screen.squareSize(); int X = screen.x(col); int Y = screen.y(row); char o = objChar (); switch (o) { case ' ' : // blank graph.setColor (Color.white); graph.drawRect (X -size+1, Y -size+1, size-1, size-1); graph.fillRect (X -size+2, Y -size+2, size-1, size-1); break; case 'o' : // particle graph.setColor (Color.red); graph.fillOval (X -size+2, Y -size+2, size-2,size-2); break; case '.' : // pin graph.setColor (Color.blue); graph.fillOval (X -size+2, Y -size+2, size-3, size-3); break; case '|' : // vertical line graph.setColor (Color.green); graph.drawLine (X -size/2, Y -size+1, X-size/2, Y); break; case '-' : // horizontal line graph.setColor (Color.green); graph.drawLine (X -size+1, Y-size/2, X, Y-size/2); break; default : // something wrong graph.setColor (Color.black); graph.drawRect (X -size/2, Y -size/2, size-2, size-2); graph.fillRect (X -size/2+1, Y -size/2+1, size-2,size-2); break; } } public void erase () // same as draw, but in white { // not just a square, exact object in white screen.removeScreenObject (row, col); // tell screen not there int size = screen.squareSize(); int X = screen.x (col); int Y = screen.y (row); char o = objChar (); switch (o) { case ' ' : // blank graph.setColor (Color.white); graph.drawRect (X -size+1, Y -size+1, size-1, size-1); graph.fillRect (X -size+2, Y -size+2, size-1, size-1); break; case 'o' : // particle graph.setColor (Color.white); graph.fillOval (X -size+1, Y -size+1, size-1,size-1); break; case '.' : // pin graph.setColor (Color.white); graph.fillOval (X -size+2, Y -size+2, size-3, size-3); break; case '|' : // vertical line graph.setColor (Color.white); graph.drawLine (X-size/2, Y -size+1, X, Y-size/2); break; case '-' : // horizontal line graph.setColor (Color.white); graph.drawLine (X -size+1, Y-size/2, X, Y-size/2); break; default : // mistake graph.setColor (Color.black); graph.drawString ("Oops!", screen.displayWidth()/2, screen.displayHeight()/2); graph.drawRect (X -size/2, Y -size/2, size-2, size-2); graph.fillRect (X -size/2+1, Y -size/2+1, size-2,size-2); break; } } }