colors - Why do I get java.lang.StackOverflowError when using Flood Fill algorithm? -


my program supposed fill in non-regular shape color (black , white beginning) specify in boundaryfill4 method. here link myimage.png: https://dl.dropbox.com/u/41007907/myimage.png use simple flood fill algorithm, not work somehow... here full code:

  import java.awt.color;   import java.awt.container;   import java.awt.image;   import java.awt.image.bufferedimage;   import javax.swing.imageicon;   import javax.swing.jframe;   import javax.swing.jlabel;        public class mypolygon extends jframe {  private jlabel my;  public mypolygon() throws interruptedexception {     createmy(); }  private void createmy() throws interruptedexception {     container contentpane = getcontentpane();     contentpane.setbackground(color.white);     contentpane.setlayout(null);     contentpane.setsize(1000, 700);      = new jlabel();     my.seticon(new imageicon("myimage.png"));     my.setbounds(50, 50, 300, 300);     contentpane.add(my);      setsize(1000, 700);     setvisible(true);     setlocationrelativeto(null);      int fill = 100;     boundaryfill4(100, 100, fill, 50); }  // flood fill method public void boundaryfill4(int x, int y, int fill, int boundary) {     int current;     current = getpixel(x, y);     if ((current >= boundary) && (current != fill)) {         setpixel(x, y, fill);         boundaryfill4(x + 1, y, fill, boundary);         boundaryfill4(x - 1, y, fill, boundary);         boundaryfill4(x, y + 1, fill, boundary);         boundaryfill4(x, y - 1, fill, boundary);     } }  // getting color integer @ specified point(x, y) private int getpixel(int x, int y) {     image img = ((imageicon) my.geticon()).getimage();     bufferedimage buffered = new bufferedimage(img.getwidth(null),             img.getheight(null), bufferedimage.type_int_argb);     buffered.getgraphics().drawimage(img, 0, 0, null);     color c = new color(buffered.getrgb(x, y));     int current = buffered.getrgb(x, y);     return current; }  // setting color integer specified point(x, y) private void setpixel(int x, int y, int fill) {     image img = ((imageicon) my.geticon()).getimage();     bufferedimage buffered = new bufferedimage(img.getwidth(null),             img.getheight(null), bufferedimage.type_int_argb);     buffered.getgraphics().drawimage(img, 0, 0, null);     int red = fill;     int green = fill;     int blue = fill;     color c = new color(buffered.getrgb(x, y));     c = new color(red, green, blue);     buffered.setrgb(x, y, c.getrgb()); }  // main method public static void main(string args[]) throws interruptedexception {     mypolygon = new mypolygon();     my.setdefaultcloseoperation(jframe.exit_on_close); } } 

why stackoverflow error? how can correct code works?

stackoverflowexception means, recursion deep memory or not end. try on smaller image. when not solves problem there wrong recursion-end-condition. (does setpixel() , getpixel change image? write junittest)

also should simplify setpixel , getpixel methods. complex. every pixel set or create new bufferedimage-instance , dispose after setting 1 pixel. can store , reuse bufferedimage.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -