-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
277 lines (220 loc) · 8.61 KB
/
Copy pathGUI.java
File metadata and controls
277 lines (220 loc) · 8.61 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
import java.awt.image.BufferedImage;
import java.awt.image.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.ArrayList;
/**
* @Jonathan Ke @9/14/2019
*
*User interface for ImageMosaic display
**/
public class GUI implements ActionListener, MouseListener{
private int size; // number of images per row of image
private int randVal; // randomness of picture control
private int finalWidth; // pic width
private int finalHeight; // pic height
//scale factor for smaller image
private double scaleFactor = 0.1;
private String imagesUtilized;
// images stored in window
private BufferedImage origImage;
private BufferedImage outputImage;
// panels and components
private JLabel errorMessage = new JLabel(" ");;
private JTextField sizeTextBox;
private JTextField randomTextBox;
private JTextField mainImageBox;
private JTextField folderBox;
private JLabel pic;
private JLabel scaledLabel;
private SubImage[] inputImages;
public GUI(String original, String folder) {
String originalImageStr = original; // The Big Image
imagesUtilized = folder; // Directory of Small Images
origImage = LargeImage.loadImage(originalImageStr);
// fill in field variables EDIT FOR DIFFERENTIAL APPEARANCES
size = 80;
randVal = 5;
finalWidth = 700;
finalHeight = origImage.getHeight() * finalWidth / origImage.getWidth(); // height as scale factor of width
// rescale original image to these specifications
origImage = Resizer.resize(origImage, finalWidth, finalHeight);
// get output from mosaic algorithm in MapPoints
outputImage = getMosaic();
// draw the GUI
GridLayout gridLayout = new GridLayout(0, 1);
JPanel pRight = new JPanel(gridLayout);
gridLayout.setHgap(0);
gridLayout.setVgap(0);
JLabel sizeField = new JLabel("Enter Resolution Quality (1-200):");
sizeTextBox = new JTextField();
JLabel randomField = new JLabel("Enter Randomness Factor (0-20):");
randomTextBox = new JTextField();
JButton enterParameters = new JButton("Enter");
enterParameters.addActionListener(this);
JLabel originalImageLabel = new JLabel("Enter main image:");
mainImageBox = new JTextField();
JLabel fileLabel = new JLabel("Enter image file:");
folderBox = new JTextField();
FileInput fileInput = new FileInput(mainImageBox,folderBox,this);
JButton enterFiles = new JButton("Image Reload");
enterFiles.addActionListener(fileInput);
scaledLabel = new JLabel(new ImageIcon(getScaledOriginal()));
JButton scaleButton = new JButton("MOSAIC SUBIMAGE");
scaleButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
origImage = Resizer.resize(scaledOriginal, finalWidth, finalHeight);
setMosaic();
}
});
pRight.add(scaledLabel);
pRight.add(scaleButton);
pRight.add(sizeField);
pRight.add(sizeTextBox);
pRight.add(randomField);
pRight.add(randomTextBox);
pRight.add(enterParameters);
pRight.add(errorMessage);
JPanel pLeft = new JPanel(new GridLayout(1,0));
pLeft.add(originalImageLabel);
pLeft.add(mainImageBox);
pLeft.add(fileLabel);
pLeft.add(folderBox);
pLeft.add(enterFiles);
pic = new JLabel(new ImageIcon(getFinalImage())); //actual mosaic image used
pic.addMouseListener(this);
//final panel appended to f
JPanel pFinal = new JPanel(new BorderLayout(1, 1));
JPanel pSub = new JPanel(new BorderLayout(1,1));
pSub.add(pic,BorderLayout.LINE_START);
pSub.add(pRight,BorderLayout.CENTER);
pFinal.add(pSub,BorderLayout.CENTER);
pFinal.add(pLeft, BorderLayout.SOUTH);
JFrame f = new JFrame("Mosaic Lab");
f.add(pFinal);
f.pack();
f.setFocusable(true);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void setImageUtilized(String a){
imagesUtilized = a;
}
public void setOriginalImage(String a){
origImage = LargeImage.loadImage(a);
finalWidth = 700;
finalHeight = origImage.getHeight() * finalWidth / origImage.getWidth();
origImage = Resizer.resize(origImage, finalWidth, finalHeight);
}
//button action listener
@Override
public void actionPerformed(ActionEvent e) {
String sizeText = sizeTextBox.getText();
String randText = randomTextBox.getText();
int size = this.size;
int rand = this.randVal;
try{
size = Integer.parseInt(sizeText);
} catch (Exception ex){
setErrorMessage("INVALID RESOLUTION VALUE");
sizeTextBox.setText("");
return;
}
try{
rand = Integer.parseInt(randText);
} catch (Exception ex){
setErrorMessage("INVALID RANDOMNESS FACTOR");
randomTextBox.setText("");
return;
}
if (size < 1 || size > 200) {
setErrorMessage("INVALID RESOLUTION VALUE");
return;
} else if (rand > 20 || rand < 0){
setErrorMessage("INVALID RANDOMNESS FACTOR");
return;
} else {
this.size = size;
this.randVal = rand;
sizeTextBox.setText("");
randomTextBox.setText("");
setErrorMessage(" ");
System.out.println(this.size+ " "+this.randVal);
setMosaic();
}
}
//mouse listener methods
@Override
public void mouseClicked(MouseEvent e){
int x = e.getX();
int y = e.getY();
System.out.println(x+" "+y);
BufferedImage clickedImage = getSubImage(x,y);
//find image and update scaledLabel
scaledOriginal = Resizer.resize(clickedImage, 100,100);
scaledLabel.setIcon(new ImageIcon(scaledOriginal));
}
private SubImage[][] outputArray;
//x and y are the pixel coordinates of the mosaic image
private BufferedImage getSubImage(int x, int y){
int pixelSizeX = finalWidth/size;
int pixelSizeY = finalHeight/size;
int iX = x/pixelSizeX;
int iY = Math.max(y/pixelSizeY-1,0);
SubImage s = outputArray[iX][iY];
int imageIndex = s.getIndex();
System.out.println(imageIndex);
BufferedImage b = MapPoints.loadImage(imagesUtilized+"/"+imagesUtilized+"_",(imageIndex));
return b;
}
@Override
public void mousePressed(MouseEvent e){
}
@Override
public void mouseReleased(MouseEvent e){
}
@Override
public void mouseEntered(MouseEvent e){
}
@Override
public void mouseExited(MouseEvent e){
}
public void setErrorMessage(String msg){
errorMessage.setText(msg);
}
public BufferedImage getMosaic(){
// Construct object maps and points
LargeImage lg = new LargeImage(origImage, size, size);
// the images used to mosaic
inputImages = MapPoints.getImages(imagesUtilized + "/" + imagesUtilized + "_",
new File(imagesUtilized + "/").listFiles().length - 1, origImage.getWidth() / size,
origImage.getHeight() / size);
outputArray = MapPoints.findImageMosaic(randVal, lg.getSubImageArray(), inputImages);
//scaled original image
scaledOriginal = Resizer.resize(lg.getMainImage(), 100,100);
return MapPoints.pasteImages(outputArray,lg.getMainImage().getWidth(),lg.getMainImage().getHeight());
}
public void setMosaic(){
outputImage = getMosaic();
pic.setIcon(new ImageIcon(outputImage));
scaledLabel.setIcon(new ImageIcon(scaledOriginal));
System.out.println("DONE!");
}
private BufferedImage scaledOriginal;
private Image getScaledOriginal(){
return scaledOriginal;
}
public BufferedImage getFinalImage(){
return outputImage;
}
public BufferedImage getOriginalImage(){
return origImage;
}
public static void main(String[] args){
GUI g = GUI();
}
}