Java Reference
In-Depth Information
this.addMouseListener(mouseAdapter);
}
public void paint(Graphics g) {
int w = this.getWidth();
int h = this.getHeight();
g.setColor(Color.white);
g.fillRect(0, 0, w, h);
g.setColor(Color.black);
for (Point point : blackPixels)
g.drawRect(point.x, point.y, 1, 1);
}
public void clear() {
this.blackPixels.clear();
this.invalidate();
this.repaint();
}
public void addPixels(Collection<? extends Point> blackPixels) {
this.blackPixels.addAll(blackPixels);
this.invalidate();
this.repaint();
}
public void removePixels(Collection<? extends Point> blackPixels) {
this.blackPixels.removeAll(blackPixels);
this.invalidate();
this.repaint();
}
public boolean isPixel(Point blackPixel) {
return this.blackPixels.contains(blackPixel);
}
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;
}
How It Works
This is how it works:
1.
There is a lot going on in this small project. Start by taking a look at the SimplePaint class. The
JFrame is set up just like before; a SimplePaintPanel component is added to the content pane,
and the initMenu() is called to initialize the menu bar.
2.
The initMenu() shows you how you can set up menus with Swing. Note that JFrame has a dedi-
cated area to place menu bars, which you can set and get using the setJMenuBar and getJMenuBar
methods. Here, a single File menu entry was created, and three menu items were placed under it.
Search WWH ::




Custom Search