我正在尝试创建类似程序的绘画,我正在实现一个桶填充工具。我正在存储已经绘制的所有点并使用Graphics2D的drawLine
来绘制实际的线,所以我不想存储桶填充的所有点(所以我不想进行填充)。
对于水桶填充,到目前为止,我使用BufferedImage
填写了不在我的列表中但仍在绘制的点。
我认为要做的一件事就是只存储最外面的点,而不是使用这些点来使用Graphics2D的fillPolygon
。唯一的问题是我不确定如何找到这些点。
我被困在这里,所有人都有任何想法吗?
答案 0 :(得分:6)
可能有很多不同的实现方法,个人而言,我会使用2D图形形状API ......
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Path2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class BucketFill {
public static void main(String[] args) {
new BucketFill();
}
public BucketFill() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<Point> points;
public TestPane() {
points = new ArrayList<Point>(25);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (points.size() > 0) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
List<Point> proxy = new ArrayList<>(points);
Path2D.Double path = new Path2D.Double();
Point p = proxy.remove(0);
path.moveTo(p.getX(), p.getY());
while (proxy.size() > 0) {
p = proxy.remove(0);
path.lineTo(p.getX(), p.getY());
}
g2d.setColor(Color.RED);
g2d.fill(path);
g2d.setColor(Color.BLACK);
g2d.draw(path);
g2d.dispose();
}
}
}
}
使用fillPolygon更新
您可以使用Shape
替换fillPolygon
实现,只需删除对形状的引用,然后使用以下内容。
List<Point> proxy = new ArrayList<>(points);
int[] xPoints = new int[proxy.size()];
int[] yPoints = new int[proxy.size()];
int nPoints = proxy.size();
int index = 0;
while (proxy.size() > 0) {
Point p = proxy.remove(0);
xPoints[index] = p.x;
yPoints[index] = p.y;
index++;
}
g2d.setColor(Color.RED);
g2d.fillPolygon(xPoints, yPoints, nPoints);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xPoints, yPoints, nPoints);
这将生成所谓的闭合多边形(即,您可以看到所有线都连接起来)。
您可以在Shape
之后和之前调用path.closePath()
来使用while-loop
。
更新了多个多边形
很多内容将归结为您认为多边形以及如何存储这些值,但您可以使用Area
并从中减去交叉多边形...
public class BucketFill {
public static void main(String[] args) {
new BucketFill();
}
public BucketFill() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<List<Point>> points;
public TestPane() {
points = new ArrayList<>(25);
MouseAdapter ma = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
List<Point> newShape = new ArrayList<>(25);
newShape.add(e.getPoint());
points.add(newShape);
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
List<Point> newShape = points.get(points.size() - 1);
newShape.add(e.getPoint());
repaint();
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (points.size() > 0) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setColor(Color.BLUE);
List<Shape> shapes = new ArrayList<>(25);
for (List<Point> subPoints : points) {
if (subPoints.size() > 0) {
List<Point> proxy = new ArrayList<>(subPoints);
Path2D path = new Path2D.Float();
Point startPoint = proxy.remove(0);
path.moveTo(startPoint.x, startPoint.y);
for (Point p : proxy) {
path.lineTo(p.x, p.y);
}
path.closePath();
shapes.add(path);
path = null;
}
}
for (Shape master : shapes) {
Area area = new Area(master);
for (Shape inner : shapes) {
if (inner != master) {
area.subtract(new Area(inner));
}
}
g2d.setColor(Color.RED);
g2d.fill(area);
g2d.setColor(Color.BLACK);
g2d.draw(area);
}
g2d.dispose();
}
}
}
}