Java Reference
In-Depth Information
try (FileInputStream fis = new FileInputStream(openFile)) {
image = ImageIO.read(fis);
} catch (IOException e) {
return;
}
if (image == null)
return;
paintPanel.clear();
Set<Point> blackPixels = new HashSet<Point>();
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
Color c = new Color(image.getRGB(x, y));
if ((c.getBlue() < 128 || c.getRed() < 128 || c.getGreen() < 128)
&& c.getAlpha() == 255) {
blackPixels.add(new Point(x, y));
}
}
}
paintPanel.addPixels(blackPixels);
}
9.
Try loading the project again and loading an image, as shown in Figure 11-24.
figure 11-24  
10. Finally, you can add the ability to save images:
@Override
public void actionPerformed(ActionEvent ev) {
switch (ev.getActionCommand()) {
case ACTION_NEW:
paintPanel.clear();
Search WWH ::




Custom Search