To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/17.1/src/ants/Grid.java. This is a spam-protection measure; sorry for the inconvenience.

· Grid.java ·

   1/*
   2 * Grid.java
   3 *
   4 * Created on 07 October 2006, 01:36
   5 *
   6 * To change this template, choose Tools | Template Manager
   7 * and open the template in the editor.
   8 */
   9
  10package ants;
  11
  12import ants.control.AntControl;
  13import ants.event.AntEvent;
  14import ants.event.AntListener;
  15import ants.event.FoodUpdateEvent;
  16import java.awt.Canvas;
  17import java.awt.Color;
  18import java.awt.Graphics;
  19import java.awt.Point;
  20import java.awt.event.MouseAdapter;
  21import java.awt.event.MouseEvent;
  22import java.awt.image.BufferedImage;
  23import java.util.Vector;
  24
  25/**
  26 *
  27 * @author James Hamilton
  28 */
  29public class Grid extends Canvas implements Runnable {
  30    
  31    private GridSquare[][] grid;
  32    private GridVector gridVector;
  33    
  34    private BufferedImage bi;
  35    private Graphics big;
  36    private Thread animate;
  37    private Thread resetFoodStrength;
  38    
  39    private static Grid singleton = null;
  40    private Vector<MyObject> objects;
  41    
  42    private Vector<FoodCluster> foodClusters;
  43    
  44    private Nest nest;
  45    
  46    private int size = 100;
  47    
  48    /** Creates a new instance of Grid */
  49    private Grid(int size) {
  50        this.size = size;
  51        initialise();
  52    }
  53    
  54    private void initialise() {
  55        grid = new GridSquare[size][size];
  56        setGridVector(new GridVector());
  57        setSize(size * GridSquare.SIZE, size * GridSquare.SIZE);
  58        
  59        for(int x = 0; x < grid.length; x++) {
  60            for(int y = 0; y < grid.length; y++) {
  61                
  62                grid[x][y] = new GridSquare();
  63                grid[x][y].setLocation(x * GridSquare.SIZE, y * GridSquare.SIZE);
  64                getGridSquares().add(grid[x][y]);
  65            }
  66            
  67        }
  68        bi = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
  69        big = bi.createGraphics();
  70        
  71        setNest(new Nest(grid[20][20]));
  72      
  73        objects = new Vector<MyObject>();
  74        objects.add(getNest());
  75        
  76        
  77        
  78        setFoodClusters(new Vector<FoodCluster>());
  79        
  80        animate = null;
  81        animate = new Thread(this);
  82        animate.start();    
  83        
  84        addMouseListener( new MouseAdapter() {
  85            public void mouseClicked(MouseEvent e) {
  86                System.out.println(getGridSquareAt(e.getX(), e.getY()));
  87            }
  88        });
  89        
  90        addAntListener(AntControl.getInstance());
  91    }
  92
  93    private Vector<AntListener> listeners = new Vector<AntListener>();
  94    
  95    public void addAntListener(AntListener listener) {
  96        listeners.add(listener);
  97    }
  98    
  99    public void notifyListeners(AntEvent e) {
 100        for(AntListener listener : listeners)
 101            listener.antEventHandler(e);
 102    }
 103    
 104    public synchronized static Grid getInstance(int n) {
 105        if(singleton == null)
 106            singleton = new Grid(n);
 107        return singleton;
 108    }
 109    
 110    public synchronized static Grid getInstance() {
 111        if(singleton == null)
 112            singleton = new Grid(100);
 113        return singleton;
 114    }
 115    
 116    public void reset() {
 117        initialise();
 118    }
 119
 120    public void recalculateFoodStrengths() {
 121        for(GridSquare gs : getGridSquares()) {
 122            gs.recalculateFoodStrength();
 123        }
 124    }
 125    
 126    public void setFoodStrengthRadius(int radius) {
 127        for(FoodCluster cluster : foodClusters) {
 128            cluster.setStrengthRadius(radius);
 129        }      
 130    }
 131    
 132    public void resetFoodStrengths() {
 133        recalculateFoodStrengths();
 134    }
 135    
 136    public synchronized GridSquare getGridSquare(int x, int y) throws Exception {
 137        try {
 138            return grid[x][y];
 139        }catch (ArrayIndexOutOfBoundsException e) {
 140            throw new Exception("Invalid Square");
 141        }
 142    }
 143    
 144    public GridSquare getGridSquare(Point p) throws Exception {
 145        return getGridSquare(p.x, p.y);
 146    }
 147
 148    public GridSquare getGridSquareAt(int x, int y) {
 149        for(GridSquare gs : gridVector) {
 150            if(gs.contains(x, y)) return gs;
 151        }
 152        return null;
 153    }
 154    
 155    public Vector<MyObject> getObjects() {
 156        return objects;
 157    }
 158    
 159    public void addObject(MyObject object) {
 160        objects.add(object);
 161    }
 162    
 163    public void populate(int n) {
 164
 165        
 166    }
 167    
 168    public void populateWithFood(int n) {
 169        
 170        for(int j = 0; j < n; j++) {
 171            
 172            boolean stop = false;
 173            
 174            while(!stop) {
 175                
 176                int x = (int)(Math.random() * grid.length);
 177                int y = (int)(Math.random() * grid.length);
 178                
 179                if(grid[x][y].empty()) {
 180
 181                    int size = (int)(Math.random() * 5) + 1;
 182                    
 183                    try {
 184                        addFoodCluster(x, y, size);
 185                    }catch (Exception e) {
 186                        //no such gridsquare
 187                    }
 188                    
 189       
 190                    stop = true;
 191                }
 192            }
 193        }
 194        
 195        this.notifyListeners(new FoodUpdateEvent(this));
 196    }
 197 
 198    public void addFoodCluster(int x, int y, int radius) throws Exception {
 199        
 200        getGridSquare(x, y).getGridSquares(radius, true).getFreeGridSquares().addFood();
 201        
 202        notifyListeners(new FoodUpdateEvent(this));
 203    }
 204    
 205    public void addObstacleCluster(int x, int y, int radius) throws Exception {
 206        getGridSquare(x, y).getGridSquares(radius, true).addObstacles(); 
 207    }
 208    
 209    public void populateWithObstacles(int n) {
 210        
 211        for(int j = 0; j < n; j++) {
 212            
 213            boolean stop = false;
 214            
 215            while(!stop) {
 216                
 217                int x = (int)(Math.random() * grid.length);
 218                int y = (int)(Math.random() * grid.length);
 219                
 220                if(grid[x][y].empty()) {
 221                    try {
 222                        addObstacleCluster(x, y, 3);
 223                    }catch (Exception e) {
 224                        //no such gridsquare
 225                    }
 226                    
 227            
 228                    stop = true;
 229                }
 230            }
 231        }
 232        
 233        for(int x = 0; x < grid.length; x++) {
 234            try {
 235                Obstacle obstacle = new Obstacle(getGridSquare(x, 0));
 236                objects.add(obstacle);
 237                getGridSquare(x, 0).setObject(obstacle);
 238                
 239                Obstacle obstacle1 = new Obstacle(getGridSquare(x, grid.length-1));
 240                objects.add(obstacle1);
 241                getGridSquare(x,grid.length-1).setObject(obstacle1);                
 242                
 243            }catch (Exception e) {
 244                //
 245            }
 246        }
 247 
 248       for(int y = 0; y < grid.length; y++) {
 249            try {
 250                Obstacle obstacle = new Obstacle(getGridSquare(0, y));
 251                objects.add(obstacle);
 252                getGridSquare(0, y).setObject(obstacle);
 253                
 254                Obstacle obstacle1 = new Obstacle(getGridSquare(grid.length-1, y));
 255                objects.add(obstacle1);
 256                getGridSquare(grid.length-1,y).setObject(obstacle1);                
 257                
 258            }catch (Exception e) {
 259                //
 260            }
 261        }
 262        
 263
 264    }
 265    
 266    
 267    public int countAnts() {
 268        int count = 0;
 269        for(int x = 0; x < grid.length; x++) {
 270            for(int y = 0; y < grid.length; y++) {
 271                if(grid[x][y].containsAnt()) count++;
 272            }
 273        }
 274        return count;
 275    }
 276    
 277    public int countCarriedFood() {
 278        int count = 0;
 279        for(int x = 0; x < grid.length; x++) {
 280            for(int y = 0; y < grid.length; y++) {
 281                if(grid[x][y].containsAnt() && grid[x][y].getAnt().carryingFood()) count++;
 282            }
 283        }
 284        return count;
 285    }
 286    
 287    public int countNests() {
 288        int count = 0;
 289        for(int x = 0; x < grid.length; x++) {
 290            for(int y = 0; y < grid.length; y++) {
 291                if(grid[x][y].isNest()) count++;
 292            }
 293        }
 294        return count;
 295    }
 296    
 297    
 298    
 299    public void setAntAt(Ant ant, int x, int y) {
 300        grid[x][y].setAnt(ant);
 301    }
 302    
 303    
 304    public void paint(Graphics g) {
 305        update(g);
 306    }
 307    
 308    public void update(Graphics g) {
 309        clear();
 310         
 311        for(GridSquare object : gridVector) {
 312            
 313            object.draw(big);
 314            
 315        }
 316        
 317        g.drawImage(bi, 0, 0, this);
 318    }
 319    
 320    public void clear() {
 321        big.setColor(Color.black);
 322        big.fillRect(0,0,getWidth(),getHeight());
 323    }
 324    
 325    public void run() {
 326        while(animate!=null) {
 327            
 328            try {
 329                animate.sleep(300);
 330                
 331                
 332                Thread.yield();
 333                //printStats();
 334                
 335                repaint();
 336            }catch (Exception e) {
 337                System.out.println(e);
 338            }
 339            
 340        }
 341        big.dispose();
 342    }
 343
 344    
 345    public void printStats() {
 346        System.out.print(countAnts() + " ants. ");
 347        System.out.print(countCarriedFood() + " carrying food");
 348        System.out.println();
 349    }
 350
 351    public Nest getNest() {
 352        return nest;
 353    }
 354
 355    public void setNest(Nest nest) {
 356        this.nest = nest;
 357    }
 358
 359    public Vector<FoodCluster> getFoodClusters() {
 360        return foodClusters;
 361    }
 362
 363    public void setFoodClusters(Vector<FoodCluster> foodClusters) {
 364        this.foodClusters = foodClusters;
 365    }
 366
 367    public GridVector getGridSquares() {
 368        return gridVector;
 369    }
 370    
 371    public GridVector getGridSquaresWithFood() {
 372        return getGridSquares().getFoodGridSquares();
 373    }
 374    
 375    public int countFood() {
 376        return getGridSquaresWithFood().size();
 377    }
 378
 379    public void setGridVector(GridVector gridVector) {
 380        this.gridVector = gridVector;
 381    }
 382}
 383

· Grid.java ends ·

Generated by CHIP: Code Highlighting in PHP, version 2.7.0.