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