Driver.java creates an instance of the ScapeG class. import javax.swing.JFrame; import java.io.*; public class Driver { public static void main(String[] args) { JFrame frame = new JFrame("Andy Menke's Sugarscape v0.2"); frame.setSize(800, 800); frame.setLocation(0, 0); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try { frame.setContentPane(new ScapeG(new Scape(50, 50, "DefaultScape.txt"))); } catch(IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } frame.setVisible(true); } } ScapeG.java controls all of the graphical aspects of my program. It contains an instance of the Scape class, which actually processes everything. import java.util.ArrayList; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; //cell: 20 x 20 public class ScapeG extends JPanel { private Scape myScape; private BufferedImage myImage; private Graphics myBuffer; private Timer t; public ScapeG(Scape s) { myScape = s; try { myScape.addAgent(new Agent(myScape,8,8,0, new Location(7, 0, myScape))); } catch(Exception e) { System.err.println("Caught Exception: " + e.getMessage()); } myImage = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB); myBuffer = myImage.getGraphics(); myBuffer.setColor(Color.white); myBuffer.fillRect(0, 0, 800, 800); t = new Timer(100, new TimeListener()); t.start(); } public void paintComponent(Graphics g) { g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null); } public void clearBuffer() { myBuffer.setColor(Color.white); myBuffer.fillRect(0, 0, 800, 800); } public void display(Scape s) //This method actually displays what is going on in the Scape program. { Location[][] locs = s.getLocs(); ArrayList agents = s.getAgents(); myBuffer.setColor(Color.yellow); for(int i=0; i=0 && y=0 && x=1 && v<=6 && m>=1 && m<=4) { myVision=v; myMetabolism=m; } else { throw new Exception("Invalid vision or metabolism"); System.exit(1); } myLoc=l; mySugar=s; } public Agent(Scape sc, Location l) { myScape=sc; myLoc=l; myVision=(int)(Math.random()*6)+1; myMetabolism=(int)(Math.random()*4)+1; mySugar=20; } public Location getLoc() { return myLoc; } private void eat() { mySugar=mySugar-myMetabolism; } private void collect() { mySugar+=myLoc.takeSugar(); } public boolean rule() //general strategy { this.M(); this.collect(); this.eat(); return mySugar <= 0; } //movement rules public void M() //Agent finds nearest location with maximal amount of sugar, then moves there. { int max=-1; Location maxLoc = myLoc; Location pos=myLoc; int[] dirs = {0,1,2,3}; dirs=randomize(dirs); for(int k=0; k<=myVision; k++) { for(int n=0; n<4; n++) { Location tmp=checkDir(k,dirs[n]); if(tmp != null && tmp.getSugar()>max) { max=tmp.getSugar(); maxLoc=tmp; } } } myLoc=maxLoc; } //end movement rules private Location checkDir(int dist, int dir) // This gets the location a given distance away in a given direction. { int x=myLoc.getX(); int y=myLoc.getY(); switch(dir) { case 1: y-=dist; break; case 2: x+=dist; break; case 3: y+=dist; break; case 4: x-=dist; break; default: break; } return myScape.getLocation(y,x); } private int[] randomize(int[] a) //This method randomizes the order of an array. For an agent, this will randomize the order of directions that he //checks. { int[] ret = new int[a.length]; ArrayList tmp = new ArrayList(); for(int k=0; k0; m--) { int val=r.nextInt(m); Integer i=(Integer)tmp.get(val); tmp.remove(val); ret[a.length-m]=i.intValue(); } //print(ret); return ret; } private void print(int[] a) //For debugging. { for(int k=0; k