-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageCompressorGUI.java
More file actions
290 lines (247 loc) · 10.8 KB
/
Copy pathImageCompressorGUI.java
File metadata and controls
290 lines (247 loc) · 10.8 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package compressor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ImageCompressorGUI extends JFrame implements ActionListener {
private static final Color BACKGROUND_COLOR = new Color(37, 44, 53);
private static final Color FOREGROUND_COLOR = Color.WHITE;
private static final Color BUTTON_COLOR = new Color(50, 100, 200);
private static final Color BUTTON_HOVER_COLOR = new Color(70, 120, 220);
private static final Color BORDER_COLOR = new Color(80, 80, 80);
private static final int IMAGE_PREVIEW_WIDTH = 380;
private static final int IMAGE_PREVIEW_HEIGHT = 380;
private final JLabel statusLabel;
private File selectedFile;
private final ImageCompressor compressor;
private final FileHandler fileHandler;
private final JLabel imagePreview;
private JSlider qualitySlider;
private JTextField qualityTextField;
public ImageCompressorGUI() {
compressor = new ImageCompressor();
fileHandler = new FileHandler();
setTitle("Image Compressor");
setSize(640, 480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setResizable(false);
getContentPane().setBackground(BACKGROUND_COLOR);
add(createHeader(), BorderLayout.NORTH);
imagePreview = createImagePreview();
add(imagePreview, BorderLayout.CENTER);
JPanel controlPanel = createControlPanel();
add(controlPanel, BorderLayout.EAST);
statusLabel = createStatusLabel();
add(statusLabel, BorderLayout.SOUTH);
}
private JLabel createHeader() {
JLabel header = new JLabel("Image Compressor", JLabel.CENTER);
header.setFont(new Font("Arial", Font.BOLD, 24));
header.setForeground(FOREGROUND_COLOR);
header.setBorder(new EmptyBorder(20, 5, 20, 5));
return header;
}
private JLabel createImagePreview() {
JLabel preview = new JLabel("No Image Selected", JLabel.CENTER);
preview.setFont(new Font("Arial", Font.PLAIN, 14));
preview.setPreferredSize(new Dimension(IMAGE_PREVIEW_WIDTH, IMAGE_PREVIEW_HEIGHT));
preview.setBorder(BorderFactory.createLineBorder(BORDER_COLOR));
preview.setForeground(FOREGROUND_COLOR);
preview.setBackground(BACKGROUND_COLOR);
preview.setOpaque(true);
return preview;
}
private JPanel createControlPanel() {
JPanel controlPanel = new JPanel(new GridBagLayout());
controlPanel.setBackground(BACKGROUND_COLOR);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
controlPanel.add(createButton("Select Image", e -> selectImage()), gbc);
gbc.gridy++;
controlPanel.add(createButton("Compress", this), gbc);
gbc.gridy++;
qualitySlider = createQualitySlider();
controlPanel.add(createQualityPanel(), gbc);
gbc.gridy++;
controlPanel.add(qualitySlider, gbc);
return controlPanel;
}
private JButton createButton(String text, ActionListener actionListener) {
JButton button = new JButton(text);
button.setFocusPainted(false);
button.setBackground(BUTTON_COLOR);
button.setFont(new Font("Arial", Font.PLAIN, 14));
button.setForeground(FOREGROUND_COLOR);
button.setOpaque(true);
button.setBorderPainted(false);
button.setPreferredSize(new Dimension(120, 30));
button.addMouseListener(new ButtonHoverListener(button));
button.addActionListener(actionListener);
return button;
}
private JSlider createQualitySlider() {
JSlider slider = new JSlider(0, 100, 70);
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setForeground(FOREGROUND_COLOR);
slider.setBackground(BACKGROUND_COLOR);
slider.addChangeListener(e -> qualityTextField.setText(String.valueOf(qualitySlider.getValue())));
return slider;
}
private JPanel createQualityPanel() {
JPanel qualityPanel = new JPanel();
qualityPanel.setBackground(BACKGROUND_COLOR);
JLabel label = new JLabel("Image Quality:", JLabel.CENTER);
label.setForeground(Color.WHITE);
label.setFont(new Font("Arial", Font.PLAIN, 14));
qualityPanel.add(label);
qualityTextField = createQualityTextField();
qualityPanel.add(qualityTextField);
JLabel percentLabel = new JLabel("%");
percentLabel.setFont(new Font("Arial", Font.PLAIN, 14));
percentLabel.setForeground(Color.WHITE);
qualityPanel.add(percentLabel);
return qualityPanel;
}
private JTextField createQualityTextField() {
JTextField textField = new JTextField(2);
textField.setText(String.valueOf(qualitySlider.getValue()));
textField.setForeground(FOREGROUND_COLOR);
textField.setBackground(BACKGROUND_COLOR);
textField.setCaretColor(FOREGROUND_COLOR);
textField.setBorder(BorderFactory.createLineBorder(BORDER_COLOR));
textField.setHorizontalAlignment(JTextField.CENTER);
// Add a listener to update slider when text field is changed
textField.addActionListener(e -> {
try {
int value = Integer.parseInt(textField.getText());
if (value < 0 || value > 100) throw new NumberFormatException();
qualitySlider.setValue(value);
} catch (NumberFormatException ex) {
textField.setText(String.valueOf(qualitySlider.getValue())); // Reset to slider value if input is invalid
}
});
return textField;
}
private JLabel createStatusLabel() {
JLabel label = new JLabel("Status: No file selected", JLabel.CENTER);
label.setFont(new Font("Arial", Font.PLAIN, 14));
label.setForeground(FOREGROUND_COLOR);
label.setBorder(new EmptyBorder(15, 0, 15, 0));
return label;
}
private static class ButtonHoverListener extends java.awt.event.MouseAdapter {
private final JButton button;
private final Color originalColor;
public ButtonHoverListener(JButton button) {
this.button = button;
this.originalColor = button.getBackground();
}
@Override
public void mouseEntered(java.awt.event.MouseEvent evt) {
button.setBackground(BUTTON_HOVER_COLOR);
}
@Override
public void mouseExited(java.awt.event.MouseEvent evt) {
button.setBackground(originalColor);
}
@Override
public void mousePressed(java.awt.event.MouseEvent evt) {
button.setBackground(BUTTON_HOVER_COLOR.darker());
}
@Override
public void mouseReleased(java.awt.event.MouseEvent evt) {
button.setBackground(BUTTON_HOVER_COLOR);
}
}
private void selectImage() {
selectedFile = fileHandler.chooseFile();
if (selectedFile != null) {
statusLabel.setText("Selected file: " + selectedFile.getName());
displayImagePreview(selectedFile);
} else {
resetImagePreview();
}
}
private void resetImagePreview() {
statusLabel.setText("No file selected");
imagePreview.setIcon(null);
imagePreview.setText("No Image Selected");
imagePreview.setFont(new Font("Arial", Font.PLAIN, 14));
}
public void displayImagePreview(File file) {
try {
ImageIcon icon = new ImageIcon(file.getPath());
Image originalImage = icon.getImage();
int originalWidth = originalImage.getWidth(null);
int originalHeight = originalImage.getHeight(null);
// Calculate new dimensions maintaining aspect ratio
double aspectRatio = (double) originalWidth / originalHeight;
int newWidth = IMAGE_PREVIEW_WIDTH;
int newHeight = (int) (IMAGE_PREVIEW_WIDTH / aspectRatio);
if (newHeight > IMAGE_PREVIEW_HEIGHT) {
newHeight = IMAGE_PREVIEW_HEIGHT;
newWidth = (int) (IMAGE_PREVIEW_HEIGHT * aspectRatio);
}
Image scaledImage = originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
imagePreview.setIcon(new ImageIcon(scaledImage));
imagePreview.setText(null); // Clear text if image is displayed
} catch (Exception e) {
imagePreview.setText("Could not load image preview.");
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (selectedFile != null) {
try {
File outputFile = new File("compressed_" + selectedFile.getName());
float quality = qualitySlider.getValue() / 100f;
compressor.compressImage(selectedFile, outputFile, quality);
statusLabel.setText("Image compressed to: " + outputFile.getPath());
JOptionPane.showMessageDialog(this,
"Image successfully compressed!",
"Success",
JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this,
"Error: Unsupported Format. Use either .jpg or .png",
"Error",
JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this,
"Please select an image file first!",
"Error",
JOptionPane.ERROR_MESSAGE);
statusLabel.setText("Please select an image first.");
}
}
@Override
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
int confirmed = JOptionPane.showConfirmDialog(this,
"Are you sure you want to exit?",
"Exit Confirmation",
JOptionPane.YES_NO_OPTION);
if (confirmed == JOptionPane.YES_OPTION) {
dispose();
}
} else {
super.processWindowEvent(e);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
ImageCompressorGUI gui = new ImageCompressorGUI();
gui.setVisible(true);
});
}
}