Termite colony simulator using java
Posted
by
ashii
on Stack Overflow
See other posts from Stack Overflow
or by ashii
Published on 2011-01-12T00:40:16Z
Indexed on
2011/01/12
0:53 UTC
Read the original article
Hit count: 235
hi everyone,
i hve to design a simulator that will maintain an environment, which consists of a collection of patches arranged in a rectangular grid of arbitrary size. Each patch contains zero or more wood chips. A patch may be occupied by one or more termites or predators, which are mobile entities that live within the world and behave according to simple rules.
A TERMITE can pick up a wood chip from the patch that it is currently on, or drop a wood chip that it is carrying. Termites travel around the grid by moving randomly from their current patch to a neighbouring patch, in one of four possible directions. New termites may hatch from eggs, and this is simulated by the appearance of a new termite at a random patch within the environment.
A PREDATOR moves in a similar way to termites, and if a predator moves onto a patch that is occupied by a termite, then the predator eats the termite.
At initialization, the termites, predators, and wood chips are distributed randomly in the environment. Simulation then proceeds in a loop, and the new state of the environment is obtained at each iteration.
i have designed the arena using jpanel but im not able to randomnly place wood,termite and predator in that arena. can any one help me out?? my code for the arena is as following:
01 import java.awt.*;
02 import javax.swing.*;
03
04 public class Arena extends JPanel
05 {
06 private static final int Rows = 8;
07 private static final int Cols = 8;
08 public void paint(Graphics g)
09 {
10 Dimension d = this.getSize();
11 // don't draw both sets of squares, when you can draw one
12
// fill in the entire thing with one color
13
g.setColor(Color.WHITE);
14
// make the background
15
g.fillRect(0,0,d.width,d.height);
16
// draw only black
17
g.setColor(Color.BLACK);
18
// pick a square size based on the smallest dimension
19
int sqsize = ((d.width<d.height) ? d.width/Cols : d.height/Rows);
20
// loop for rows
21
for (int row=0; row<Rows; row++)
22
{
23 int y = row*sqsize; // y stays same for entire row, set here
24 int x = (row%2)*sqsize; // x starts at 0 or one square in
25 for (int i=0; i<Cols/2; i++)
26 {
27 // you will only be drawing half the squares per row
28 // draw square
29 g.fillRect(x,y,sqsize,sqsize);
30 // move two square sizes over
31 x += sqsize*2;
32 }
33 }
34
35 }
36
37
38
39 public void update(Graphics g) { paint(g); }
40
41
42
43 public static void main (String[] args)
44 {
45
46 JFrame frame = new JFrame("Arena");
47 frame.setSize(600,400);
48 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
49 frame.setContentPane(new Arena());
50 frame.setVisible(true);
51 }
52
53 }
© Stack Overflow or respective owner