Skip to content

Commit

Permalink
The GUI has been added to the working program. I also added some idea…
Browse files Browse the repository at this point in the history
…s for a more complex Rabbit agent.
  • Loading branch information
pbatzel authored and pbatzel committed Dec 2, 2009
1 parent e589999 commit d5d1c1c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
32 changes: 26 additions & 6 deletions CellGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@
public class CellGrid extends JFrame{
private final int WINDOW_WIDTH = 400; //size of the JFrame
private final int WINDOW_HEIGHT = 400;
public static final Color empty = Color.darkGray;
public static final Color agent1 = Color.blue;
public static final Color agent2 = Color.yellow;
public static final Color mixed = Color.green;
ArrayList<ArrayList<myJCanvas>> myList;
private int tlx;
private int tly;

//Takes the number of x and y cells (width and height) and makes the grid
public CellGrid(int rows, int cols){
public CellGrid(int rows, int cols, int tlx, int tly){

//int xCellSize = WINDOW_WIDTH/(xCells+1); //gridlayout auto-resizes
//int yCellSize = WINDOW_HEIGHT/(yCells+1);
//int xCellSize = 65;
//int yCellSize = 65;
this.tlx = tlx;
this.tly = tly;

JPanel panel1 = new JPanel(); //just a holder
GridLayout layout1 = new GridLayout(rows,cols,5,5); //sets the width and height
Expand All @@ -33,6 +41,10 @@ public CellGrid(int rows, int cols){

myList = //array list of array lists
new ArrayList<ArrayList<myJCanvas>>();

setSize(500,500);
setVisible(true); //make the JFrame visible
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//for(int i=0;i<rows;i++) //add the second set of array lists to the first
// myList.add(new ArrayList<myJCanvas>());
Expand All @@ -45,8 +57,17 @@ public CellGrid(int rows, int cols){
}

}

/*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).get(xPos).setBackground(color);
myList.get(yPos-tly).get(xPos-tlx).setBackground(color);
//myList.get(yPos).get(xPos).setBackground(color);
}

//could extend anything that's Swing (not awt)
Expand All @@ -55,10 +76,9 @@ public void setColor(int xPos, int yPos, Color color){
public class myJCanvas extends JPanel{
public myJCanvas(int tempWidth, int tempHeight){
this.setSize(tempWidth, tempHeight); //sizes that do nothing because of GridLayout
this.setBackground(Color.blue); //default color
this.setBackground(empty); //empty cell color
this.setMaximumSize(new Dimension(25,25)); //doesn't matter because of GridLayout
this.setMinimumSize(new Dimension(60,60));

this.setPreferredSize(new Dimension(60,60));
}
public myJCanvas(){
Expand All @@ -68,7 +88,7 @@ public myJCanvas(){
}

//Just for demonstration/debug
public static void main(String[] args) throws InterruptedException {
/* public static void main(String[] args) throws InterruptedException {
int rows = 20;
int cols = 10;
CellGrid gpw = new CellGrid(rows, cols); //make my CellGrid
Expand All @@ -81,5 +101,5 @@ public static void main(String[] args) throws InterruptedException {
gpw.setColor(1, 1, Color.BLACK);
gpw.setColor(2,2,Color.BLACK);
}
}*/
}
33 changes: 33 additions & 0 deletions ComplexRabbit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.Random;


public class ComplexRabbit extends Agent{
final int deathFromAge = 20;
Random r = new Random();
int energy = 75;
int age = 0;

public void go(){

// move(r.nextInt(5),r.nextInt(5));
move(1,0);
energy = energy - 10;
age++;

if(energy >= 100){
reproduce();
}

if(age == 20){
die();
}
}

public void reproduce(){
energy-=25;
}

public void die(){

}
}
4 changes: 2 additions & 2 deletions LocalCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public byte[] serialize() {
oos.writeInt(x);
oos.writeInt(y);
oos.writeInt(agents.size());
if(agents.size() != 0) {
/*if(agents.size() != 0) {
System.err.println(MessageFormat.format("Serializing cell ({0}, {1}); {2} agents.",
x, y, agents.size()));
}
}*/
for( Agent a : agents){
oos.writeObject(a);
}
Expand Down
15 changes: 10 additions & 5 deletions LocalEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class LocalEngine extends Engine {
int turn = 0;
boolean rollback = false;
HashMap<Integer, ArrayList<byte[]>> states;
CellGrid gui;

public LocalEngine(int tlx, int tly, int width, int height, int globalWidth, int globalHeight) {
super(tlx, tly, width, height);
Expand All @@ -29,6 +30,7 @@ public LocalEngine(int tlx, int tly, int width, int height, int globalWidth, int
this.globalHeight = globalHeight;
peerList = new ArrayList<RemoteEngine>();
cells = new LocalCell[height][width];
gui = new CellGrid(this.height, this.width, tlx, tly);
for (int i = 0; i < this.height; i++) {
for (int j = 0; j < this.width; j++) {
cells[i][j] = new LocalCell(tlx + j, tly + i, this);
Expand All @@ -53,16 +55,16 @@ private void rollback(int turn){
rollback = true;
ArrayList<byte[]> state = states.get(turn);
for( byte[] b : state){
System.err.println("The byte array is of length " + b.length);
//System.err.println("The byte array is of length " + b.length);
ByteArrayInputStream s = new ByteArrayInputStream(b);
try {
ObjectInputStream ois = new ObjectInputStream(s);
int x = ois.readInt();
int y = ois.readInt();
int count = ois.readInt();
System.err.println(MessageFormat.format(
/*System.err.println(MessageFormat.format(
"Rolling back cell ({0}, {1}); {2} agents.", x,
y, count));
y, count));*/
LocalCell cell = getCell(x,y);
cell.agents.clear();
while(count-- != 0){
Expand Down Expand Up @@ -110,7 +112,6 @@ public void go() {
}
handleMessages();
print();
//TODO: Display to GUI.
}
}

Expand Down Expand Up @@ -172,8 +173,10 @@ public void print() {
LocalCell cell = cells[i][j];
if (cell.agents.size() > 0) {
System.out.print("* ");
gui.setColor(j, i, CellGrid.agent1);
} else {
System.out.print("- ");
gui.setColor(j, i, CellGrid.empty);
}
}
System.out.println();
Expand Down Expand Up @@ -216,13 +219,15 @@ private void sendCells(RemoteEngine remote){
for(Agent a : cell.agents){
Protocol.sendAgent(remote.out, cell.x, cell.y, a);
}
}
}
}
remote.setCoordinates(rTlx,rTly,rWidth,rHeight);
this.peerList.add(remote);
//TODO: Actually change the size of the data structure that
//holds the cells.
this.width = this.width - rWidth;
gui.dispose();
gui = new CellGrid(height, width, tlx, tly);

}

Expand Down

0 comments on commit d5d1c1c

Please sign in to comment.