-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayImage.java
More file actions
214 lines (188 loc) · 5.68 KB
/
DisplayImage.java
File metadata and controls
214 lines (188 loc) · 5.68 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
/**
*/
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
*/
public final class DisplayImage {
private static final int BW_THRESH = 128;
private static final int RED_THRESH = 200;
private static final int GREEN_BLUE_THRESH = 50;
private BufferedImage image; // the rasterized image
private JFrame frame; // on-screen view
private String filename; // name of file
private int width, height; // width and height
/**
* Creates a picture by reading in a .png, .gif, or .jpg from
* the given filename or URL name.
*
* @param filename image file name
*/
public DisplayImage (String filename) {
this.filename = filename;
try {
// try to read from file in working directory
File file = new File (filename);
image = ImageIO.read (file);
width = image.getWidth (null);
height = image.getHeight (null);
}
catch (IOException e) {
throw new RuntimeException ("Could not open file: " + filename);
}
// check that image was read in
if (image == null) {
throw new RuntimeException ("Invalid image file: " + filename);
}
}
public DisplayImage (int width, int height) {
this.width = width;
this.height = height;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
// check that image was read in
if (image == null) {
throw new RuntimeException ("Invalid image file: " + filename);
}
}
/**
* Returns a JLabel containing this Picture, for embedding in a JPanel,
* JFrame or other GUI widget.
*
* @return label
*/
private JLabel getJLabel() {
if (image == null) { // no image available
return null;
}
ImageIcon icon = new ImageIcon (image);
return new JLabel(icon);
}
/**
* Displays the picture in a window on the screen.
*/
public void show() {
// create the GUI for viewing the image if needed
if (frame == null) {
frame = new JFrame();
frame.setContentPane (getJLabel());
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setTitle (filename);
frame.setResizable (false);
frame.pack();
frame.setVisible (true);
}
// draw
frame.repaint();
}
/**
* Returns the height of the picture in pixels.
*
* @return height
*/
public int height() {
return height;
}
/**
* Returns the width of the picture in pixels.
*
* @return width
*/
public int width() {
return width;
}
/**
* Returns the color of pixel (i, j).
*
* @param x x coord
* @param y y coord
* @return color
*/
public Color get (int x, int y) {
return new Color (image.getRGB (x, y));
}
/**
* Sets the color of pixel (i, j) to c.
*
* @param x x coord
* @param y y coord
* @param c color
*/
public void set (int x, int y, Color c) {
if (c == null)
throw new IllegalArgumentException ("Can't set Color to null");
image.setRGB (x, y, c.getRGB());
}
/**
* Checks whether pixel x, y is on or off.
*
* @param x x coord
* @param y y coord
* @return true if pixel x, y is on, false if it is off.
*/
public boolean isOn (int x, int y) {
Color c = new Color (image.getRGB (x, y));
int intensity = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
return intensity < BW_THRESH;
}
/**
* Checks whether pixel x, y is on or off.
*
* @param x x coord
* @param y y coord
* @return true if pixel x, y is on, false if it is off.
*/
public boolean isRed (int x, int y) {
Color c = new Color (image.getRGB (x, y));
return c.getRed() > RED_THRESH && c.getGreen() < GREEN_BLUE_THRESH && c.getBlue() < GREEN_BLUE_THRESH;
}
/**
* Save the picture to a file in a standard image format.
*
* @param filename name of file to save to.
*/
public void save (String filename) {
this.filename = filename;
File file = new File (filename);
if (frame != null) { frame.setTitle(filename); }
String suffix = filename.substring (filename.lastIndexOf('.') + 1);
suffix = suffix.toLowerCase();
if (suffix.equals("jpg") || suffix.equals("png")) {
try {
ImageIO.write(image, suffix, file);
}
catch (IOException e) {
e.printStackTrace();
}
}
else {
throw new IllegalArgumentException ("Filename must end in .jpg or .png");
}
}
/**
* Test client. Reads a picture specified by the command-line argument,
* and shows it in a window on the screen.
*
* @param args command line arguments
*/
public static void main (String[] args) {
DisplayImage pic = new DisplayImage ("maze2.PNG");
int xMax = pic.width() - 1;
int yMax = pic.height() - 1;
for (int x = 0; x <= xMax; x++)
for (int y = 0; y <= yMax; y++) {
if(pic.isRed(x,y)) {
pic.set(x,y,Color.white);
}
}
pic.set(4,4,Color.red);
pic.set(pic.width()-4, pic.height()-4, Color.red);
pic.show();
pic.save("maze2black.PNG");
}
}