Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'ve been wondering about the flood fill algorithm. Could someoneprovide an alg

ID: 3614588 • Letter: I

Question

I've been wondering about the flood fill algorithm. Could someoneprovide an algorithm for clicking on JButton objects in a 2Darray?

For instance, if there is a 10*10 grid of buttons, some of whichhave an attribute other than what we want to fill, and we click ona button; if it happens to contain the attribute that we want, itdisables itself and all the buttons (NorthWest, N, NE, E, SE, S,SW, W) around it where those buttons will do the same (if theyhappen to be the attribute that we want).

I have used recursion, but Java doesn't have enough stack space forthis recursive problem.

Explanation / Answer

I assume you found a way to identify the button, then do arecursive method that spreads out from the target on a 2Dmatrix. The stack overflow is not due to Java, but rather your recursionmethod: if a button has been checked already, you cannot check itagain in your recursive method - which is why you're gettinginfinite recursion, or a stack overflow. Change your recursive method as follows: if the button at thetarget coordinates (for this iteration) is disabled, thenreturn.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote