-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathControlPanel.java
More file actions
161 lines (136 loc) · 5.78 KB
/
ControlPanel.java
File metadata and controls
161 lines (136 loc) · 5.78 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
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Font;
public class ControlPanel extends JPanel {
private DisplayPanel display;
private Font font = new Font("Tahoma", Font.BOLD, 24);
private String[] boardList, mutationList, selectionList, populationList, generationsList, pauseList;
private JComboBox boardDropdown, mutationDropdown, selectionDropdown, populationDropdown, generationsDropdown, pauseDropdown;
private JButton update, restart;
public ControlPanel(DisplayPanel display) {
this.display = display;
setPreferredSize(new Dimension(300, 720));
setBackground(Color.DARK_GRAY);
setLayout(new FlowLayout(FlowLayout.LEFT));
//top, left, bottom, right
setBorder(new EmptyBorder(new Insets(150, 50, 150, 25)));
boardList = new String[]{"8", "9", "10", "12","15", "16", "20", "24", "30",
"36", "48", "60", "120", "144", "180", "240", "360", "720"};
mutationList = new String[]{"10", "20", "40", "60", "80", "100"};
selectionList = new String[]{"2", "3", "4", "5", "10", "15"};
populationList = new String[]{"25", "50", "75", "100", "150", "200", "300"};
generationsList = new String[]{"50", "100", "200", "400", "1000", "4000", "Unlimited"};
pauseList = new String[]{"0", "10", "50", "100", "500"};
boardDropdown = new JComboBox(boardList);
mutationDropdown = new JComboBox(mutationList);
selectionDropdown = new JComboBox(selectionList);
populationDropdown = new JComboBox(populationList);
generationsDropdown = new JComboBox(generationsList);
pauseDropdown = new JComboBox(pauseList);
boardDropdown.setSelectedItem("8");
mutationDropdown.setSelectedItem("80");
selectionDropdown.setSelectedItem("5");
populationDropdown.setSelectedItem("150");
generationsDropdown.setSelectedItem("400");
pauseDropdown.setSelectedItem("100");
JLabel sizeLabel = new JLabel("Board Size");
JLabel mutationLabel = new JLabel("Mutation Chance (%)");
JLabel selectionLabel = new JLabel("Selection Factor");
JLabel populationLabel = new JLabel("Population Size");
JLabel generationsLabel = new JLabel("# of Generations");
JLabel pauseLabel = new JLabel("Delay (Milliseconds)");
update = new JButton("Update");
update.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
display.updateVariables(getMutationChance(), getSelectionFactor(),
getNumGenerations(), getPauseLength());
}
});
restart = new JButton("Restart");
restart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
// Update the variables and reset
display.updateVariables(getMutationChance(), getSelectionFactor(),
getNumGenerations(), getPauseLength());
display.restart(getBoardSize(), getPopulationSize());
repaint();
}
});
sizeLabel.setForeground(Color.WHITE);
mutationLabel.setForeground(Color.WHITE);
selectionLabel.setForeground(Color.WHITE);
populationLabel.setForeground(Color.WHITE);
generationsLabel.setForeground(Color.WHITE);
pauseLabel.setForeground(Color.WHITE);
int gap = 50;
add(sizeLabel);
add(boardDropdown);
add(Box.createRigidArea(new Dimension(0, gap)));
add(mutationLabel);
add(mutationDropdown);
add(Box.createRigidArea(new Dimension(0, gap)));
add(selectionLabel);
add(selectionDropdown);
add(Box.createRigidArea(new Dimension(0, gap)));
add(populationLabel);
add(populationDropdown);
add(Box.createRigidArea(new Dimension(0, gap)));
add(generationsLabel);
add(generationsDropdown);
add(Box.createRigidArea(new Dimension(0, gap)));
add(pauseLabel);
add(pauseDropdown);
add(Box.createRigidArea(new Dimension(0, gap)));
add(update);
add(Box.createRigidArea(new Dimension(10, 0)));
add(restart);
}
public void update() {
repaint();
}
public void paint(Graphics g) {
super.paint(g);
QueenSet bestBoard = display.getBestBoard();
int fitness = bestBoard.getFitness();
int currentGeneration = display.getCurrentGeneration();
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString("N Queens Genetic", 50, 70);
g.drawString("Algorithm", 100, 105);
g.drawString("Generation #" + currentGeneration, 50, 600);
g.drawString("Fitness: " + fitness, 50, 650);
}
public int getBoardSize() {
return Integer.parseInt(boardList[boardDropdown.getSelectedIndex()]);
}
public int getMutationChance() {
return Integer.parseInt(mutationList[mutationDropdown.getSelectedIndex()]);
}
public int getSelectionFactor() {
return Integer.parseInt(selectionList[selectionDropdown.getSelectedIndex()]);
}
public int getPopulationSize() {
return Integer.parseInt(populationList[populationDropdown.getSelectedIndex()]);
}
public int getNumGenerations() {
String choice = generationsList[generationsDropdown.getSelectedIndex()];
if (choice.equals("Unlimited")) {
return (int) Double.POSITIVE_INFINITY;
}
return Integer.parseInt(choice);
}
public int getPauseLength() {
return Integer.parseInt(pauseList[pauseDropdown.getSelectedIndex()]);
}
}