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);
    }
}
rupinderjeet
  • 2,984
  • 30
  • 54
  • 1
    Have you tried stepping through this code in a debugger? – nanofarad Feb 24 '15 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. – user253751 Feb 24 '15 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. – Hovercraft Full Of Eels Feb 24 '15 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. – rupinderjeet Feb 24 '15 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. – rupinderjeet Feb 24 '15 at 22:55
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Feb 25 '15 at 00:45
  • Can you do this in Swing? Will StackOverFlowException disappear? – rupinderjeet Feb 25 '15 at 08:36

1 Answers1

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

Axel
  • 13,939
  • 5
  • 50
  • 79