-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
228 lines (171 loc) · 7.05 KB
/
Main.java
File metadata and controls
228 lines (171 loc) · 7.05 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
222
223
224
225
226
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Hashtable;
//By: Brandon Beckwith
/*
Java GUI code was created using Java Swing Documentation:
https://docs.oracle.com/javase/7/docs/api/javax/swing/package-summary.html
The A Star algorithm was created using the textbook and lecture slides
*/
public class Main {
private static final int SIZE = 15;
private static final JButton RESET_B = new JButton();
private static final JSlider RAND_SLIDER = new JSlider();
private static final Board BOARD = new Board(SIZE);
private static final BoardPoints POINTS = new BoardPoints();
private static SpaceButton[][] buttons;
private static final JFrame FRAME = new JFrame("A*");
/**
* Main class used to run the program
* @param args
*/
public static void main (String[] args){
Main.mainPanel();
}
private static void mainPanel(){
buttons = new SpaceButton[SIZE][SIZE];
BOARD.generateBoard(0.1f);
FRAME.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
initControls();
JPanel mainPanel = new JPanel();
mainPanel.setBackground(Colors.Space);
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel controls = new JPanel();
controls.setBackground(Colors.Space);
controls.setForeground(Colors.SpaceBorder);
controls.add(RESET_B);
controls.add(RAND_SLIDER);
controls.setMaximumSize(new Dimension(Integer.MAX_VALUE, 75));
JPanel field = new JPanel();
GridLayout layout = new GridLayout(SIZE, SIZE);
layout.setHgap(0);
layout.setVgap(0);
field.setLayout(layout);
field.setBackground(Color.DARK_GRAY);
RESET_B.setBorder(BorderFactory.createLineBorder(Colors.SpaceBorder, 5, true));
for (int i=0; i < SIZE; i ++){
for (int j=0; j < SIZE; j ++) {
buttons[i][j] = new SpaceButton(BOARD.getSpace(i,j));
buttons[i][j].update();
buttons[i][j].addActionListener(buttons[i][j]);
field.add(buttons[i][j]);
}
}
mainPanel.add(controls);
mainPanel.add(field);
FRAME.setContentPane(mainPanel);
FRAME.pack();
FRAME.setSize(600,700);
FRAME.setVisible(true);
}
private static void initControls(){
RESET_B.setText("Reset");
RESET_B.setBackground(Colors.SpaceBorder);
RESET_B.setForeground(Colors.Text);
RESET_B.addActionListener(e -> {
BOARD.resetState();
POINTS.reset();
updateButtons();
});
RAND_SLIDER.setMajorTickSpacing(10);
RAND_SLIDER.setValue(10);
RAND_SLIDER.setPaintLabels(true);
RAND_SLIDER.setPreferredSize(new Dimension(350, 75));
RAND_SLIDER.setBackground(Colors.Space);
RAND_SLIDER.setForeground(Colors.SpaceBorder);
Hashtable<Integer, JLabel> labels = new Hashtable<>();
for (int i=0; i <= 10; i++){
int val = i*10;
JLabel tLabel = new JLabel(Integer.toString(val) + "%");
tLabel.setForeground(Colors.Text);
labels.put(val, tLabel);
}
RAND_SLIDER.setLabelTable(labels);
RAND_SLIDER.setPaintTicks(true);
RAND_SLIDER.addChangeListener(e -> {
JSlider slider = (JSlider) e.getSource();
BOARD.resetState();
BOARD.generateBoard(((float) slider.getValue()) / 100f);
POINTS.reset();
updateButtons();
});
}
private static void updateButtons(){
for (int i=0; i < SIZE; i ++){
for (int j=0; j < SIZE; j ++) {
buttons[i][j].update();
}
}
FRAME.repaint();
}
private static class BoardPoints {
Point start, end;
void reset() {
this.start = null;
this.end = null;
}
}
private static class SpaceButton extends JButton implements ActionListener{
final Space space;
private SpaceButton(Space space){
this.space = space;
this.setPreferredSize(new Dimension(50, 50));
this.setOpaque(true);
this.setForeground(Colors.Text);
this.setBorder(BorderFactory.createLineBorder(Colors.SpaceBorder, 1, false));
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
SpaceButton thisSpace = (SpaceButton) e.getComponent();
if (e.getButton() == MouseEvent.BUTTON3) {
thisSpace.space.swapType();
}
else if (e.getButton() == MouseEvent.BUTTON1) {
if (POINTS.end != null && POINTS.start != null) {
POINTS.reset();
BOARD.resetState();
} else if (POINTS.start == null) {
if (thisSpace.space.getType() == SpaceType.EMPTY) {
POINTS.start = thisSpace.space.getPoint();
thisSpace.space.setType(SpaceType.START);
}
} else {
if (thisSpace.space.getType() == SpaceType.EMPTY) {
POINTS.end = thisSpace.space.getPoint();
ArrayList<Space> path = AStar.findPath(BOARD, POINTS.start, POINTS.end);
if (path.isEmpty()) {
JOptionPane.showMessageDialog(FRAME, "Path not found!");
System.out.println("Failed to find path");
} else {
StringBuilder out = new StringBuilder();
for (Space space : path) {
if (space.getType() == SpaceType.EMPTY)
space.setType(SpaceType.PATH);
out.append(space.getPointString()).append(" ");
}
System.out.println("Path is: " + out);
System.out.println(BOARD);
}
thisSpace.space.setType(SpaceType.END);
}
}
}
updateButtons();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
}
private void update(){
this.setBackground(this.space.getType().toColor());
this.setText(this.space.getType().toString());
}
}
}