1 import java.awt.Color;
2 import java.io.File;
3 import java.net.URL;
4 import java.awt.image.BufferedImage;
5 import java.awt.image.ColorModel;
6 import java.awt.image.Raster;
7 import java.awt.image.WritableRaster;
8 import javax.imageio.ImageIO;
9 import javax.swing.ImageIcon;
10 import javax.swing.JFileChooser;
11 import javax.swing.JFrame;
12 import javax.swing.JLabel;
13
14 /**
15 A picture whose pixels can be read and written.
16 */
17 public class Picture
18 {
19 private String source;
20 private JFrame frame;
21 private JLabel label;
22 private BufferedImage image;
23
24 /**
25 Constructs a picture with no image.
26 */
27 public Picture()
28 {
29 frame = new JFrame();
30 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
31 label = new JLabel("(No image)");
32 frame.add(label);
33 frame.pack();
34 frame.setVisible(true);
35 }
36
37 /**
38 Gets the width of this picture.
39 @return the width
40 */
41 public int getWidth() { return image.getWidth(); }
42
43 /**
44 Gets the height of this picture.
45 @return the height
46 */
47 public int getHeight() { return image.getHeight(); }
48
49 /**
50 Loads a picture from a given source.
51 @param source the image source. If the source starts
52 with http://, it is a URL, otherwise, a filename.
53 */
54 public void load(String source)
55 {
56 try
57 {
58 this.source = source;
59 BufferedImage img;
60 if (source.startsWith("http://"))
61 img = ImageIO.read(new URL(source).openStream());
62 else
63 img = ImageIO.read(new File(source));
64
65 setImage(img);
66 }
67 catch (Exception ex)
68 {
69 this.source = null;
70 ex.printStackTrace();
71 }
72 }
73
74 /**
75 Reloads this picture, undoing any manipulations.
76 */
77 public void reload()
78 {
79 load(source);
80 }
81
82 /**
83 Displays a file chooser for picking a picture.
84 */
85 public void pick()
86 {
87 JFileChooser chooser = new JFileChooser(".");
88 if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
89 {
90 load(chooser.getSelectedFile().getAbsolutePath());
91 }
92 }
93
94 private void setImage(BufferedImage image)
95 {
96 this.image = image;
97 label.setIcon(new ImageIcon(image));
98 label.setText(" ");
99 frame.pack();
100 }
101
102 /**
103 Gets the color of a pixel.
104 @param x the column index (between 0 and getWidth() - 1)
105 @param y the row index (between 0 and getHeight() - 1)
106 @return the color of the pixel at position (x, y)
107 */
108 public Color getColorAt(int x, int y)
109 {
110 Raster raster = image.getRaster();
111 ColorModel model = image.getColorModel();
112 int argb = model.getRGB(raster.getDataElements(x, y, null));
113 return new Color(argb, true);
114 }
115
116 /**
117 Sets the color of a pixel.
118 @param x the column index (between 0 and getWidth() - 1)
119 @param y the row index (between 0 and getHeight() - 1)
120 @param c the color for the pixel at position (x, y)
121 */
122 public void setColorAt(int x, int y, Color c)
123 {
124 WritableRaster raster = image.getRaster();
125 ColorModel model = image.getColorModel();
126 Object colorData = model.getDataElements(c.getRGB(), null);
127 raster.setDataElements(x, y, colorData);
128 label.repaint();
129 }
130 }