0

我正在尝试创建一个类似油漆的程序,我目前正在实施一个桶填充工具。我正在存储已绘制的所有点并使用 Graphics2DdrawLine绘制实际线,因此我不想存储桶填充的所有点(所以我不想进行洪水填充)。

对于桶填充,到目前为止,我已经使用 aBufferedImage来填充不在我的列表中但仍在绘制的点。

我想做的一件事是只存储最外层的点,然后我可以使用 Graphics2DfillPolygon使用这些点。唯一的问题是我不确定如何找到这些点。

我被困在这里,所以有人有什么想法吗?

4

1 回答 1

6

可能有很多不同的方法可以实现这一点,就个人而言,我会使用 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);

这将生成所谓的封闭多边形(即,您可以看到所有线都连接在一起)。

在此处输入图像描述

您可以通过在绘制之后和之前Shape调用来实现使用。path.closePath()while-loop

更新了多个多边形

在此处输入图像描述

很多都取决于您认为的多边形以及如何存储这些值,但是您可以使用 anArea并从中减去相交的多边形...

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();

            }
        }
    }
}
于 2013-02-27T02:58:23.237 回答