java - 边界填充算法..java中的Stackoverflow错误

标签 java graphics flood-fill

作为我作业的一部分,我正在尝试用 Java 实现边界填充算法。 我收到一个 stackoverflow 错误。这是代码...

package fillAlgorithms;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Robot;

public class BoundaryFillAlgorithm implements FillAlgorithm {
    public void fill(int x, int y, Graphics g, Color fillColor,
            Color boundaryColor) throws AWTException {
        Robot robot = new Robot();
        // reads the pixel value of pixel at x,y
        Color currentPixelColor = robot.getPixelColor(x, y);
        // if pixel is neither boundary color nor fill color
        // then fills the color
        if (!currentPixelColor.equals(boundaryColor)
                && !currentPixelColor.equals(fillColor)) {
            g.setColor(fillColor);
            g.drawLine(x, y, x, y);
            // recursive call
            fill(x + 1, y, g, fillColor, boundaryColor);
            fill(x - 1, y, g, fillColor, boundaryColor);
            fill(x, y + 1, g, fillColor, boundaryColor);
            fill(x, y - 1, g, fillColor, boundaryColor);

        }

    }
}

这是调用类

import fillAlgorithms.BoundaryFillAlgorithm;
import graphics.Point;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JApplet;

import shapes.Polygon;

@SuppressWarnings("serial")
public class FillApplet extends JApplet {
    @Override
    public void paint(Graphics g) {
        try {
            // Center of the coordinate system
            Point coordinateCenter = new Point(400, 400);
            Color black = new Color(0, 0, 0);
            Color red = new Color(255, 0, 0);
            Color white = new Color(255, 255, 255);
            g.setColor(red);
                    // filled applet with red color 
            g.fillRect(0, 0, 1000, 1000);
            Point vertices[] = new Point[3];
            // These vertices are with respect to the center of coordinate
            // center defined above
            vertices[0] = new Point(-5, 5);
            vertices[1] = new Point(5, 0);
            vertices[2] = new Point(0, -5);
            // Polygon class contains methods to draw polygons
            // This constructor accepts the vertices in the correct order and
            // the color of polygon
            // Fill color may be different from this color
            Polygon polygon = new Polygon(vertices, black);
            // Draw method draws the polygon after translating them into the
            // standard coordinate system of
            // having 0,0 in the top left corner
            polygon.draw(g, coordinateCenter);
            BoundaryFillAlgorithm algo = new BoundaryFillAlgorithm();
            algo.fill(400, 400, g, black, black);

        } catch (AWTException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我尝试调试它并注意到机器人类总是给出相同的颜色(r = 16,g = 16,b = 32)..即使它到达多边形(三角形)的边界 有没有更有效的方法来做到这一点? 这段代码有什么问题?

最佳答案

请注意,robot.getPixelColor(x, y); 指的是屏幕坐标 - 但您几乎不知道您的小程序在屏幕上的哪个位置显示!所以坐标(400,400)没有任何意义。

正如 Rainer Schwarze 在其中一条评论中建议的那样,该算法可能应该应用于 BufferedImage。

关于java - 边界填充算法..java中的Stackoverflow错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22087998/

相关文章:

c - C 洪水填充程序中的 "Shadow"

algorithm - 部分洪水填充

c# - 在 C# win Form 中创建维恩图

java - Android 过渡预览

java - Gradle 构建问题 : upgrading Quickblox and httpclient on latest Android SDK23

java - Cloud Firestore 查询 - 您只能对 null 执行相等比较

java - 使用 BufferedImage 生成缩略图,无需反转颜色并在 Java 中获得 alpha

java - 消除滚动文本的闪烁/抖动

Java 洪水填充问题

java - java不同类别的假文件路径