首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java 8+ Swing:如何绘制平齐多边形

Java 8+ Swing:如何绘制平齐多边形
EN

Stack Overflow用户
提问于 2014-10-24 15:59:14
回答 1查看 799关注 0票数 3

(很抱歉这么长的邮筒.至少它有照片?)

我已经写了一个算法,通过统计生成N个凸多边形来创建一个马赛克图像,该多边形覆盖图像,没有重叠。这些多边形有3-8个边,每个边的角度是45度的倍数。这些多边形在内部存储为一个矩形,每个角都有位移。下面的图片解释了这是如何工作的:

getRight()返回x + width - 1getBottom()返回y + height - 1。这个类的设计是为了在填充的像素周围保持一个紧密的包围框,所以这个图像中显示的坐标是正确的。请注意,width >= ul + ur + 1width >= ll + lr + 1height >= ul + ll + 1height >= ur + ul + 1,否则一侧将出现空像素。还请注意,角的位移可能为0,因此指示所有像素都填充在该角。这使得这个表示可以存储3-8个边凸多边形,每个边至少有一个像素长。

虽然用数学来表示这些区域很好,但我想画它们,这样我就能看到它们。使用简单的lambda和对多边形中的每个像素进行迭代的方法,我可以完美地呈现图像。作为一个例子,下面是Claude Monet's 使用99个多边形,允许所有的分裂方向。

呈现此图像的代码如下所示:

代码语言:javascript
复制
public void drawOnto(Graphics graphics) {
    graphics.setColor(getColor());
    forEach(
        (i, j) -> {
            graphics.fillRect(x + i, y + j, 1, 1);
        }
    );
}

private void forEach(PerPixel algorithm) {
    for (int j = 0; j < height; ++j) {
        int nj = height - 1 - j;

        int minX;
        if (j < ul) {
            minX = ul - j;
        } else if (nj < ll) {
            minX = ll - nj;
        } else {
            minX = 0;
        }

        int maxX = width;
        if (j < ur) {
            maxX -= ur - j;
        } else if (nj < lr) {
            maxX -= lr - nj;
        }

        for (int i = minX; i < maxX; ++i) {
            algorithm.perform(i, j);
        }
    }
}

