Skip to content

Commit

Permalink
Fixed typecasting in MessageReader.
Browse files Browse the repository at this point in the history
Added args with a parser to the main in LocalEngine (--help to get started).
Re-added the jScrollPane to CellGrid for large numbers of cells.
  • Loading branch information
pbatzel authored and pbatzel committed Dec 7, 2009
1 parent 8128167 commit 6b67790
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 35 deletions.
62 changes: 58 additions & 4 deletions src/engine/LocalEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class LocalEngine extends Engine {
ArrayList<RemoteEngine> peerList;
int globalWidth;
int globalHeight;
int stopTurn = 50;
public int turn = 0;
boolean rollback = false;
HashMap<Integer, ArrayList<byte[]>> states;
Expand Down Expand Up @@ -160,7 +161,7 @@ private void fossilCollect(){
public void go() {

while (true) {
while (turn < 50) {
while (turn < stopTurn) {
if (!rollback) {
turn++;
saveState();
Expand Down Expand Up @@ -370,13 +371,66 @@ public static void main(String[] args) {
int port = 1234;
LocalEngine engine = null;
boolean isClient = false;
String IP = null;

int i=0;
String arg;

//check for different --types in args[]
while(i<args.length && args[i].startsWith("--")){
arg = args[i++];

if(arg.equals("--help")){
System.out.println("Usage: LocalEngine: [--isClient] IPAddress [--setSize] width height " +
"[--port] portNum");
System.exit(0);
}
else if(arg.equals("--isClient")){
if(i < args.length){
isClient = true;
IP = args[i++];
}
else{
System.err.println("--isClient requires [IP]");
System.exit(0);
}
}

//note: if --setSize is called by the client it probably shouldn't change
//the size of it's screen. Not sure how to handle this with shared code.
else if(arg.equals("--setSize")){
if((i+1)<args.length){
globalWidth = Integer.parseInt(args[i++]);
globalHeight = Integer.parseInt(args[i++]);
}
else{
System.err.println("--setSize requres [width height]");
System.exit(0);
}
}
//not sure if we even want to include this with the random porting idea
else if(arg.equals("--setPort")){
if(i < args.length){
port = Integer.parseInt(args[i++]);
}
else{
System.out.println("--setPort requires [portNum]");
System.exit(0);
}
}
else{
System.out.println("Usage: LocalEngine: [--isClient] IPAddress [--setSize] width height " +
"[--port] portNum");
System.out.println("Default values will be used.");
//could just System.exit(0); if defaults aren't to be used
}
}
try {

// Client case
if (args.length == 1) {
isClient = true;
if (isClient) {
// Use multicast instead.
InetAddress other = InetAddress.getByName(args[0]);
InetAddress other = InetAddress.getByName(IP);
Socket socket = new Socket(other, port);
// TODO Remove magic number.
RemoteEngine server = new RemoteEngine(socket);
Expand Down
2 changes: 1 addition & 1 deletion src/net/MessageReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void run() {
int messageType = in.read();
switch (messageType) {
case Message.SENDAGENT:
Message message = new Message(engine.turn, messageType);
Message message = new Message(engine.turn, (byte) messageType);
message.recvAgent(in);
synchronized (recvdMessages) {
if (!recvdMessages.remove(message)) {
Expand Down
51 changes: 21 additions & 30 deletions src/ui/CellGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;

public class CellGrid extends JFrame {
Expand All @@ -26,51 +27,41 @@ public CellGrid(int rows, int cols, int tlx, int tly) {
// int xCellSize = 65;
// int yCellSize = 65;

JPanel panel1 = new JPanel(); //just a holder
GridLayout layout1 = new GridLayout(rows, cols, 5, 5); // sets the width
// and height
this.setLayout(layout1); // adds the layout we just made
this.setBackground(Color.black); // sets the background black
setTitle("CABS - " + tlx + "," + tly + " " + rows + "x" + cols); // sets
// the
// title
// setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
LineBorder bord1 = new LineBorder(Color.black, 5); // border because it
// looked funny
// before
// this.setBorder(bord1); //adds the border
// JScrollPane scroller = new JScrollPane(panel1); //makes a scroll pane
// from the panel
// add(scroller); //adds the scroll pane to CellGrid
panel1.setLayout(layout1); // adds the layout we just made
panel1.setBackground(Color.black); // sets the background black
setTitle("CABS - " + tlx + "," + tly + " " + rows + "x" + cols);
// sets the title
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
LineBorder bord1 = new LineBorder(Color.black, 5);
// border because it looked funny before
panel1.setBorder(bord1); //adds the border
JScrollPane scroller = new JScrollPane(panel1);
//makes a scroll pane from the panel
add(scroller); //adds the scroll pane to CellGrid
pack();
myList = // array list of array lists
new ArrayList<ArrayList<myJCanvas>>();

setSize(500, 500);

// for(int i=0;i<rows;i++) //add the second set of array lists to the
// first
// myList.add(new ArrayList<myJCanvas>());
for (int x = 0; x < rows; x++) {
myList.add(new ArrayList<myJCanvas>());
for (int y = 0; y < cols; y++) { // steps through the lists and
// makes canvases
for (int y = 0; y < cols; y++) {
// steps through the lists and makes canvases
myList.get(x).add(new myJCanvas());
this.add(myList.get(x).get(y)); // adds the canvases to the
// panel
panel1.add(myList.get(x).get(y));
// adds the canvases to the panel
}
}
setVisible(true); // make the JFrame visible
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

/*
* public void draw(LocalCell[][] cell){ for (int i = 0; i < cell.x; i++) {
* for (int j = 0; j < this.width; j++) { cells[i][j] = new LocalCell(tlx +
* j, tly + i, this); } } }
*/
public void setColor(int xPos, int yPos, Color color) {
// myList.get(yPos-tly).get(xPos-tlx).setBackground(color);
myList.get(yPos).get(xPos).setBackground(color);
}

Expand All @@ -80,11 +71,11 @@ public void setColor(int xPos, int yPos, Color color) {
// rabbits, 3 grass
public class myJCanvas extends JPanel {
public myJCanvas(int tempWidth, int tempHeight) {
this.setSize(tempWidth, tempHeight); // sizes that do nothing
// because of GridLayout
this.setSize(tempWidth, tempHeight);
// sizes that do nothing because of GridLayout
this.setBackground(empty); // empty cell color
this.setMaximumSize(new Dimension(25, 25)); // doesn't matter
// because of GridLayout
this.setMaximumSize(new Dimension(25, 25));
// doesn't matter because of GridLayout
this.setMinimumSize(new Dimension(60, 60));
this.setPreferredSize(new Dimension(60, 60));
}
Expand Down

0 comments on commit 6b67790

Please sign in to comment.