-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGui.java
More file actions
221 lines (148 loc) · 4.36 KB
/
Gui.java
File metadata and controls
221 lines (148 loc) · 4.36 KB
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
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Timer;
public class Gui extends JFrame implements ActionListener {
Puzzleboard p;
JFrame frame = new JFrame();
JButton[][] grid;
boolean b;
//takes in size of _puzzleboard_. will have to calibrate indices later...
public Gui(Grid g, Puzzleboard p2, int depth, int width){
this.p = new Puzzleboard(g);
frame.setSize(500,300);
frame.setResizable(false);
//frame.getContentPane().setBackground(Color.BLUE);
//first = #rows (depth), second = #columns (width)
frame.setLayout(new GridLayout(depth + 1, width));
grid = new JButton[width][depth];
//
for(int i = 0; i<width; i++){
for(int j = 0; j<depth; j++){
grid[i][j] = new JButton(Character.toString(p2.getCage()[i][j].getValue()));
grid[i][j].setEnabled(false);
//grid[i][j].setForeground(Color.RED);
//grid[i][j].setFont(new Font("sansserif",Font.BOLD,12);
grid[i][j].setBorderPainted(false);
grid[i][j].setMargin(new Insets(0, 0, 0, 0));
grid[i][j].setVerticalAlignment(SwingConstants.TOP);
grid[i][j].setAlignmentX(frame.TOP_ALIGNMENT);
grid[i][j].setFocusPainted(false);
grid[i][j].setContentAreaFilled(false);
frame.add(grid[i][j]);
}
}
JButton Run = new JButton("Find Solution");
frame.add(Run);
Run.addActionListener(this);
JButton insert = new JButton(" ");
frame.add(insert);
insert.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void iterate(){
int count = 0;
while(!this.getP().allPlaced()){
//sleep:
/* try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}*/
//sleep
while(this.getP().tryNextHole()){
count++;
this.updateGui(this.getP(), this.getP().getWidth(), this.getP().getDepth());
/*//sleeping...
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
//wake up!
*/
//this.printCage();
}
//System.out.println(patterns);
//System.out.println("removing");
if(!this.getP().allPlaced()){
this.getP().removeBlock();
}
//this.printCage();
}
this.getP().printCage();
System.out.println("iterations: " + count);
}
public void updateGui(Puzzleboard p, int depth, int width){
for(int i = 0; i<width; i++){
for(int j = 0; j<depth; j++){
this.grid[i][j].setText(Character.toString(p.getCage()[i][j].getValue()));
}
}
this.frame.repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
String name = e.getActionCommand();
/*if(name.equals("Insert")){
System.out.println("hi");
}*/
if(name.equals("Find Solution")){
//System.out.println(p.getBlockNumber());
// SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
// @Override
// protected Void doInBackground() throws Exception {
// //this.iterate();
// for (int i = 0; i <= 10; i++) {
// Thread.sleep(1000);
// System.out.println("Running " + i);
// }
//
// return null;
// }
// };
//
// worker.execute();
//
this.b = true;
}
}
public Puzzleboard getP() {
return p;
}
public void setP(Puzzleboard p) {
this.p = p;
}
public static void main(String[] args) throws FileNotFoundException{
Grid g = new Grid("puzzle2.txt");
Puzzleboard poozle = new Puzzleboard(g);
Gui vis = new Gui(g, poozle, poozle.getWidth(), poozle.getDepth());
while(!vis.isB()){
System.out.println("Loading Puzzle");
}
final long startTime = System.currentTimeMillis();
//+++Solver+++
vis.iterate();
//++++++++++++
final long endTime = System.currentTimeMillis();
final long time = endTime - startTime;
System.out.println("time: " + time + " milliseconds." );
}
public boolean isB() {
return b;
}
public void setB(boolean b) {
this.b = b;
}
}