然而,由于许多原因,这并不理想。首先,图形表示多边形的概念现在是类本身的一部分;最好允许其他类的焦点是表示这些多边形。其次,这需要多次调用fillRect()来绘制一个像素。最后,我希望能够开发其他方法来呈现这些多边形,而不是按原样绘制它们(例如,performing weighted interpolation over the Voronoi tessellation represented by the polygons' centers)。

所有这些都指向生成一个java.awt.Polygon,它表示多边形的顶点(我将其命名为Region以区别于Polygon类)。没问题;我编写了一种方法来生成一个Polygon,该方法具有上面的拐角,没有副本来处理位移为0或一侧只有一个像素的情况:

代码语言:javascript
复制
public Polygon getPolygon() {
    int[] xes = {
        x + ul,
        getRight() - ur,
        getRight(),
        getRight(),
        getRight() - lr,
        x + ll,
        x,
        x
    };
    int[] yes = {
        y,
        y,
        y + ur,
        getBottom() - lr,
        getBottom(),
        getBottom(),
        getBottom() - ll,
        y + ul
    };

    int[] keptXes = new int[8];
    int[] keptYes = new int[8];
    int length = 0;
    for (int i = 0; i < 8; ++i) {
        if (
            length == 0 ||
            keptXes[length - 1] != xes[i] ||
            keptYes[length - 1] != yes[i]
        ) {
            keptXes[length] = xes[i];
            keptYes[length] = yes[i];
            length++;
        }
    }

    return new Polygon(keptXes, keptYes, length);
}

问题是,当我尝试在Polygon方法中使用这样的Graphics.fillPolygon()时,它并不能填充所有的像素!下面是用这种不同的方法呈现的相同的拼图:

关于这种行为,我有几个相关的问题:

  1. 为什么Polygon类不填充所有这些像素,即使角度是45度的简单倍数?
  2. 如何在呈现器中始终围绕这个缺陷(就我的应用程序而言)进行编码,以便我可以按原样使用我的getPolygon()方法?我不想改变它输出的顶点,因为我需要它们精确地进行质量中心的计算。

MCE

如果上面的代码片段和图片不足以帮助解释问题,我添加了一个最小的、完整的和可验证的示例,演示了我前面描述的行为。

代码语言:javascript
复制
package com.sadakatsu.mce;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    @FunctionalInterface
    private static interface PerPixel {
        void perform(int x, int y);
    }

    private static class Region {
        private int height;
        private int ll;
        private int lr;
        private int width;
        private int ul;
        private int ur;
        private int x;
        private int y;

        public Region(
            int x,
            int y,
            int width,
            int height,
            int ul,
            int ur,
            int ll,
            int lr
        ) {
            if (
                width < 0 || width <= ll + lr || width <= ul + ur ||
                height < 0 || height <= ul + ll || height <= ur + lr ||
                ul < 0 ||
                ur < 0 ||
                ll < 0 ||
                lr < 0
            ) {
                throw new IllegalArgumentException();
            }

            this.height = height;
            this.ll = ll;
            this.lr = lr;
            this.width = width;
            this.ul = ul;
            this.ur = ur;
            this.x = x;
            this.y = y;
        }

        public Color getColor() {
            return Color.BLACK;
        }

        public int getBottom() {
            return y + height - 1;
        }

        public int getRight() {
            return x + width - 1;
        }

        public Polygon getPolygon() {
            int[] xes = {
                x + ul,
                getRight() - ur,
                getRight(),
                getRight(),
                getRight() - lr,
                x + ll,
                x,
                x
            };
            int[] yes = {
                y,
                y,
                y + ur,
                getBottom() - lr,
                getBottom(),
                getBottom(),
                getBottom() - ll,
                y + ul
            };

            int[] keptXes = new int[8];
            int[] keptYes = new int[8];
            int length = 0;
            for (int i = 0; i < 8; ++i) {
                if (
                    length == 0 ||
                    keptXes[length - 1] != xes[i] ||
                    keptYes[length - 1] != yes[i]
                ) {
                    keptXes[length] = xes[i];
                    keptYes[length] = yes[i];
                    length++;
                }
            }

            return new Polygon(keptXes, keptYes, length);
        }

        public void drawOnto(Graphics graphics) {
            graphics.setColor(getColor());
            forEach(
                (i, j) -> {
                    graphics.fillRect(x + i, y + j, 1, 1);
                }
            );
        }

        private void forEach(PerPixel algorithm) {
            for (int j = 0; j < height; ++j) {
                int nj = height - 1 - j;

                int minX;
                if (j < ul) {
                    minX = ul - j;
                } else if (nj < ll) {
                    minX = ll - nj;
                } else {
                    minX = 0;
                }

                int maxX = width;
                if (j < ur) {
                    maxX -= ur - j;
                } else if (nj < lr) {
                    maxX -= lr - nj;
                }

                for (int i = minX; i < maxX; ++i) {
                    algorithm.perform(i, j);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        int width = 10;
        int height = 8;

        Region region = new Region(0, 0, 10, 8, 2, 3, 4, 1);

        BufferedImage image = new BufferedImage(
            width,
            height,
            BufferedImage.TYPE_3BYTE_BGR
        );
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, width, height);
        region.drawOnto(graphics);
        ImageIO.write(image, "PNG", new File("expected.png"));

        image = new BufferedImage(
            width,
            height,
            BufferedImage.TYPE_3BYTE_BGR
        );
        graphics = image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, width, height);
        graphics.setColor(Color.BLACK);
        graphics.fillPolygon(region.getPolygon());
        ImageIO.write(image, "PNG", new File("got.png"));
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-24 23:24:46

我花了一整天的时间在这上面工作,而且我似乎对此有一个解决办法。在Shape类的文档中找到了这条线索,该类的内容如下:

定义的不均匀性:点被认为位于形状内当且仅当:

  • 它完全位于theShape边界内或
  • 它精确地位于形状边界上,并且紧邻点的X方向上的空间完全在边界内,或者
  • 它准确地位于水平边界段上,并且在边界内紧邻Y方向上的点的空间。

实际上,本文有点误导;第三种情况覆盖了第二种情况(即,即使Shape底部的水平边界段中的像素在其右侧有一个填充点,它仍然不会被填充)。以图片表示,下面的Polygon将不绘制x‘’ed像素:

红色、绿色和蓝色像素是Polygon的一部分,其余的则不是。蓝色像素落在第一情况下,绿色像素落在第二情况下,红色像素落在第三情况下。请注意,沿凸包的所有最右边和最低像素都不是绘制的。要绘制它们,必须将顶点移动到橙色像素上,如图所示,使凸包的一个新的最右边/最底层的部分。

最简单的方法是使用camickr的方法:同时使用fillPolygon()drawPolygon()。至少在我的45度-多边凸壳的情况下,drawPolygon()精确地绘制到顶点的线(可能也适用于其他情况),从而填充fillPolygon()遗漏的像素。但是,fillPolygon()drawPolygon()都不会绘制单像素的Polygon,因此必须编写一个特殊情况来处理这个问题。

我在试图理解上面的不一致性定义时开发的实际解决方案是创建一个不同的Polygon,其中有修改过的角,如图所示。它有好处(?)只调用绘图库一次,并自动处理特例。它可能实际上并不是最优的,但下面是我所使用的供任何人考虑的代码:

代码语言:javascript
复制
package com.sadakatsu.mosaic.renderer;

import java.awt.Polygon;
import java.util.Arrays;

import com.sadakatsu.mosaic.Region;

public class RegionPolygon extends Polygon {
    public RegionPolygon(Region region) {
        int bottom = region.getBottom();
        int ll = region.getLL();
        int lr = region.getLR();
        int right = region.getRight();
        int ul = region.getUL();
        int ur = region.getUR();
        int x = region.getX();
        int y = region.getY();

        int[] xes = {
            x + ul,
            right - ur + 1,
            right + 1,
            right + 1,
            right - lr,
            x + ll + 1,
            x,
            x
        };

        int[] yes = {
            y,
            y,
            y + ur,
            bottom - lr,
            bottom + 1,
            bottom + 1,
            bottom - ll,
            y + ul
        };

        npoints = 0;
        xpoints = new int[xes.length];
        ypoints = new int[xes.length];
        for (int i = 0; i < xes.length; ++i) {
            if (
                i == 0 ||
                xpoints[npoints - 1] != xes[i] ||
                ypoints[npoints - 1] != yes[i]
            ) {
                addPoint(xes[i], yes[i]);
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26551714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档