作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个类似绘画的程序,并且目前正在实现一个桶填充工具。我正在存储所有已绘制的点并使用 Graphics2D 的 drawLine
来绘制实际的线条,所以我不想存储桶填充的所有点(所以我不想做洪水充满)。
对于桶填充,到目前为止,我已经使用 BufferedImage
来填充不在我的列表中但仍在绘制的点。
我想做的一件事是只存储最外面的点,然后我可以使用这些点来使用 Graphics2D 的 fillPolygon
。唯一的问题是我不确定如何找到这些点。
我被困在这里,有人有什么想法吗?
最佳答案
可能有很多不同的方法可以实现这一点,就我个人而言,我会使用 2D Graphics Shape 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);
这将生成所谓的闭合多边形(即,您可以看到所有线条都连接在一起)。
您可以通过在 while-loop
之后、绘制之前调用 path.closePath()
来使用 Shape
。
更新了多个多边形
很大程度上取决于您对多边形的看法以及如何存储这些值,但您可以使用区域
并从中减去相交的多边形...
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();
}
}
}
}
关于java - 如何用Java创建一个桶填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15103161/
我是一名优秀的程序员,十分优秀!