-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditorFrame.java
384 lines (335 loc) · 14.4 KB
/
EditorFrame.java
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class OptionWindow extends JFrame {
private JButton clearWindowButton = new JButton("Clear");
private JButton grayScaleButton = new JButton("Black&White");
private JButton negativeButton = new JButton("Negative");
private JButton recoverButton = new JButton("Recover");
private JButton storeImageButton = new JButton("Store Image");
private JButton logTransformationButton = new JButton("LogT");
private JButton powerLawTransformationButton = new JButton("PowT");
private JButton exponentialTransformationButton = new JButton("ExpT");
private JButton rotateImageButton = new JButton("Rotate");
private JButton removeColorButton = new JButton("ChangeColor");
private JButton scalingButton = new JButton("Scale");
private JButton shiftButton = new JButton("Shift");
private JPanel optionPanel = new JPanel();
private float inputPrompt(String msg) {
float inputFloat = -1f;
if(EditorFrame.image == null) return inputFloat;
String inputString = JOptionPane.showInputDialog(msg);
if (inputString == null)
return -1f;
else {
try {
inputFloat = Float.parseFloat(inputString);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Not a number!");
}
}
return inputFloat;
}
private Color getRGB(String msg) {
if(EditorFrame.image == null) return null;
int[] RGB = new int[3];
int k = 0;
String inputString = JOptionPane.showInputDialog(msg);
inputString = inputString + " ";
String temp = new String("");
for (int i = 0; i < inputString.length(); i++) {
if (inputString.charAt(i) == ' ') {
if (k == 3) {
JOptionPane.showMessageDialog(null, "Invalid RGB!");
return null;
}
int number = Integer.parseInt(temp);
if (number > 255 || number < 0)
return null;
RGB[k++] = number;
temp = "";
} else {
temp += inputString.charAt(i);
}
}
return new Color(RGB[0], RGB[1], RGB[2]);
}
private static boolean isPositiveShift(){
if(EditorFrame.image == null) return true;
String input = JOptionPane.showInputDialog(null, "Enter the +ve or -ve shift(+ or -): ");
if(input == null || input.equals("")) return true;
for(char c: input.toCharArray()){
if(c == '+') return true;
if(c == '-') return false;
}
return true;
}
// singleton instance
private static OptionWindow optionWindowInstance;
public static OptionWindow getOptionWindow() {
if (optionWindowInstance == null) {
optionWindowInstance = new OptionWindow();
}
return optionWindowInstance;
}
private OptionWindow() {
this.setTitle("Options");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setLocationRelativeTo(null);
clearWindowButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TransformationFunctions.clearTheWindow(EditorFrame.image);
}
});
grayScaleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TransformationFunctions.grayScaleFilter(EditorFrame.image);
}
});
negativeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TransformationFunctions.negativeFilter(EditorFrame.image);
}
});
recoverButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TransformationFunctions.recoverToPreviousCurrentState(EditorFrame.image);
}
});
storeImageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TransformationFunctions.storeCurrentImageState(EditorFrame.image);
}
});
logTransformationButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
float scalingFactor = inputPrompt("Enter the Scaling Factor: ");
if (scalingFactor < 0f)
return;
float upscalingFactor = inputPrompt("Input Upscaling Factor: ");
if (upscalingFactor < 0f)
return;
TransformationFunctions.logTransformation(EditorFrame.image, scalingFactor, upscalingFactor);
}
});
powerLawTransformationButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
float scalingFactor = inputPrompt("Enter the Scaling Factor: ");
if (scalingFactor < 0f)
return;
float gammaFactor = inputPrompt("Enter the Gamma Factor: ");
if (gammaFactor < 0f)
return;
TransformationFunctions.powerLawTransformation(EditorFrame.image, scalingFactor, gammaFactor);
}
});
exponentialTransformationButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
float scalingFactor = inputPrompt("Enter the Scaling Factor: ");
if (scalingFactor < 0f)
return;
TransformationFunctions.exponentialTransformation(EditorFrame.image,
scalingFactor);
}
});
rotateImageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double angle = inputPrompt("Enter the rotation angle in degree(respect to the original pos): ");
TransformationFunctions.rotateTheImage(EditorFrame.image,
angle);
}
});
removeColorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TransformationFunctions.replaceTheColor(EditorFrame.image,
getRGB("Enter the color to remove(like: r:56 g:47 b:89): "),
getRGB("Enter the color to put in replace: "));
}
});
scalingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
float delX = inputPrompt("Enter the scale X: ");
float delY = inputPrompt("Enter the scale Y: ");
TransformationFunctions.scalingPos(EditorFrame.image, delX, delY);
}
});
shiftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TransformationFunctions.shiftRGB(EditorFrame.image, getRGB("Enter the shift RGB: "), isPositiveShift());
}
});
optionPanel.setLayout(new GridLayout(4, 2, 2, 1));
optionPanel.add(clearWindowButton);
optionPanel.add(grayScaleButton);
optionPanel.add(negativeButton);
optionPanel.add(recoverButton);
optionPanel.add(storeImageButton);
optionPanel.add(logTransformationButton);
optionPanel.add(powerLawTransformationButton);
optionPanel.add(exponentialTransformationButton);
optionPanel.add(rotateImageButton);
optionPanel.add(removeColorButton);
optionPanel.add(scalingButton);
optionPanel.add(shiftButton);
this.add(optionPanel);
this.pack();
this.setResizable(true);
this.setMaximizedBounds(new Rectangle(new Dimension(400, 200)));
this.setVisible(true);
}
}
public class EditorFrame {
private static JPanel panel;
private static JFrame frame;
private static JLabel label;
static BufferedImage image;
private JButton openImageButton;
private JButton optionButton;
private JButton saveButton;
public static void displayImage(BufferedImage img) {
if (img == null)
return;
int panelWidth = panel.getWidth();
int panelHeight = panel.getHeight();
int imageWidth = img.getWidth();
int imageHeight = img.getHeight();
double scaleFactor = Math.min((double) panelWidth / imageWidth, (double) panelHeight / imageHeight);
int scaledWidth = (int) (imageWidth * scaleFactor);
int scaledHeight = (int) (imageHeight * scaleFactor);
ImageIcon scaledIcon = new ImageIcon(img.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH));
label = new JLabel(scaledIcon);
label.setPreferredSize(new Dimension(scaledWidth, scaledHeight));
panel.add(label);
panel.revalidate();
panel.repaint();
frame.revalidate();
frame.repaint();
}
public static void removePreviousImageFromPanel() {
if (EditorFrame.label == null) {
System.out.println("EdittorFrame.JLabel is null!");
return;
}
JLabel component = EditorFrame.label;
panel.remove(component);
panel.revalidate();
panel.repaint();
}
public EditorFrame() {
panel = new JPanel();
openImageButton = new JButton("Open");
optionButton = new JButton("Options");
saveButton = new JButton("Save");
panel.add(openImageButton);
panel.add(optionButton);
panel.add(saveButton);
openImageButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Open Image");
int userSelection = fileChooser.showOpenDialog(null);
if (userSelection == JFileChooser.APPROVE_OPTION) {
try {
File fileToOpen = fileChooser.getSelectedFile();
image = ImageIO.read(fileToOpen);
removePreviousImageFromPanel();
displayImage(image);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
optionButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
OptionWindow.getOptionWindow();
}
});
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (EditorFrame.image == null || EditorFrame.label == null)
return;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Save Image");
int userSelection = fileChooser.showSaveDialog(null);
if (userSelection == JFileChooser.APPROVE_OPTION) {
JLabel label = EditorFrame.label;
if (label.getIcon() != null) {
try {
Image image;
if (label.getIcon() instanceof ImageIcon) {
image = ((ImageIcon) label.getIcon()).getImage();
} else {
throw new UnsupportedOperationException("Unsupported image format in label");
}
BufferedImage bufferedImage;
if (!(image instanceof BufferedImage)) {
bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
} else {
bufferedImage = (BufferedImage) image;
}
File fileToSave = fileChooser.getSelectedFile();
ImageIO.write(bufferedImage, "png", fileToSave);
label.setText("Image saved successfully!");
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error saving image: " + ex.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
} catch (UnsupportedOperationException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Image format in label not yet supported",
"Unsupported Image", JOptionPane.INFORMATION_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "No image found in the label to save", "No Image",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
frame = new JFrame("J-Editor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 700);
frame.setLocationRelativeTo(null);
frame.add(panel);
frame.setResizable(false);
frame.setVisible(true);
}
public static void main(String[] args) {
new EditorFrame();
}
}