边界填充算法.java中的堆栈溢出错误

时间:2014-02-28 06:37:45

标签: 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();
        }
    }
}

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

2 个答案:

答案 0 :(得分:0)

你将不得不重新考虑你的填充方法 假设x = 10
fill(10,...)然后调用fill(9,...)和fill(11,...)
然后调用填充(8,...),填充(10,...)填充(10,...)并填充(12, ...)

答案 1 :(得分:0)

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

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