-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildGUI.java
More file actions
227 lines (186 loc) · 6.71 KB
/
BuildGUI.java
File metadata and controls
227 lines (186 loc) · 6.71 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.Color;
import java.awt.Font;
public class BuildGUI {
//Declare global variable
private JFrame frame;
private JButton btnStartTimer;
private JLabel bubbleSortOutput, insertionSortOutput, selectionSortOutput, partitionSortOutput;
private JTextField txtElementQty, txtMinValue, txtMaxValue;
//Class Constructor
public BuildGUI() {
//Declare and Initialize the GUI's frame and panel
frame = new JFrame("Sorting Algorithm Duration");
JPanel panel = new JPanel();
//Declare and Initialize the labels for the panel
JLabel lblQty = new JLabel("Enter the amount of numbers to sort: ");
JLabel lblRange = new JLabel("Enter the range of values");
JLabel lblOpenBracket = new JLabel("[");
JLabel lblComma = new JLabel(",");
JLabel lblCloseBracket = new JLabel("]");
JLabel lblMessage = new JLabel("* Measured in milliseconds *");
JLabel lblBubbleSort = new JLabel("Bubble Sort Duration"),
lblInsertionSort = new JLabel("Insertion Sort Duration"),
lblSelectionSort = new JLabel("Selection Sort Duration"),
lblPartitionSort = new JLabel("Partition Sort Duration");
bubbleSortOutput = new JLabel();
insertionSortOutput = new JLabel();
selectionSortOutput = new JLabel();
partitionSortOutput = new JLabel();
//Initialize the font for square brackets
Font bracketFont = new Font(null, Font.PLAIN, 20);
//Set text label for element quantity input
lblQty.setBounds(17, 5, 250, 50);
panel.add(lblQty);
//Set text field for element quantity input
txtElementQty = new JTextField();
txtElementQty.setBounds(255, 17, 75, 30);
txtElementQty.setHorizontalAlignment(JTextField.CENTER);
panel.add(txtElementQty);
//Set text label for element range input
lblRange.setBounds(350, 17, 250, 30);
panel.add(lblRange);
//Set the open square bracket
lblOpenBracket.setFont(bracketFont);
lblOpenBracket.setBounds(512, 7, 30, 50);
panel.add(lblOpenBracket);
//Set the text field for max value
txtMinValue = new JTextField();
txtMinValue.setBounds(520, 18, 50, 30);
txtMinValue.setHorizontalAlignment(JTextField.CENTER);
panel.add(txtMinValue);
//Set the text label for the comma
lblComma.setBounds(575, 25, 25, 25);
panel.add(lblComma);
//Set the text field for min value
txtMaxValue = new JTextField();
txtMaxValue.setBounds(585, 18, 50, 30);
txtMaxValue.setHorizontalAlignment(JTextField.CENTER);
panel.add(txtMaxValue);
//Set the open square bracket
lblCloseBracket.setFont(bracketFont);
lblCloseBracket.setBounds(637, 7, 30, 50);
panel.add(lblCloseBracket);
//Create the start button to time the algorithms
btnStartTimer = new JButton("START");
btnStartTimer.setBounds(295, 55, 80, 40);
panel.add(btnStartTimer);
//Set the text label for the message -- measured in in milliseconds
lblMessage.setBounds(265, 100, 250, 25);
lblMessage.setFont(new Font(null, Font.ITALIC, 12));
panel.add(lblMessage);
//Set the text label for bubble sort
lblBubbleSort.setBounds(20, 115, 200, 50);
panel.add(lblBubbleSort);
//Set the text label for insertion sort
lblInsertionSort.setBounds(170, 115, 200, 50);
panel.add(lblInsertionSort);
//Set the text label for selection sort
lblSelectionSort.setBounds(335, 115, 200, 50);
panel.add(lblSelectionSort);
//Set the text label for partition sort
lblPartitionSort.setBounds(500, 115, 200, 50);
panel.add(lblPartitionSort);
//Set the output label for bubble sort
bubbleSortOutput.setBounds(22, 155, 125, 25);
bubbleSortOutput.setHorizontalAlignment(JLabel.CENTER);
bubbleSortOutput.setBackground(Color.white);
bubbleSortOutput.setOpaque(true);
bubbleSortOutput.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
panel.add(bubbleSortOutput);
//Set the output label for insertion sort
insertionSortOutput.setBounds(181, 155, 125, 25);
insertionSortOutput.setHorizontalAlignment(JLabel.CENTER);
insertionSortOutput.setBackground(Color.white);
insertionSortOutput.setOpaque(true);
insertionSortOutput.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
panel.add(insertionSortOutput);
//Set the output label for selection sort
selectionSortOutput.setBounds(346, 155, 125, 25);
selectionSortOutput.setHorizontalAlignment(JLabel.CENTER);
selectionSortOutput.setBackground(Color.white);
selectionSortOutput.setOpaque(true);
selectionSortOutput.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
panel.add(selectionSortOutput);
//Set the output label for partition sort
partitionSortOutput.setBounds(510, 155, 125, 25);
partitionSortOutput.setHorizontalAlignment(JLabel.CENTER);
partitionSortOutput.setBackground(Color.white);
partitionSortOutput.setOpaque(true);
partitionSortOutput.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
panel.add(partitionSortOutput);
//Set parameters for the panel
setPanelParameters(panel);
//Set parameters for the GUI's frame and add panel
setFrameParameters(frame, panel);
}
//Getter Methods
public JFrame getFrame() {
return frame;
}
public JButton getButton() {
return btnStartTimer;
}
public JTextField getElementQty() {
return txtElementQty;
}
public JTextField getMinValue() {
return txtMinValue;
}
public JTextField getMaxValue() {
return txtMaxValue;
}
public JLabel getSortLabel(int sortNum) {
if(sortNum == 1) {
return bubbleSortOutput;
}
else if(sortNum == 2) {
return insertionSortOutput;
}
else if(sortNum == 3) {
return selectionSortOutput;
}
else if(sortNum == 4) {
return partitionSortOutput;
}
return null;
}
//Setter Methods
public void setSortColor(int sortNum, Color color) {
if(sortNum == 1) {
bubbleSortOutput.setBackground(color);
}
else if(sortNum == 2) {
insertionSortOutput.setBackground(color);
}
else if(sortNum == 3) {
selectionSortOutput.setBackground(color);
}
else if(sortNum == 4) {
partitionSortOutput.setBackground(color);
}
}
//Resets components to prepare for next
public void reset() {
bubbleSortOutput.setText("");
insertionSortOutput.setText("");
selectionSortOutput.setText("");
partitionSortOutput.setText("");
bubbleSortOutput.setBackground(Color.white);
insertionSortOutput.setBackground(Color.white);
selectionSortOutput.setBackground(Color.white);
partitionSortOutput.setBackground(Color.white);
}
//Set Frame and Panel parameters
void setFrameParameters(JFrame frame, JPanel panel) {
frame.setContentPane(panel);
frame.setBounds(0, 0, 660, 250);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void setPanelParameters(JPanel panel) {
panel.setLayout(null);
panel.setBackground(Color.white);
}
}