-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppWindow.java
More file actions
148 lines (133 loc) · 5.89 KB
/
AppWindow.java
File metadata and controls
148 lines (133 loc) · 5.89 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AppWindow extends JFrame{
public static final int FRAME_WIDTH = 150;
public static final int FRAME_HEIGHT = 200;
private JRadioButton jRadioButton1;
private JRadioButton jRadioButton2;
private JRadioButton jRadioButton3;
private ButtonGroup buttonGroup1;
private JButton submmit;
private JLabel fileNameLabel;
private JTextField fileNameTextField;
private PixelReader pixelReader;
private JTextField xTextField;
private JTextField yTextField;
private JTextField pixelNumberTextField;
private JLabel xLabel;
private JLabel YLabel;
private JLabel pixelNumberLabel;
private int x,y,len;
private String fileName;
private FileSaver fileSaver;
public AppWindow(){
initComponents();
}
private boolean goodNumberFormat(){
try{
x = Integer.parseInt(xTextField.getText());
y = Integer.parseInt(yTextField.getText());
len = Integer.parseInt(pixelNumberTextField.getText());
}catch(NumberFormatException e){
return false;
}
return true;
}
private boolean goodFileNameFormat(){
fileName = fileNameTextField.getText();
char c;
if(fileName.isEmpty())
return false;
for(int i = 0; i < fileName.length(); i++){
c = fileName.charAt(i);
if((c<'A'|| c>'Z') && (c<'a' || c > 'z') && (c<'0' || c>'9'))
return false;
}
return true;
}
private void initComponents(){
jRadioButton1 = new JRadioButton("Horizontally");
jRadioButton2 = new JRadioButton("Vertically");
jRadioButton3 = new JRadioButton("Diagonally");
buttonGroup1 = new ButtonGroup();
submmit = new JButton("Read'em!");
fileNameLabel = new JLabel("Name of the file :");
fileNameTextField = new JTextField("");
pixelReader = new PixelReader();
xTextField = new JTextField("0");
yTextField = new JTextField("0");
pixelNumberTextField = new JTextField("0");
xLabel = new JLabel("X coordinate");
YLabel = new JLabel("Y coordinate");
pixelNumberLabel = new JLabel("How many pixels to read?");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setTitle("Pixelator");
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds((int)dim.getWidth()/4,(int)dim.getHeight()/4, FRAME_WIDTH,FRAME_HEIGHT);
this.setIconImage(new ImageIcon("logo.jpg").getImage());
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
buttonGroup1.add(jRadioButton3);
getContentPane().setLayout(new GridLayout(12,1));
add(jRadioButton1);
add(jRadioButton2);
add(jRadioButton3);
add(xLabel);
add(xTextField);
add(YLabel);
add(yTextField);
add(pixelNumberLabel);
add(pixelNumberTextField);
add(fileNameLabel);
add(fileNameTextField);
add(submmit);
submmit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(goodFileNameFormat()){
if(goodNumberFormat()){
fileSaver = new FileSaver(fileNameTextField.getText());
if(jRadioButton1.isSelected()){
pixelReader.readHorizontally(x,y,len, fileSaver);
}
else if(jRadioButton2.isSelected()){
pixelReader.readVertically(x,y,len, fileSaver);
}
else if(jRadioButton3.isSelected()){
pixelReader.readDiagonally(x,y,len, fileSaver);
}
else
{
JOptionPane.showMessageDialog(rootPane,
"No options selected.",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(rootPane,
"Wrong number format",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(rootPane,
"Wrong file name format or empty field",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run() {
AppWindow appWindow = new AppWindow();
appWindow.setVisible(true);
appWindow.setResizable(false);
}
});
}
}