-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssortingWindow.java
More file actions
180 lines (151 loc) · 6.15 KB
/
ssortingWindow.java
File metadata and controls
180 lines (151 loc) · 6.15 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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
class SortingWindow {
private Timer[] timers;
private int speed = 25;
private final Sorts[] sorts;
private final GuiSorts parentFrame;
private JPanel resultsPanel;
private JPanel upperMenu;
private JLabel[] durations;
public SortingWindow(Sorts[] sorts, GuiSorts parentFrame) {
this.sorts = sorts;
this.parentFrame = parentFrame;
this.timers = new Timer[sorts.length];
this.durations = new JLabel[sorts.length];
createAndShowSortingWindow();
}
private void createAndShowSortingWindow() {
resultsPanel = new JPanel();
int panelAmount = sorts.length;
resultsPanel.setLayout(new GridLayout((panelAmount + 1) / 2, 2, 20, 20));
resultsPanel.setBackground(Color.BLACK);
createUpperMenu(panelAmount);
createSortingPanels(panelAmount);
parentFrame.add(upperMenu, BorderLayout.NORTH);
parentFrame.add(resultsPanel, BorderLayout.CENTER);
parentFrame.revalidate();
parentFrame.repaint();
}
private void createUpperMenu(int panelAmount) {
upperMenu = new JPanel();
upperMenu.setBackground(Color.BLACK);
JButton exitButton = new JButton("Back");
exitButton.addActionListener(e -> handleExit());
upperMenu.add(exitButton);
JButton speedUpButton = new JButton("1x");
speedUpButton.addActionListener(e -> handleSpeedChange(e, panelAmount));
upperMenu.add(speedUpButton);
}
private void createSortingPanels(int panelAmount) {
for (int i = 0; i < panelAmount; i++) {
JPanel outerPanel = new JPanel(new BorderLayout());
outerPanel.setBackground(Color.BLACK);
JPanel sortingPanel = createAnimatedPanel(sorts[i], i);
outerPanel.add(sortingPanel, BorderLayout.CENTER);
addPanelLabels(outerPanel, sorts[i].name, i);
resultsPanel.add(outerPanel);
}
}
private void addPanelLabels(JPanel outerPanel, String sortName, int index) {
JLabel nameLabel = new JLabel(sortName);
nameLabel.setForeground(Color.WHITE);
nameLabel.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 30));
nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
outerPanel.add(nameLabel, BorderLayout.NORTH);
JLabel durationLabel = new JLabel("0.000000s");
durationLabel.setForeground(Color.WHITE);
try{
Font digitalFont = Font.createFont(Font.TRUETYPE_FONT, new File("C:\\SortsJava\\Fonts\\Digital.ttf")).deriveFont(40f);
durationLabel.setFont(digitalFont);
}catch(FontFormatException | IOException e)
{
e.printStackTrace();
durationLabel.setFont(new Font("Arial", Font.BOLD, 24));
}
durationLabel.setHorizontalAlignment(SwingConstants.CENTER); // Center the text
outerPanel.add(durationLabel, BorderLayout.SOUTH);
durations[index] = durationLabel; // Store the label reference
}
private JPanel createAnimatedPanel(Sorts type, int index) {
type.sort(); // Run the sort
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawSortingBars(g, type.getArrayState());
}
};
panel.setPreferredSize(new Dimension(1000, 500));
panel.setBackground(Color.BLACK);
Timer timer = new Timer(speed, e -> {
if (type.getArrayState() == null) {
((Timer) e.getSource()).stop();
} else {
panel.repaint();
// Format the duration to show seconds with 6 decimal places
long currentTime = type.getCurrenttime();
double seconds = currentTime / 1_000_000_000.0; // Convert nanoseconds to seconds
String formattedTime = String.format("%.6fs", seconds);
durations[index].setText(formattedTime);
}
});
timers[index] = timer;
timer.start();
return panel;
}
private void drawSortingBars(Graphics g, int[] array) {
if (array == null) return;
int spaceBetweenBars = 1;
int width = (g.getClipBounds().width / array.length) - spaceBetweenBars;
int panelHeight = g.getClipBounds().height;
for (int i = 0; i < array.length; i++) {
int height = (int) ((double) array[i] / 1000000 * panelHeight);
g.setColor(Color.BLUE);
g.fillRect(i * (width + spaceBetweenBars), panelHeight - height, width, height);
}
}
private void handleSpeedChange(ActionEvent e, int panelAmount) {
JButton button = (JButton) e.getSource();
String buttonText = button.getText();
switch (buttonText) {
case "4x":
speed = 25;
button.setText("1x");
break;
case "2x":
speed /= 2;
button.setText("4x");
break;
default:
speed /= 2;
button.setText("2x");
break;
}
for (int i = 0; i < panelAmount; i++) {
if (timers[i] != null) {
timers[i].setDelay(buttonText.equals("2x") ? 1 : speed);
}
}
}
private void handleExit() {
// Stop all timers
for (Timer timer : timers) {
if (timer != null && timer.isRunning()) {
timer.stop();
}
}
// Remove panels
parentFrame.remove(resultsPanel);
parentFrame.remove(upperMenu);
// Reset parent frame
parentFrame.resetToStartPanel();
// Clean up
System.gc();
}
}