1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
| import java.util.Random;
public class Automata {
protected int grid[][], clone[][];
public Automata(int lineNb, int columnNb) {
grid= new int[lineNb][columnNb];
clone = new int[lineNb][columnNb];
}
public void init() {
Random r = new Random();
for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[0].length; j++) {
grid[i][j]= r.nextInt(2);
}
}
}
public int getWidth() {
return grid.length;
}
public int getHeight() {
return grid[0].length;
}
public boolean isLiving(int i, int j) {
return (grid[i][j] == 1);
}
private int countLivingNeighbors(int i, int j) {
int s=0;
if (i>0) s=s+grid[i-1][j];
if ((i>0) && (j<grid[0].length-1)) s=s+grid[i-1][j+1];
if (j<grid[0].length-1) s=s+grid[i][j+1];
if ((i<grid.length-1) && (j<grid[0].length-1)) s=s+grid[i+1][j+1];
if (i<grid.length-1) s=s+grid[i+1][j];
if ((i<grid.length-1) && (j>0)) s=s+grid[i+1][j-1];
if (j>0) s=s+grid[i][j-1];
if ((i>0) && (j>0)) s=s+grid[i-1][j-1];
return s;
}
public void evolve() {
int nbLivNgb;
for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[0].length; j++) {
nbLivNgb=countLivingNeighbors(i,j);
if (grid[i][j]==1) {
if ((nbLivNgb<2)||(nbLivNgb>3)) clone[i][j]=0;
else clone[i][j]=1;
}
else {
if (nbLivNgb==3) clone[i][j]=1;
else clone[i][j]=0;
}
}
}
for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[0].length; j++) {
grid[i][j]=clone[i][j];
}
}
}
}
import java.awt.*;
import javax.swing.*;
public class GraphicAutomata extends JPanel{
private static final long serialVersionUID = 7596711375908391907L;
Automata myAutomata;
Process garde;
Boolean go;
public GraphicAutomata(Automata a,Process p,Boolean b){
super();
myAutomata=a;
garde=p;
go=b;
}
public void paintComponent(Graphics g){
super.paintComponent( g);
this.setBackground(Color.WHITE);
for(int i=0; i<myAutomata.getWidth();i++){
for(int j=0;j<myAutomata.getHeight();j++){
if(myAutomata.isLiving(i, j)){
g.setColor(Color.WHITE);
g.fillRect(20+10*j, 20+10*i, 10, 10);
}
else {
g.setColor(Color.GRAY);
g.fillRect(20+10*j, 20+10*i, 10, 10);
}
}
}
}
public void init(){
myAutomata.init();
super.repaint();
}
public void next(){
myAutomata.evolve();
super.repaint();
}
public void jump(){
int iter=Integer.parseInt(JOptionPane.showInputDialog("Leap:"));
for(int i=0; i<iter;i++) myAutomata.evolve();
super.repaint();
}
public void run(){
class Process implements Runnable {
boolean running;
Boolean consulte;
Process() {
running = true;
consulte=true;
}
public void run() {
while (running) {
synchronized(go) {
if (go.booleanValue()||consulte.booleanValue()) {
myAutomata.evolve();
}
else running = false;
}
try { Thread.sleep(200);}
catch (InterruptedException f) {}
}
}
}
synchronized(go) {
go = new Boolean(true);
}
Thread me = new Thread(new Process());
me.start();
}
public void pause(){
synchronized(go){
go= new Boolean(false);
}
}
}
import javax.swing.*;
public class Bouton extends JButton{
private static final long serialVersionUID = -8957666302885499495L;
String nom;
String code;
GraphicAutomata sollicite;
public Bouton(String n,String c,GraphicAutomata g){
super();
nom=n;
code=c;
sollicite=g;
}
public void addGraphicAutomata(GraphicAutomata b){
if(b!=null){
this.sollicite=b;
}
}
}
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Application {
private static final Component init = null;
private static final Component next = null;
private static final Component jump = null;
private static final Component run = null;
private static final Component pause = null;
static GraphicAutomata goL;
static Bouton control;
public static void main(String[] arg) {
int lDim = Integer.parseInt(JOptionPane.showInputDialog("Number of rows: "));
int cDim = Integer.parseInt(JOptionPane.showInputDialog("Number of columns: "));
Automata myAutomata = new Automata(lDim,cDim);
myAutomata.init();
control = new Bouton("Next", null, goL);
control.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
control.sollicite.next();
}
}
);
control = new Bouton("Init", null, goL);
control.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
control.sollicite.init();
}
}
);
control= new Bouton("Jump", null, goL);
control.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int iter = Integer.parseInt(JOptionPane.showInputDialog("Leap: "));
for (int i=0; i<iter; i++)
control.sollicite.jump();
}
}
);
/*arret=new JButton("Close");
arret.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
goL.init();
space.repaint();
System.exit(0);
}
}
);*/
control = new Bouton("Run", null, goL);
control.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
control.sollicite.run();
}
}
);
control = new Bouton("Pause", null, goL);
control.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Boolean go = null;
synchronized(go) {
go = new Boolean(false);
}
}
}
);
JPanel buttonPanel = new JPanel();
//buttonPanel.add(init);
buttonPanel.add(next);
buttonPanel.add(jump);
buttonPanel.add(run);
buttonPanel.add(pause);
//buttonPanel.add(arret);
JFrame screen = null;
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setSize(800, 600);
screen.setLayout(new BorderLayout());
Component space = null;
screen.add(space,BorderLayout.CENTER);
screen.add(buttonPanel,BorderLayout.SOUTH);
screen.setVisible(true);
}
} |
Partager