Skip to content

Commit b5ad1ab

Browse files
committed
Adding Files to repository
0 parents  commit b5ad1ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2078
-0
lines changed

.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path=""/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="output" path=""/>
6+
</classpath>

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>TEMP</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

practice/AutomataDisplay$1.class

1.32 KB
Binary file not shown.

practice/AutomataDisplay$2.class

864 Bytes
Binary file not shown.

practice/AutomataDisplay$3.class

1.06 KB
Binary file not shown.

practice/AutomataDisplay$4.class

837 Bytes
Binary file not shown.

practice/AutomataDisplay$5.class

1.42 KB
Binary file not shown.

practice/AutomataDisplay.class

5.69 KB
Binary file not shown.

practice/AutomataDisplay.java

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package practice;
2+
import java.awt.BorderLayout;
3+
import java.awt.Color;
4+
import java.awt.Dimension;
5+
import java.awt.GridBagConstraints;
6+
import java.awt.GridBagLayout;
7+
import java.awt.event.ActionEvent;
8+
import java.awt.event.ActionListener;
9+
import javax.swing.BorderFactory;
10+
import javax.swing.JButton;
11+
import javax.swing.JFrame;
12+
import javax.swing.JLabel;
13+
import javax.swing.JLayeredPane;
14+
import javax.swing.JPanel;
15+
import javax.swing.JScrollPane;
16+
import javax.swing.JTextField;
17+
import javax.swing.WindowConstants;
18+
19+
20+
public class AutomataDisplay extends JFrame{
21+
/**
22+
*
23+
*/
24+
private static final long serialVersionUID = -4650371836502914232L;
25+
//final Constant Values
26+
final int GAMEWIDTH = 700;
27+
final int GAMEHEIGHT = 750;
28+
final int BOARDWIDTH = 700;
29+
final int BOARDHEIGHT = 600;
30+
private JPanel BoardDisplay;
31+
private JPanel Board;
32+
private JScrollPane BoardScroll;
33+
//The Engine
34+
private AutomataEngine engine;
35+
//A mimic board
36+
private Square[][] fBoard;
37+
//The rule container
38+
private Rules rules;
39+
//The number of tiles to be used
40+
private int numTiles;
41+
//Field that displays curGen
42+
private JTextField curGen;
43+
//Square length of display
44+
private int squareLength;
45+
46+
/**
47+
* Constructor
48+
* @param numTiles
49+
* @param name
50+
* @param rule
51+
*/
52+
public AutomataDisplay(int numTiles, String name, Rules rule)
53+
{
54+
super(name);
55+
setSize(new Dimension(GAMEWIDTH, GAMEHEIGHT));
56+
setFocusable(true);
57+
setVisible(true);
58+
setResizable(false);
59+
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
60+
//Sets class variables
61+
this.numTiles = numTiles;
62+
this.rules = rule;
63+
squareLength = this.BOARDHEIGHT;
64+
BuildBoard();
65+
}
66+
/**
67+
* Will build a new Automata board
68+
*/
69+
private void BuildBoard()
70+
{
71+
//Builds the Cellular Automata Board
72+
Board = new JPanel();
73+
BoardScroll = new JScrollPane(Board);
74+
this.add(BoardScroll, BorderLayout.NORTH, JLayeredPane.DEFAULT_LAYER);
75+
Board.setLayout(new FullyJustifiedGridLayout(numTiles,numTiles));
76+
Board.setPreferredSize(new Dimension(squareLength, squareLength));
77+
BoardScroll.setPreferredSize(new Dimension(BOARDWIDTH, BOARDHEIGHT));
78+
fBoard = new Square[numTiles][numTiles];
79+
for (int i = 0; i < numTiles; i++) {
80+
for(int j = 0; j<numTiles; j++){
81+
Square square = new Square(rules.getColorSet());
82+
fBoard[i][j] = square;
83+
square.addMouseListener(square);
84+
Board.add(square);
85+
}
86+
}
87+
//Configures the Board Display which holds the buttons
88+
configureButtons();
89+
//Create the engine
90+
this.engine = new AutomataEngine(rules, fBoard, curGen);
91+
}
92+
/**
93+
* Helper method that will configure the buttons
94+
*/
95+
private void configureButtons(){
96+
BoardDisplay = new JPanel(new GridBagLayout());
97+
BoardDisplay.setBorder(BorderFactory.createLineBorder(Color.BLACK));
98+
BoardDisplay.setBackground(Color.white);
99+
this.add(BoardDisplay, null, JLayeredPane.DEFAULT_LAYER);
100+
final JButton startStop = new JButton("Start Simulation");
101+
startStop.setActionCommand("Start");
102+
ActionListener startSim = new ActionListener(){
103+
public void actionPerformed(ActionEvent e) {
104+
if(e.getActionCommand().equals("Start")){
105+
engine.start();
106+
startStop.setText("Stop Simulation");
107+
startStop.setActionCommand("Stop");
108+
} else {
109+
engine.stop();
110+
startStop.setText("Start Simulation");
111+
startStop.setActionCommand("Start");
112+
}
113+
}
114+
};
115+
startStop.addActionListener(startSim);
116+
JButton finish = new JButton("Finish");
117+
finish.addActionListener(new ActionListener(){
118+
public void actionPerformed(ActionEvent e){
119+
engine.finish();
120+
finish();
121+
}
122+
});
123+
curGen = new JTextField("0",5);
124+
curGen.setEditable(false);
125+
//Labels
126+
JLabel generation = new JLabel("Current Generation:");
127+
JLabel speedText = new JLabel("Current Speed:");
128+
final JTextField speedField = new JTextField(AutomataEngine.DEFAULT_DELAY+"",6);
129+
JButton changeSpeed = new JButton("Change Speed");
130+
changeSpeed.addActionListener(new ActionListener(){
131+
public void actionPerformed(ActionEvent e){
132+
engine.changeSpeed(Integer.parseInt(speedField.getText()));
133+
}
134+
});
135+
JButton resetScreen = new JButton("Reset Screen");
136+
resetScreen.addActionListener(new ActionListener(){
137+
public void actionPerformed(ActionEvent e){
138+
engine.resetScreen();
139+
}
140+
});
141+
JLabel displayText = new JLabel("Set size of Display");
142+
final JTextField displayField = new JTextField(""+this.squareLength,6);
143+
JButton changeDisplay = new JButton("Change Display");
144+
changeDisplay.addActionListener(new ActionListener(){
145+
public void actionPerformed(ActionEvent e){
146+
squareLength = Integer.parseInt(displayField.getText());
147+
Board.setPreferredSize(new Dimension(squareLength, squareLength));
148+
BoardScroll.revalidate();
149+
BoardScroll.repaint();
150+
}
151+
});
152+
//Add stuff to BoardDisplay at bottom of screen
153+
GridBagConstraints c = new GridBagConstraints();
154+
c.gridy = 0;
155+
c.gridx = GridBagConstraints.RELATIVE;
156+
BoardDisplay.add(startStop,c);
157+
BoardDisplay.add(speedText,c);
158+
BoardDisplay.add(speedField,c);
159+
BoardDisplay.add(changeSpeed,c);
160+
BoardDisplay.add(generation,c);
161+
BoardDisplay.add(curGen,c);
162+
BoardDisplay.add(finish,c);
163+
c.gridy = 1;
164+
BoardDisplay.add(displayText,c);
165+
BoardDisplay.add(displayField,c);
166+
BoardDisplay.add(changeDisplay,c);
167+
BoardDisplay.add(resetScreen,c);
168+
}
169+
170+
private void finish(){
171+
this.dispose();
172+
}
173+
}

