To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/17.0/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//        for(FoodCluster cluster : foodClusters) {
 125//            cluster.recalculateStrengths(true);
 126//        }
 127        
 128    }
 129    
 130    public void setFoodStrengthRadius(int radius) {
 131        for(FoodCluster cluster : foodClusters) {
 132            cluster.setStrengthRadius(radius);
 133        }      
 134    }
 135    
 136    public void resetFoodStrengths() {
 137        recalculateFoodStrengths();
 138//        for(GridSquare gs : gridVector) {
 139//            gs.setFoodStrength(0);
 140//        }        
 141    }
 142    
 143    public synchronized GridSquare getGridSquare(int x, int y) throws Exception {
 144        try {
 145            return grid[x][y];
 146        }catch (ArrayIndexOutOfBoundsException e) {
 147            throw new Exception("Invalid Square");
 148        }
 149    }
 150    
 151    public GridSquare getGridSquare(Point p) throws Exception {
 152        return getGridSquare(p.x, p.y);
 153    }
 154
 155    public GridSquare getGridSquareAt(int x, int y) {
 156        for(GridSquare gs : gridVector) {
 157            if(gs.contains(x, y)) return gs;
 158        }
 159        return null;
 160    }
 161    
 162    public Vector<MyObject> getObjects() {
 163        return objects;
 164    }
 165    
 166    public void addObject(MyObject object) {
 167        objects.add(object);
 168    }
 169    
 170    public void populate(int n) {
 171        
 172//        for(int i = 0; i < n; i++) {
 173//            
 174//       
 175//            Ant a = new Ant(grid[35][30]);
 176//            objects.add(a);
 177//          
 178//            a.start();
 179//       
 180//        }
 181        
 182//        Nest nest = new Nest(n);
 183//        nest.setGridSquare(grid[35][30]);
 184//        objects.add(nest);
 185        
 186    }
 187    
 188    public void populateWithFood(int n) {
 189        
 190        for(int j = 0; j < n; j++) {
 191            
 192            boolean stop = false;
 193            
 194            while(!stop) {
 195                
 196                int x = (int)(Math.random() * grid.length);
 197                int y = (int)(Math.random() * grid.length);
 198                
 199                if(grid[x][y].empty()) {
 200
 201                    int size = (int)(Math.random() * 5) + 1;
 202                    
 203                    try {
 204                        addFoodCluster(x, y, size);
 205                    }catch (Exception e) {
 206                        //no such gridsquare
 207                    }
 208                    
 209//                    GridVector gridSquares = grid[x][y].getGridSquares(size, true);
 210//             
 211//                    for(GridSquare gs : gridSquares.getFreeGridSquares()) {
 212//                     
 213//                            Food food2 = new Food(gs);
 214//                            gs.setFood(food2);
 215//                                 
 216//                    }
 217       
 218                    stop = true;
 219                }
 220            }
 221        }
 222        
 223        this.notifyListeners(new FoodUpdateEvent(this));
 224    }
 225 
 226    public void addFoodCluster(int x, int y, int radius) throws Exception {
 227        
 228        getGridSquare(x, y).getGridSquares(radius, true).getFreeGridSquares().addFood();
 229        
 230        notifyListeners(new FoodUpdateEvent(this));
 231    }
 232    
 233    public void addObstacleCluster(int x, int y, int radius) throws Exception {
 234        getGridSquare(x, y).getGridSquares(radius, true).addObstacles(); 
 235    }
 236    
 237    public void populateWithObstacles(int n) {
 238        
 239        for(int j = 0; j < n; j++) {
 240            
 241            boolean stop = false;
 242            
 243            while(!stop) {
 244                
 245                int x = (int)(Math.random() * grid.length);
 246                int y = (int)(Math.random() * grid.length);
 247                
 248                if(grid[x][y].empty()) {
 249                    try {
 250                        addObstacleCluster(x, y, 3);
 251                    }catch (Exception e) {
 252                        //no such gridsquare
 253                    }
 254                    
 255//                     for(GridSquare gs : grid[x][y].getGridSquares(3, true)) {
 256//
 257//                            Obstacle food2 = new Obstacle(gs);
 258//                            objects.add(food2);
 259//                            gs.setObject(food2);
 260//                   
 261//                    }                 
 262            
 263                    stop = true;
 264                }
 265            }
 266        }
 267        
 268        for(int x = 0; x < grid.length; x++) {
 269            try {
 270                Obstacle obstacle = new Obstacle(getGridSquare(x, 0));
 271                objects.add(obstacle);
 272                getGridSquare(x, 0).setObject(obstacle);
 273                
 274                Obstacle obstacle1 = new Obstacle(getGridSquare(x, grid.length-1));
 275                objects.add(obstacle1);
 276                getGridSquare(x,grid.length-1).setObject(obstacle1);                
 277                
 278            }catch (Exception e) {
 279                //
 280            }
 281        }
 282 
 283       for(int y = 0; y < grid.length; y++) {
 284            try {
 285                Obstacle obstacle = new Obstacle(getGridSquare(0, y));
 286                objects.add(obstacle);
 287                getGridSquare(0, y).setObject(obstacle);
 288                
 289                Obstacle obstacle1 = new Obstacle(getGridSquare(grid.length-1, y));
 290                objects.add(obstacle1);
 291                getGridSquare(grid.length-1,y).setObject(obstacle1);                
 292                
 293            }catch (Exception e) {
 294                //
 295            }
 296        }
 297        
 298
 299    }
 300    
 301    
 302    public int countAnts() {
 303        int count = 0;
 304        for(int x = 0; x < grid.length; x++) {
 305            for(int y = 0; y < grid.length; y++) {
 306                if(grid[x][y].containsAnt()) count++;
 307            }
 308        }
 309        return count;
 310    }
 311    
 312//    public int countFood() {
 313//        int count = 0;
 314//        for(int x = 0; x < grid.length; x++) {
 315//            for(int y = 0; y < grid.length; y++) {
 316//                if(grid[x][y].containsFood()) count++;
 317//            }
 318//        }
 319//        return count;
 320//    }
 321    
 322    public int countCarriedFood() {
 323        int count = 0;
 324        for(int x = 0; x < grid.length; x++) {
 325            for(int y = 0; y < grid.length; y++) {
 326                if(grid[x][y].containsAnt() && grid[x][y].getAnt().carryingFood()) count++;
 327            }
 328        }
 329        return count;
 330    }
 331    
 332    public int countNests() {
 333        int count = 0;
 334        for(int x = 0; x < grid.length; x++) {
 335            for(int y = 0; y < grid.length; y++) {
 336                if(grid[x][y].isNest()) count++;
 337            }
 338        }
 339        return count;
 340    }
 341    
 342    
 343    
 344    public void setAntAt(Ant ant, int x, int y) {
 345        grid[x][y].setAnt(ant);
 346    }
 347    
 348    
 349    public void paint(Graphics g) {
 350        update(g);
 351    }
 352    
 353    public void update(Graphics g) {
 354        clear();
 355        
 356        
 357//        for(MyObject object : objects) {
 358//            
 359//            object.draw(big);
 360//            
 361//        }
 362//        
 363        for(GridSquare object : gridVector) {
 364            
 365            object.draw(big);
 366            
 367        }
 368        
 369//        for(int x = 0; x < grid.length; x++) {
 370//            for(int y = 0; y < grid.length; y++) {
 371//                try {
 372//                    grid[x][y].draw(big);
 373//                }catch (NullPointerException e) {
 374//                    //emtpy
 375//                }
 376//            }
 377//        }
 378        g.drawImage(bi, 0, 0, this);
 379    }
 380    
 381    public void clear() {
 382        big.setColor(Color.black);
 383        big.fillRect(0,0,getWidth(),getHeight());
 384    }
 385    
 386    
 387    
 388    public void run() {
 389        while(animate!=null) {
 390            
 391            try {
 392                animate.sleep(300);
 393                
 394                
 395                Thread.yield();
 396                //printStats();
 397                
 398                repaint();
 399            }catch (Exception e) {
 400                System.out.println(e);
 401            }
 402            
 403        }
 404        big.dispose();
 405    }
 406
 407    
 408    public void printStats() {
 409        System.out.print(countAnts() + " ants. ");
 410        System.out.print(countCarriedFood() + " carrying food");
 411        System.out.println();
 412        // if(countCarriedFood() == 0 && countAnts() > 20) System.exit(1);
 413    }
 414
 415    public Nest getNest() {
 416        return nest;
 417    }
 418
 419    public void setNest(Nest nest) {
 420        this.nest = nest;
 421    }
 422
 423    public Vector<FoodCluster> getFoodClusters() {
 424        return foodClusters;
 425    }
 426
 427    public void setFoodClusters(Vector<FoodCluster> foodClusters) {
 428        this.foodClusters = foodClusters;
 429    }
 430
 431    public GridVector getGridSquares() {
 432        return gridVector;
 433    }
 434    
 435    public GridVector getGridSquaresWithFood() {
 436        return getGridSquares().getFoodGridSquares();
 437//        Vector<GridSquare> gridSquares = new Vector<GridSquare>();
 438//        for(GridSquare gs : getGridSquares()) {
 439//            if(gs.containsFood())
 440//                gridSquares.add(gs);
 441//        }
 442//        
 443//        return gridSquares;
 444    }
 445    
 446    public int countFood() {
 447        return getGridSquaresWithFood().size();
 448    }
 449
 450    public void setGridVector(GridVector gridVector) {
 451        this.gridVector = gridVector;
 452    }
 453}
 454

· Grid.java ends ·

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