-
Notifications
You must be signed in to change notification settings - Fork 0
/
EndeavorUI.java
126 lines (113 loc) · 3.5 KB
/
EndeavorUI.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
import javax.imageio.ImageIO;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.*;
import java.io.*;
import java.util.*;
/**
* Main UI class for the Endeavor image editing program.
*
* @author Joseph Daly
* @version 0.0.1
*/
public class EndeavorUI extends JFrame
{
// Current image
private BufferedImage image;
private JLabel displayedImage;
private JFileChooser fileChoose = new JFileChooser();
private JMenuBar toolbar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenu toolMenu = new JMenu("Tools");
private EndeavorUI ref = this;
ArrayList<EndeavorTool> tools = new ArrayList<EndeavorTool>() {{add(new EndeavorColorBalance()); add(new EndeavorChromaKey());}};
/**
* Constructor for objects of class EndeavorUI
*/
public EndeavorUI()
{
super("Endeavor");
setSize(1000, 700);
setLayout(new FlowLayout());
setVisible(true);
displayedImage = new JLabel();
add(displayedImage);
JMenuItem openImageMenu = new JMenuItem("Open");
openImageMenu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
ref.askUserToOpenImage();
} catch (IOException e1) {
JOptionPane.showMessageDialog(ref, "An I/O error occured. Please check access permissions and file type.");
}
}
});
JMenuItem saveImageMenu = new JMenuItem("Save");
saveImageMenu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ref.askUserToSaveImage();
}
});
fileMenu.add(openImageMenu);
fileMenu.add(saveImageMenu);
for(EndeavorTool tool : tools)
{
addTool(tool, tool.getName());
}
toolbar.add(fileMenu);
toolbar.add(toolMenu);
setJMenuBar(toolbar);
}
public void openImage(File imageFile) throws IOException
{
try {
image = ImageIO.read(imageFile);
displayedImage.setIcon(new ImageIcon(image));
} catch (IOException e1) {
JOptionPane.showMessageDialog(ref, "An I/O error occured. Please check access permissions and file type.");
}
}
public void askUserToOpenImage() throws IOException
{
int returnVal = fileChoose.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fileChoose.getSelectedFile();
openImage(file);
}
else
{
JOptionPane.showMessageDialog(this, "Please select an image!");
}
}
public void askUserToSaveImage()
{
int returnVal = fileChoose.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
try {
File file = fileChoose.getSelectedFile();
ImageIO.write(image, "png", file);
} catch (IOException e1) {
JOptionPane.showMessageDialog(this, "An error occured while saving the image.");
}
}
}
public void addTool(EndeavorTool tool, String name)
{
EndeavorUI ref = this;
JMenuItem toolButton = new JMenuItem(name);
toolButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
EndeavorImageManip.apply_effect(ref, image, tool);
displayedImage.setIcon(new ImageIcon(image));
}
});
toolMenu.add(toolButton);
}
}