practice/AutomataEngine$1.class

767 Bytes
Binary file not shown.

practice/AutomataEngine.class

3.76 KB
Binary file not shown.

practice/AutomataEngine.java

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package practice;
2+
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
import java.util.HashMap;
6+
7+
import javax.swing.JTextField;
8+
import javax.swing.Timer;
9+
10+
public class AutomataEngine {
11+
//Default time delay
12+
public final static int DEFAULT_DELAY = 500;
13+
//Rules object containing all necessary rule information
14+
private Rules rules;
15+
//Fake board to hold squares
16+
private Square[][] fBoard;
17+
//Timer object
18+
private Timer time;
19+
private JTextField curGen;
20+
private int[][] template;
21+
private HashMap<Integer,int[]> answerSet;
22+
23+
/**
24+
* Constructor
25+
* @param rule - the rule set to be used
26+
* @param fBoard - the mimic board to be used for data
27+
* @param curGen - the curGen container
28+
*/
29+
public AutomataEngine(Rules rule, Square[][] fBoard, JTextField curGen){
30+
rules = rule;
31+
this.fBoard = fBoard;
32+
this.curGen = curGen;
33+
this.time = new Timer(DEFAULT_DELAY, new ActionListener() {
34+
public void actionPerformed(ActionEvent evt) {
35+
tick();
36+
}
37+
});
38+
template = rules.getTemplate();
39+
answerSet = rules.getAnswerSet();
40+
}
41+
/**
42+
* Start the Simulation
43+
*/
44+
public void start(){
45+
if(time.isRunning()) return;
46+
for(Square[] sl:fBoard){
47+
for(Square s:sl){
48+
s.removeMouseListener(s);
49+
}
50+
}
51+
time.start();
52+
}
53+
54+
/**
55+
* Stops the simulation
56+
*/
57+
public void stop(){
58+
if(!time.isRunning()) return;
59+
time.stop();
60+
for(Square[] sl:fBoard){
61+
for(Square s:sl){
62+
s.addMouseListener(s);
63+
}
64+
}
65+
}
66+
67+
/**
68+
* One tick of the simulation
69+
*/
70+
private void tick(){
71+
int[][] tempBoard = new int[fBoard.length][fBoard.length];
72+
int colorLength = rules.getColorSet().length;
73+
//Iterate through every square where the square will serve as top
74+
//left position for template
75+
for(int i = 0; i<fBoard.length; i++){
76+
for(int j = 0; j<fBoard[i].length; j++){
77+
//chunkState will determine which rule will be used
78+
//curMultiplier helps performance
79+
int chunkState = 0;
80+
int curMultiplier = 1;
81+
//This is iterating through template at position i,j
82+
for(int k = i; k<i+template.length; k++){
83+
for(int p = j; p<j+template[0].length; p++){
84+
//If the template actually contains something lets see if real thing does
85+
if(template[k-i][p-j] != 0){
86+
chunkState += fBoard[k%fBoard.length][p%fBoard.length].getType()*curMultiplier;
87+
curMultiplier = curMultiplier*colorLength;
88+
}
89+
}
90+
}
91+
int[] newDig = rules.calculateRule(chunkState);
92+
for(int k = 0; k<newDig.length; k++){
93+
//need to generalize to cover multiple centers
94+
tempBoard[(i+rules.getYAnswerSet().get(k))%tempBoard.length]
95+
[(j+rules.getXAnswerSet().get(k))%tempBoard.length] = newDig[k];
96+
}
97+
}
98+
}
99+
//Transfer changes to the actual board
100+
for(int i = 0; i<tempBoard.length; i++){
101+
for(int j = 0; j<tempBoard.length; j++){
102+
fBoard[i][j].setType(tempBoard[i][j]);
103+
}
104+
}
105+
Integer cGen = Integer.parseInt(curGen.getText());
106+
cGen++;
107+
curGen.setText(cGen.toString());
108+
}
109+
110+
/**
111+
* Finishes the current simulation while giving option to save initial settings
112+
*/
113+
public void finish(){
114+
// TODO need to add saving function
115+
}
116+
/**
117+
* @return the isRunning
118+
*/
119+
public boolean isRunning() {
120+
return time.isRunning();
121+
}
122+
123+
public void changeSpeed(int speed){
124+
time.setDelay(speed);
125+
}
126+
127+
public void resetScreen(){
128+
this.stop();
129+
this.curGen.setText("0");
130+
for(int i = 0; i<fBoard.length; i++){
131+
for(int j = 0; j<fBoard[i].length; j++){
132+
fBoard[i][j].setType(0);
133+
}
134+
}
135+
136+
}
137+
}

