0

I want to fill an applet with blue color. I am doing this using paint(Graphics g) method. While filling the applet, i get StackOverFlowException after 8-10 seconds. I want to get rid of it. Please suggest me what to do or correct me if i am doing wrong. I asked someone about this, he said it store locations and removing them later, so that stack is always nearly empty. Please help me.

Code:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

public class Flooding extends Applet
{
    boolean[][] mark;
    boolean justOnce = true;

    @Override
    public void init()
    {
        mark = new boolean[800][600];

        this.setSize(100, 500);
        this.setPreferredSize(new Dimension(100, 500));
        this.setVisible(true);
    }

    @Override
    public void start()
    {
        setBackground(Color.WHITE);
        setForeground(Color.BLACK);
    }

    @Override
    public void paint(Graphics g)
    {
        if(justOnce)
        {
            super.paint(g);
            justOnce = false;
        }

        for (int row=0; row<500; row++)
        {
            for (int col=0; col<100; col++) 
            {
                flood(mark, row, col, g);
            }
        }
    }

    private static void sleep(int msec)
    {
        try 
        {
            Thread.currentThread().sleep(msec);
        }
        catch (InterruptedException e) { }
    }

    public void flood( boolean[][] mark, int row, int col, Graphics g)
    {
        if (row < 0) return;
        if (col < 0) return;
        if (row > 100) return;
        if (col > 500) return;

        if (mark[row][col]) return;

        g.setColor(Color.BLUE);
        g.drawLine(row, col, row, col);

        mark[row][col] = true;

        repaint();
        sleep(1);

        flood(mark, row - 1, col, g);
        flood(mark, row, col, g);
        flood(mark, row + 1, col, g);
        flood(mark, row-1, col-1, g);
        flood(mark, row, col-1, g);
        flood(mark, row+1, col-1, g);
        flood(mark, row-1, col + 1, g);
        flood(mark, row, col + 1, g);
        flood(mark, row+1, col + 1, g);
    }
}
7
  • 1
    Have you tried stepping through this code in a debugger? Commented Feb 24, 2015 at 22:46
  • Removing the sleep(1) would make it faster. Why is that there? I don't know if Swing will even be able to repaint stuff while it's busy calling your code. Commented Feb 24, 2015 at 22:48
  • 1
    You're recursing beyond the memory available to you, and it looks like it's because your code is written to do infinite recursion as you never mark a cell as "painted" and then avoid repainting painted cells -- not good. Also, never call repaint or thread.sleep inside of a painting method, not unless your goal is to put your entire GUI to sleep. Commented Feb 24, 2015 at 22:48
  • So, after removing sleep(1); Its still the same. I have to fill a square of 430 x 430 pixels. So, will it be impossible for this method. Please suggest me anything else to fill then. Commented Feb 24, 2015 at 22:51
  • boolean[][] mark variable stops the program to paint the pixel that is already painted. So, its not actually bigger, i think. i just need to extend its filling limit upto 430x430 pixels. Commented Feb 24, 2015 at 22:55

1 Answer 1

1

Look at your code: first call to flood() is with coordinates (0, 0) which later calls flood() with coordinates (1, 0) which calls flood()... With your dimensions, you get to a recursion 500 levels deep. Change your code to not using recursion.

(You can of course increase the stack size with -Xss, but your code is broken, so fix that instead.)

Sign up to request clarification or add additional context in comments.

2 Comments

...and better don't call repaint() while painting.
How can i fix its broken part? Please enlighten me, I am new. Its Flood Fill algorithm that i am trying to implement here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.