-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGui.java
More file actions
419 lines (362 loc) · 12.1 KB
/
Gui.java
File metadata and controls
419 lines (362 loc) · 12.1 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.JComponent;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.io.File;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
/* Create the graphical user interface */
public class Gui extends JFrame implements ActionListener, MouseListener {
private static final long serialVersionUID = 1;
private JPanel buttonPanel;
private JButton buttons[];
private Graphics display; // Graphics object needed to draw
private Board panel; // Panel containing map
private Container contentPane;
private Color backgroundColor = new Color(225, 225, 255);
private final int MENU_HEIGHT = 74; // Height of buttons toolbar
private final int BORDER = 9; // Border width
private final int INITIAL_RESOLUTION = 6;
private static final int FINAL_RESOLUTION = 9; // Full image resolution
private final int MAX_RESOLUTION = 11;
private final int MIN_RESOLUTION = 1;
private static final int CANVAS_SIZE = 512; // Size of window where images are displayed
private int windowWidth; // Dimensions of GUI window
private int windowHeight;
private int xcoord, ycoord; // Coordinates entered in the graphical user interface
private static int[][] pixelsMatrix; // Pixels of image to draw
private int resolution = INITIAL_RESOLUTION;
private DrawImage dim;
private static int imageSize; // Size of image being displayed
/* ============================================== */
public Gui(int width, int height, String fileName) {
/* ============================================== */
try {
windowWidth = width;
windowHeight = height;
pixelsMatrix = new int[CANVAS_SIZE][CANVAS_SIZE];
panel = new Board();
contentPane = getContentPane();
contentPane.add(panel);
setSize(width, height);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setVisible(true);
display = panel.getGraphics();
contentPane.setBackground(backgroundColor);
display.setColor(backgroundColor);
/* Create buttons and mouse event handlers */
buttonPanel = new JPanel();
buttons = new JButton[4];
buttons[0] = makeButton("++Res", backgroundColor);
buttons[1] = makeButton("--Res", backgroundColor);
buttons[3] = makeButton("Exit",backgroundColor);
buttons[2] = makeButton("Find",backgroundColor);
for (int i = 0; i < buttons.length; i++)
buttonPanel.add(buttons[i]);
//x = new JTextField("x: ",5);
//y = new JTextField("y: ",5);
//buttonPanel.add(x);
//buttonPanel.add(y);
buttonPanel.add(buttons[2]);
buttonPanel.add(buttons[3]);
/* Place buttons at the bottom of the Window */
contentPane.add(buttonPanel, BorderLayout.SOUTH);
for (int i = 0; i < buttons.length; ++i)
buttons[i].addActionListener(this);
//x.addActionListener(this);
//y.addActionListener(this);
contentPane.setFocusable(true);
contentPane.requestFocusInWindow();
System.out.println("Initializing ...");
Thread.sleep(2000);
resolution = INITIAL_RESOLUTION;
dim = new DrawImage(fileName, resolution);
contentPane.add(dim);
dim.addMouseListener(this);
setVisible(true);
System.out.println("Ready.");
} catch (Exception e) {
System.out.println("Error starting program: " + e.getMessage());
}
}
/* Returns true if the given color are similar; returns false otherwise */
public static boolean similarColor(int color1, int color2) {
int simred = 0xf << 16;
int simgreen = 0xf << 8;
int simblue = 15;
if ((Math.abs((color1 & 0xff0000) - (color2 & 0xff0000)) < simred) &&
(Math.abs((color1 & 0xff00) - (color2 & 0xff00)) < simgreen) &&
(Math.abs((color1 & 0xff) - (color2 & 0xff)) < simblue)) return true;
else return false;
}
public static int[][] getPixelsMatrix() {
return pixelsMatrix;
}
/* Computes the average color of the square region of the image
"pixels" with upper left corner at (x,y) and size given by
the last parameter. */
public static int averageColor(int[][] pixels, int x, int y, int size) {
int red, green, blue;
red = green = blue = 0;
for (int i = x; i < x + size; ++i)
for (int j = y; j < y + size; ++j) {
// Compute red, green, and blue component of each pixel in the square region
red = red + ((pixels[j][i] >> 16) & 0xFF);
green = green + ((pixels[j][i] >> 8) & 0xFF);
blue = blue + (pixels[j][i] & 0xFF);
}
// Average the components and convert them back to a color
int n = size * size;
red = red / n;
green = green / n;
blue = blue / n;
return ((red & 0xFF) << 16) + ((green & 0xFF) << 8) + (blue & 0xFF);
}
/* Stores the pixels represented by r in matrixPixels so the image can be
displayed */
public static void storePixel (QTreeNode r) {
int x = r.getx();
int y = r.gety();
int size = 0, i, j;
int deltax = 0, deltay = 0, scale = 1;
QTreeNode startingNode = DrawImage.startingNode;
QTreeNode root = DrawImage.imageTree.getRoot();
imageSize = DrawImage.size;
try {
if (imageSize == CANVAS_SIZE) {
size = r.getSize();
for (i = x; i < x + size; ++i)
for (j = y; j < y + size; ++j)
pixelsMatrix[j][i] = r.getColor();
return;
}
deltax = startingNode.getx();
deltay = startingNode.gety();
x = x - deltax; // Translate the coordinates of the node with respect
// to the starting node
y = y - deltay;
if (imageSize == 2*CANVAS_SIZE) {
if (startingNode != root)
pixelsMatrix[y][x] = r.getColor();
else {
scale = 2;
x = x/scale;
y = y/scale;
size = r.getSize()/scale;
for (i = x; i < x + size; ++i)
for (j = y; j < y + size; ++j)
pixelsMatrix[j][i] = r.getColor();
}
}
else {
if (getLevel(startingNode) == 2)
pixelsMatrix[y][x] = r.getColor();
else if (startingNode == root)
scale = 4;
else if (startingNode.getParent() == root)
scale = 2;
x = x/scale;
y = y/scale;
size = r.getSize()/scale;
//pixelsMatrix[y/scale][x/scale] = r.getColor();
for (i = x; i < x + size; ++i)
for (j = y; j < y + size; ++j)
pixelsMatrix[j][i] = r.getColor();
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
}
/* Mark the pixels in the list stored in head */
public static void markPixels(Duple head, int resolution) {
QTreeNode r;
int deltax = 0, deltay = 0, scale = 1;
int x, y, size = 0;
QTreeNode startingNode = DrawImage.startingNode;
QTreeNode root = DrawImage.imageTree.getRoot();
imageSize = DrawImage.size;
if (DrawImage.size == 2*CANVAS_SIZE) scale = 2;
else if (DrawImage.size == 4*CANVAS_SIZE) scale = 4;
ListNode<QTreeNode> p = head.getFront();
while (p != null) {
r = (QTreeNode)p.getData();
x = r.getx();
y = r.gety();
if (imageSize == CANVAS_SIZE) {
size = r.getSize();
for (int i = x; i < x + size; ++i)
for (int j = y; j < y + size; ++j)
pixelsMatrix[j][i] = 0xff0000;
}
else {
deltax = startingNode.getx();
deltay = startingNode.gety();
x = x - deltax; // Translate the coordinates of the node with respect
// to the starting node
y = y - deltay;
if (imageSize == 2*CANVAS_SIZE) {
if (startingNode != root) {
pixelsMatrix[y][x] = 0xff0000;
}
else {
scale = 2;
x = x/scale;
y = y/scale;
size = r.getSize()/scale;
for (int i = x; i < x + size; ++i)
for (int j = y; j < y + size; ++j)
pixelsMatrix[j][i] = 0xff0000;
}
}
else {
if (getLevel(startingNode) == 2)
pixelsMatrix[y][x] = 0xff0000;
else if (startingNode == root)
scale = 4;
else if (startingNode.getParent() == root)
scale = 2;
x = x/scale;
y = y/scale;
size = r.getSize()/scale;
//pixelsMatrix[y/scale][x/scale] = r.getColor();
for (int i = x; i < x + size; ++i)
for (int j = y; j < y + size; ++j)
pixelsMatrix[j][i] = 0xff0000;
}
}
p = p.getNext();
}
}
/* Compute the level of the node r */
/* ========================================*/
private static int getLevel(QTreeNode r) {
/* ========================================*/
int level = 0;
while (r.getParent() != null) {
r = r.getParent();
++level;
}
return level;
}
/* ==================== */
public int displayWidth() {
/* ==================== */
return windowWidth - BORDER;
}
/* ==================== */
public int displayHeight() {
/* ==================== */
return windowHeight - MENU_HEIGHT;
}
/* =================================================== */
public Graphics getDisplay() {
/* =================================================== */
return display;
}
/* =================================================== */
private JButton makeButton(String label, Color color)
/* =================================================== */
{
JButton b = new JButton(label);
b.setBackground(color);
return b;
}
/* ======================================= */
public void actionPerformed(ActionEvent e)
/* ======================================= */
{
int scale = 1, deltax = 0, deltay = 0;
String s = (String) e.getActionCommand();
int imgSize = DrawImage.size;
if (s.equals("Find")) {
if ((imgSize > CANVAS_SIZE) && (resolution > FINAL_RESOLUTION)) {
deltax = DrawImage.startingNode.getx();
deltay = DrawImage.startingNode.gety();
}
if ((imgSize == 2*CANVAS_SIZE) && (resolution <= FINAL_RESOLUTION)) scale = 2;
else if (imgSize == 4*CANVAS_SIZE)
if (resolution <= FINAL_RESOLUTION) scale = 4;
else if (resolution == (FINAL_RESOLUTION + 1)) scale = 2;
xcoord = (xcoord * scale) + deltax;
ycoord = (ycoord * scale) + deltay;
contentPane.remove(dim);
dim = new DrawImage(resolution,xcoord,ycoord,true);
contentPane.add(dim);
dim.addMouseListener(this);
setVisible(true);
} else if (s.equals("++Res")) {
if (((imgSize <= CANVAS_SIZE) && (resolution < FINAL_RESOLUTION)) ||
((imgSize == 2*CANVAS_SIZE) && (resolution < FINAL_RESOLUTION + 1)) ||
((imgSize == 4*CANVAS_SIZE) && (resolution < MAX_RESOLUTION))) {
++resolution;
if (imgSize == 2*CANVAS_SIZE) scale = 2;
else if (imgSize == 4*CANVAS_SIZE) {
if (resolution == FINAL_RESOLUTION + 1) scale = 4;
else {
deltax = DrawImage.startingNode.getx();
deltay = DrawImage.startingNode.gety();
scale = 2;
}
}
xcoord = xcoord * scale + deltax;
ycoord = ycoord * scale + deltay;
if (xcoord >= imgSize || ycoord >= imgSize) {
xcoord = 0;
ycoord = 0;
}
contentPane.remove(dim);
dim = new DrawImage(resolution,xcoord,ycoord);
contentPane.add(dim);
dim.addMouseListener(this);
setVisible(true);
}
} else if (s.equals("--Res")) {
if (resolution >= MIN_RESOLUTION) {
if ((imgSize == 4*CANVAS_SIZE) && (resolution == MAX_RESOLUTION)) --resolution;
--resolution;
if (xcoord == 0 && ycoord == 0) xcoord = ycoord = imgSize / 2;
contentPane.remove(dim);
dim = new DrawImage(resolution,xcoord,ycoord);
contentPane.add(dim);
dim.addMouseListener(this);
setVisible(true);
}
}else if (s.equals("Exit")) {
dispose();
System.exit(0);
}
contentPane.requestFocusInWindow();
}
public void mouseClicked(MouseEvent e) {
xcoord = e.getX();
ycoord = e.getY();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
/* ====================================== */
public static void main(String args[]) {
/* ====================================== */
Gui window;
if (args.length == 1)
window = new Gui(530, 590, args[0]); // Set up drawing environment;
else if (args.length == 2) ;
}
}