PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/com/thomasriga/ai/cellular/automaton/Can_Button.java

https://github.com/thomasriga/CellularAutomaton
Java | 236 lines | 195 code | 30 blank | 11 comment | 43 complexity | e4073c7015b53f4887b1320a797d31f0 MD5 | raw file
  1. /*
  2. CellularAutomaton Copyright (C) 2010 Thomas Riga
  3. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
  4. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  5. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6. Contact the author at thomasriga@gmail.com (http://www.thomasriga.com)
  7. */
  8. package com.thomasriga.ai.cellular.automaton;
  9. import java.awt.*;
  10. import java.util.Vector;
  11. class Can_Button extends Canvas
  12. implements Runnable
  13. {
  14. public class Scan
  15. {
  16. int NO;
  17. int NE;
  18. int EA;
  19. int SE;
  20. int SO;
  21. int SW;
  22. int WE;
  23. int NW;
  24. Scan(int firstField[][], int columns, int rows, int x, int y)
  25. {
  26. int tx = x - 1;
  27. if(tx < 0)
  28. tx = columns - 1;
  29. int ty = y - 1;
  30. if(ty < 0)
  31. ty = rows - 1;
  32. NW = firstField[tx][ty];
  33. ty = y - 1;
  34. if(ty < 0)
  35. ty = rows - 1;
  36. NO = firstField[x][ty];
  37. tx = x + 1;
  38. if(tx == columns)
  39. tx = 0;
  40. ty = y - 1;
  41. if(ty < 0)
  42. ty = rows - 1;
  43. NE = firstField[tx][ty];
  44. tx = x + 1;
  45. if(tx == rows)
  46. tx = 0;
  47. EA = firstField[tx][y];
  48. tx = x + 1;
  49. if(tx == columns)
  50. tx = 0;
  51. ty = y + 1;
  52. if(ty == rows)
  53. ty = 0;
  54. SE = firstField[tx][ty];
  55. ty = y + 1;
  56. if(ty == rows)
  57. ty = 0;
  58. SO = firstField[x][ty];
  59. tx = x - 1;
  60. if(tx < 0)
  61. tx = columns - 1;
  62. ty = y + 1;
  63. if(ty == rows)
  64. ty = 0;
  65. SW = firstField[tx][ty];
  66. tx = x - 1;
  67. if(tx < 0)
  68. tx = columns - 1;
  69. WE = firstField[tx][y];
  70. }
  71. }
  72. public Can_Button()
  73. {
  74. frameDelay = 300;
  75. lastDisplay = 0L;
  76. parA = new Vector();
  77. parB = new Vector();
  78. parA.addElement("3");
  79. parB.addElement("1");
  80. parB.addElement("4");
  81. parB.addElement("5");
  82. parB.addElement("6");
  83. parB.addElement("7");
  84. parB.addElement("8");
  85. initializeField(firstField, 40, 40);
  86. copyFields(secondField, firstField, 40, 40);
  87. repaint();
  88. animation = new Thread(this);
  89. animation.start();
  90. }
  91. public void paint(Graphics g)
  92. {
  93. g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
  94. for(int x = 0; x < 40; x++)
  95. {
  96. for(int y = 0; y < 40; y++)
  97. if(secondField[x][y] == 1)
  98. g.fillOval(x * 2 * 2, y * 2 * 2, 4, 4);
  99. }
  100. }
  101. public void run()
  102. {
  103. initializeField(firstField, 40, 40);
  104. copyFields(secondField, firstField, 40, 40);
  105. repaint();
  106. do
  107. {
  108. long time;
  109. do
  110. {
  111. move(firstField, secondField);
  112. copyFields(firstField, secondField, 40, 40);
  113. time = System.currentTimeMillis();
  114. } while(time - lastDisplay <= (long)frameDelay);
  115. repaint();
  116. try
  117. {
  118. Thread.sleep(frameDelay);
  119. }
  120. catch(InterruptedException ex) { }
  121. lastDisplay = time;
  122. } while(true);
  123. }
  124. public void move(int firstField[][], int secondField[][])
  125. {
  126. int temp = 0;
  127. for(int x = 0; x < 40; x++)
  128. {
  129. for(int y = 0; y < 40; y++)
  130. {
  131. Scan neighbours = new Scan(firstField, 40, 40, x, y);
  132. temp = neighbours.NW + neighbours.NO + neighbours.NE + neighbours.EA + neighbours.SE + neighbours.SO + neighbours.SW + neighbours.WE;
  133. for(int z = 0; z < parA.size(); z++)
  134. if(temp == (new Integer((String)parA.elementAt(z))).intValue() && firstField[x][y] == 0)
  135. secondField[x][y] = 1;
  136. for(int z = 0; z < parB.size(); z++)
  137. if(temp == (new Integer((String)parB.elementAt(z))).intValue() && firstField[x][y] == 1)
  138. secondField[x][y] = 0;
  139. }
  140. }
  141. }
  142. public void copyFields(int firstField[][], int secondField[][], int columns, int rows)
  143. {
  144. for(int x = 0; x < columns; x++)
  145. {
  146. for(int y = 0; y < rows; y++)
  147. firstField[x][y] = secondField[x][y];
  148. }
  149. }
  150. public void initializeField(int firstField[][], int columns, int rows)
  151. {
  152. for(int x = 0; x < columns; x++)
  153. {
  154. for(int y = 0; y < rows; y++)
  155. if(Math.random() < 0.90000000000000002D)
  156. firstField[x][y] = 0;
  157. else
  158. firstField[x][y] = 1;
  159. }
  160. }
  161. public void setParA(String par[])
  162. {
  163. int x = 0;
  164. parA.removeAllElements();
  165. try
  166. {
  167. for(; par[x] != null; x++)
  168. parA.addElement(par[x]);
  169. }
  170. catch(ArrayIndexOutOfBoundsException ev) { }
  171. }
  172. public void setParB(String par[])
  173. {
  174. int x = 0;
  175. parB.removeAllElements();
  176. try
  177. {
  178. for(; par[x] != null; x++)
  179. parB.addElement(par[x]);
  180. }
  181. catch(ArrayIndexOutOfBoundsException ev) { }
  182. }
  183. public void setDelay(int par)
  184. {
  185. frameDelay = par;
  186. }
  187. public void reset()
  188. {
  189. initializeField(firstField, 40, 40);
  190. copyFields(secondField, firstField, 40, 40);
  191. }
  192. private static final int RADIUS = 2;
  193. private static final int rows = 40;
  194. private static final int columns = 40;
  195. private static int firstField[][] = new int[40][40];
  196. private static int secondField[][] = new int[40][40];
  197. Thread animation;
  198. int frameDelay;
  199. long lastDisplay;
  200. Vector parA;
  201. Vector parB;
  202. }