-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenu.java
More file actions
194 lines (162 loc) · 6.78 KB
/
Menu.java
File metadata and controls
194 lines (162 loc) · 6.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
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
package src.classSrc;
import java.util.ArrayList;
import java.awt.Color;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseEvent;
import java.awt.Component.*;
public class Menu extends JFrame {
private ImageIcon backgroundImg;
private JLabel backgroundLabel;
private JPanel backgroundPanel;
//private JPanel learnPanel;
//private JPanel testPanel;
private JButton learnButton;
private JButton testButton;
private JButton frontButton;
private final JComboBox<String> sortSelectComboBox;
String[] option = { "SORT", "Insertion", "Merge", "Bubble", "Quick", "Selection" };
public Menu() {
// GUI init
super("ALGORITHM VISUALIZATION");
setSize(1000, 600);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
// set background
backgroundImg = new ImageIcon("src/imageSrc/Background.png");
backgroundLabel = new JLabel(backgroundImg);
backgroundLabel.setBounds(0, 0, getWidth(), getHeight());
backgroundPanel = (JPanel) this.getContentPane();
backgroundPanel.setOpaque(false);
getLayeredPane().add(backgroundLabel, Integer.valueOf(Integer.MIN_VALUE));
// set listener
ActionListener handler = new TestEventListener();
ActionListener listener = new SortEventListener();
// set color
Color myRed = new Color(255, 163, 163);
Color myBlue = new Color(102, 178, 255);
Color myGreen = new Color(153, 255, 153);
Color myPink = new Color(255, 240, 245);
// set learn button
learnButton = new JButton(new ImageIcon("src/imageSrc/uncheckedLearn.png"));
learnButton.setBounds(210, 100, 434, 100);
learnButton.setFont(new Font("Helvetica", Font.PLAIN, 24));
learnButton.setOpaque(false);
learnButton.setContentAreaFilled(false);
learnButton.setFocusPainted(false);
learnButton.setBorder(BorderFactory.createLineBorder(myRed, 2));
// learnButton.setBorderPainted(false);
learnButton.addActionListener(listener);
// set test button
testButton = new JButton(new ImageIcon("src/imageSrc/uncheckedTest.png"));
testButton.setBounds(210,225, 550,100);
testButton.setFont(new Font("Helvetica", Font.PLAIN, 24));
testButton.setOpaque(false);
testButton.setContentAreaFilled(false);
testButton.setFocusPainted(false);
testButton.setBorder(BorderFactory.createLineBorder(myBlue, 2));
// testButton.setBorderPainted(false);
testButton.addActionListener(handler);
// set Menu Button
frontButton = new JButton(new ImageIcon("src/imageSrc/uncheckedFront.png"));
frontButton.setBounds(210, 350 , 550, 100);
frontButton.setFont(new Font("Helvetica", Font.PLAIN, 24));
frontButton.setOpaque(false);
frontButton.setContentAreaFilled(false);
frontButton.setFocusPainted(false);
frontButton.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 2));
// frontButton.setBorderPainted(false);
frontButton.addActionListener(handler);
// set btn listener
ButtonListener buttonListener = new ButtonListener();
learnButton.addMouseMotionListener(buttonListener); // move、drag
testButton.addMouseMotionListener(buttonListener);
frontButton.addMouseMotionListener(buttonListener);
addMouseMotionListener(buttonListener);
// set comboBox
sortSelectComboBox = new JComboBox<>(option);
sortSelectComboBox.setFont(new Font("Times New Roman", Font.BOLD, 12));
sortSelectComboBox.setBounds(660, 100, 100, 20);
sortSelectComboBox.setRenderer(new DefaultListCellRenderer() {
{
setHorizontalAlignment(DefaultListCellRenderer.CENTER);
}
});
// add new elements to frame
add(sortSelectComboBox);
add(learnButton);
add(testButton);
add(frontButton);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
private class ButtonListener extends MouseInputAdapter {
public void mouseMoved(MouseEvent e) {
if (e.getSource() == learnButton) {
learnButton.setIcon(new ImageIcon("src/imageSrc/Learn.png"));
} else {
learnButton.setIcon(new ImageIcon("src/imageSrc/uncheckedLearn.png"));
}
if (e.getSource() == testButton) {
testButton.setIcon(new ImageIcon("src/imageSrc/Test.png"));
} else {
testButton.setIcon(new ImageIcon("src/imageSrc/uncheckedTest.png"));
}
if (e.getSource() == frontButton) {
frontButton.setIcon(new ImageIcon("src/imageSrc/Front.png"));
} else {
frontButton.setIcon(new ImageIcon("src/imageSrc/uncheckedFront.png"));
}
}
}
private class SortEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String sort = sortSelectComboBox.getItemAt(sortSelectComboBox.getSelectedIndex());
// Go to the tutorial area depending on the selection of dropDownMenu value
switch (sort) {
case "Insertion":
new InsertSortVisualizationFrame();
setVisible(false);
break;
case "Merge":
new MergeSortVisualizationFrame();
setVisible(false);
break;
case "Bubble":
new BubbleSortVisualizationFrame();
setVisible(false);
break;
case "Quick":
new QuickSortVisualizationFrame();
setVisible(false);
break;
case "Selection":
new SelectionSortVisualizationFrame();
setVisible(false);
break;
}
}
}
private class TestEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// Go to test area
if (e.getSource() == testButton) {
TestInfoFrame testInfoFrame = new TestInfoFrame();
setVisible(false);
} else if (e.getSource() == frontButton) {
new MainFrame();
setVisible(false);
}
}
}
}