practice/AutomataMain$1.class

683 Bytes
Binary file not shown.

practice/AutomataMain$10.class

1.67 KB
Binary file not shown.

practice/AutomataMain$11.class

1001 Bytes
Binary file not shown.

practice/AutomataMain$12.class

1.04 KB
Binary file not shown.

practice/AutomataMain$13.class

716 Bytes
Binary file not shown.

practice/AutomataMain$14.class

1.13 KB
Binary file not shown.

practice/AutomataMain$15.class

1.52 KB
Binary file not shown.

practice/AutomataMain$16.class

1.39 KB
Binary file not shown.

practice/AutomataMain$17.class

1.55 KB
Binary file not shown.

practice/AutomataMain$18.class

551 Bytes
Binary file not shown.

practice/AutomataMain$2.class

983 Bytes
Binary file not shown.

practice/AutomataMain$3.class

778 Bytes
Binary file not shown.

practice/AutomataMain$4.class

943 Bytes
Binary file not shown.

practice/AutomataMain$5.class

794 Bytes
Binary file not shown.

practice/AutomataMain$6.class

820 Bytes
Binary file not shown.

practice/AutomataMain$7.class

823 Bytes
Binary file not shown.

practice/AutomataMain$8.class

796 Bytes
Binary file not shown.

practice/AutomataMain$9.class

1.29 KB
Binary file not shown.

practice/AutomataMain.class

12 KB
Binary file not shown.

0 commit comments

Comments
 (0)