【问题标题】:Boundary Fill algorithm..Stackoverflow error in java边界填充算法..java中的Stackoverflow错误
【发布时间】:2014-03-03 00:29:40
【问题描述】:

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

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) ..即使它到达多边形的边界(三角形) 有没有更有效的方法来做到这一点? 这段代码有什么问题?

【问题讨论】:

  • 每个填充调用都使用一个新的Robot 实例,这很奇怪
  • 我更正了那个..谢谢!
  • 一定要使用Robot吗?或者您是否也可以使用您在其上绘制的图像,然后在小程序中绘制?
  • @Rainer Schwarze 这可能是这里的主要问题。 @surbhi 请注意,robot.getPixelColor(x, y); 指的是 屏幕 坐标 - 但您几乎不知道您的小程序在屏幕上的 何处 显示!所以坐标(400,400)没有任何意义。正如 Rainer 建议的那样,该算法可能应该应用于BufferedImage 左右。
  • 如果您正在寻找更有效的方法,请读取整个缓冲区一次并将其放入一个数组,然后处理该数组并将结果发送回后台缓冲区。每次从机器人读取一个像素并写入 1 个像素长度并不是一个好主意。另外我不认为递归函数是正确的方法。您浪费了太多尝试一次又一次地在相同像素上写入的调用。而且你很可能会因为大区域的深度太深而导致堆栈溢出

标签: java graphics flood-fill


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 应该已经被包围递归调用的 if 语句 if (...! currentPixelColor.equals(fillColor)) 覆盖
    猜你喜欢
    • 2015-04-04
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 2014-06-14
    • 2013-03-18
    相关资源
    最近更新 更多