Java GUI 洪水填充错误

标签 java user-interface flood-fill

我一直在使用 Java GUI,但遇到了一个错误,我不知道为什么。

我的程序基于洪水填充(填充线条内的颜色),但是当我尝试填充时出现此错误:

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.util.Hashtable.get(Unknown Source)
at javax.swing.UIDefaults.getFromHashtable(Unknown Source)
at javax.swing.UIDefaults.get(Unknown Source)
at javax.swing.MultiUIDefaults.get(Unknown Source)
at javax.swing.UIDefaults.getColor(Unknown Source)
at javax.swing.UIManager.getColor(Unknown Source)
at javax.swing.LookAndFeel.installColors(Unknown Source)
at javax.swing.LookAndFeel.installColorsAndFont(Unknown Source)
at javax.swing.plaf.basic.BasicLabelUI.installDefaults(Unknown Source)
at javax.swing.plaf.basic.BasicLabelUI.installUI(Unknown Source)
at javax.swing.JComponent.setUI(Unknown Source)
at javax.swing.JLabel.setUI(Unknown Source)
at javax.swing.JLabel.updateUI(Unknown Source)
at javax.swing.JLabel.<init>(Unknown Source)
at javax.swing.JLabel.<init>(Unknown Source)
at MapPanel.refreshImage(MapPanel.java:167)
at MapPanel.setImage(MapPanel.java:162)
at MapPanel.floodFill(MapPanel.java:224)

[这些重复了很多]

at MapPanel.floodFill(MapPanel.java:231)
at MapPanel.floodFill(MapPanel.java:230)

[第 230 行和 231 行的代码]

floodFill(newImage, mark, row - 1, col, srcColor, tgtColor);
floodFill(newImage, mark, row + 1, col, srcColor, tgtColor);

图像已更新颜色,但并非全部...

enter image description here

我希望你明白我想说的,我已经被困在这个问题上有一段时间了,任何帮助都会非常感谢。

代码

// Mouse Listener
public class MouseCapture implements MouseListener
{
    @Override
    public void mouseEntered(MouseEvent e) {
    }
    @Override
    public void mouseExited(MouseEvent e) {
    }
    @Override
    public void mousePressed(MouseEvent e) {
    }
    @Override
    public void mouseReleased(MouseEvent e) {
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("Mouse clicked");
        System.out.println(e.getX() + "," + e.getY());

        boolean[][] mark = new boolean[image.getHeight()][image.getWidth()];

        for (int row = e.getY() + 23; row < image.getHeight(); row++) {
            for (int col = e.getX() - 200; col < image.getWidth(); col++) {
                floodFill(image, mark, row, col, Color.WHITE, getColor());
            }
        }

        image.setRGB(e.getX(), e.getY() + 23, Color.BLACK.getRGB());
        refreshImage();

    }
}

private void floodFill(BufferedImage newImage, boolean[][] mark, int row, int col, Color srcColor, Color tgtColor)
{
    // make sure row and col are inside the image
    if (row < 0) return;
    if (col < 0) return;
    if (row >= newImage.getHeight()) return;
    if (col >= newImage.getWidth()) return;

    // make sure this pixel hasn't been visited yet
    if (mark[row][col]) return;

    // make sure this pixel is the right color to fill
    if (!(newImage.getRGB(col, row) == (srcColor.getRGB()))) return;

    // fill pixel with target color and mark it as visited
    image.setRGB(col, row, tgtColor.getRGB());
    mark[row][col] = true;

    // set drawn image
    setImage(newImage);

    // recursively fill surrounding pixels
    // (this is equivelant to depth-first search)
    floodFill(newImage, mark, row - 1, col, srcColor, tgtColor);
    floodFill(newImage, mark, row + 1, col, srcColor, tgtColor);
    floodFill(newImage, mark, row, col - 1, srcColor, tgtColor);
    floodFill(newImage, mark, row, col + 1, srcColor, tgtColor);
}

最佳答案

如果您在使用“Flood fill”时遇到stackOverflowError,则意味着您尝试绘制的多边形对于该算法的递归版本来说太大。尝试使用带有队列或堆栈实现的版本。

import java.util.*;

public class Main {

    public static class Position {
        private int y;
        private int x;

        public Position(int y, int x) {
            this.y = y;
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public int getX() {
            return x;
        }
    }

    public static void floodFill(Position position, int oldColor, int newColor, int[][] image) {

        Queue<Position> queue = new LinkedList<Position>();
        queue.add(position);

        while (queue.size() > 0) {
            Position newPosition = queue.poll();

            if (newPosition.getY() >= 0 && newPosition.getY() < image.length // check bounds
                    && newPosition.getX() >= 0 && newPosition.getX() < image[0].length // check bounds
                    && image[newPosition.getY()][newPosition.getX()] == oldColor) {

                image[newPosition.getY()][newPosition.getX()] = newColor;

                queue.add(new Position(newPosition.getY(), newPosition.getX() - 1));
                queue.add(new Position(newPosition.getY(), newPosition.getX() + 1));
                queue.add(new Position(newPosition.getY() - 1, newPosition.getX()));
                queue.add(new Position(newPosition.getY() + 1, newPosition.getX()));
            }
        }
    }

    public static void main(String[] args) {

        int[][] array = new int[20][40]; // 20 rows, 40 columns

        for (int y = 0; y < array.length; y++) { // iterate through rows
            for (int x = 0; x < array[0].length; x++) // iterate through cols
                System.out.print(array[y][x]);
            System.out.println();
        }

        floodFill(new Position(5, 5), 0, 1, array);

        for (int y = 0; y < array.length; y++) { // iterate through rows
            for (int x = 0; x < array[0].length; x++) // iterate through cols
                System.out.print(array[y][x]);
            System.out.println();
        }
    }
}

您可以测试它是否有效。我们还可以在将位置添加到队列时检查位置,但我们的代码会不太透明。

关于Java GUI 洪水填充错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39803665/

相关文章:

java - 如何清除 Canvas 屏幕?

android - 实现特殊 UI 元素列表的最佳方式?

user-interface - 没有互联网连接时如何显示 snackbar ?

winforms - 实现二维单选按钮网格的最佳方法是什么?

ios - 对 UIImage 执行 floodfill

ios - Swift 中的颜色填充

Java 洪水填充问题

java - token ";"出现语法错误...为什么?

Java Applet GUI 组件问题

java - @size(max = value) 与 @min(value) 和 @max(value) 之间的区别