Pergunta

I am trying to implement boundary fill algorithm in Java as a part of my assignment. I am getting a stackoverflow error. Here's the code...

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);

        }

    }
}

And here's the calling class

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

I have tried to debug it and noticed that Robot class always gives the same color(r=16,g=16,b=32) ..even if it reaches the boundary of the polygon(triangle) Is there a more efficient way to do this? What's wrong with this code?

Foi útil?

Solução

Note that robot.getPixelColor(x, y); refers to screen coordinates - but you hardly ever know where on the screen your applet is shown! So the coordinates (400,400) do not make any sense.

As Rainer Schwarze suggested in one of the comments, the algorithm should probably be applied to a BufferedImage.

Outras dicas

You're going to have to rethink your fill method.
Assume x = 10
fill(10, ...) then calls fill(9,...) and fill(11,...)
Which in turn then call fill(8, ...), fill(10, ...), fill(10, ...) and fill(12, ...)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top