Java Reference
In-Depth Information
this.blackPixels.removeAll(blackPixels);
this.invalidate();
this.repaint();
}
public boolean isPixel(Point blackPixel) {
return this.blackPixels.contains(blackPixel);
}
// Change this method:
private Collection<? extends Point> getPixelsAround(Point point) {
Set<Point> points = new HashSet<>();
for (int x = point.x - brushSize; x < point.x + brushSize; x++)
for (int y = point.y - brushSize; y < point.y + brushSize; y++)
points.add(new Point(x, y));
return points;
}
7.
Making the New menu work then becomes a simple manner of filling up the actionPerformed
method in the SimplePaint class like so:
@Override
public void actionPerformed(ActionEvent ev) {
switch (ev.getActionCommand()) {
case ACTION_NEW:
paintPanel.clear();
break;
}
}
8.
Next up is the ability to load images. Some tricks are used to convert everything to black and
white, but you could theoretically show the image as is if you want to (try this out later by yourself
if you want to take up the challenge):
@Override
public void actionPerformed(ActionEvent ev) {
switch (ev.getActionCommand()) {
case ACTION_NEW:
paintPanel.clear();
break;
case ACTION_LOAD:
doLoadImage();
break;
}
}
private void doLoadImage() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(this);
if (result != JFileChooser.APPROVE_OPTION)
return;
BufferedImage image;
File openFile = fileChooser.getSelectedFile();
Search WWH ::




Custom Search