Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  traversing all pixels in an area  (Read 5555 times)
0 Members and 1 Guest are viewing this topic.
Offline moogie

JGO Knight


Medals: 11
Projects: 5
Exp: 10 years


Java games rock!


« Reply #30 - Posted 2003-11-04 00:53:10 »

currently it is all in the main thread.

Obviously i would like the program to run with out the user having to specify switches for the jvm, so increasing stack size is not an option.
Offline Jeff

JGO Coder




Got any cats?


« Reply #31 - Posted 2003-11-04 03:02:46 »

Quote
currently it is all in the main thread.

Obviously i would like the program to run with out the user having to specify switches for the jvm, so increasing stack size is not an option.


I don't see how thats obvious, but if its a requirement then yes you are either going to have to spawn a thread with a large stack size or live with the default constraints.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline moogie

JGO Knight


Medals: 11
Projects: 5
Exp: 10 years


Java games rock!


« Reply #32 - Posted 2003-11-04 03:14:53 »

Smiley

minimizing the effort on the users side its always a high priority.

I am more than pleased with the current performance of the flood fill algorithm that Tom has suggested. If there comes a time that even faster fill is needed i will look into spawning a thread.

Thanks
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #33 - Posted 2003-11-04 03:37:25 »

Quote
Smiley

minimizing the effort on the users side its always a high priority.


There are many solutions to that.  Most installer generators (eg InstallAnywhere, InstallSheild for Java) allow you to package your program with command line flags as aprt of the installed execution front end.  These front ends tend to be easier for the user to install and use then a Jar file as they even hide the java invocation.

On a simpler level a batch or .sh script can hide these things quite nicely as well/

OR you could spawn a secondary thread, as already mentioned.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline jayhg99

Senior Newbie




Java games rock!


« Reply #34 - Posted 2003-11-10 17:49:27 »

Tom, can you post a working version of this ? This is very interesting .

Thanks


Jay
Offline tom
« Reply #35 - Posted 2003-11-10 22:01:09 »

Sure. The following code generates the outline of a flower, and then fills it with a green color. The image is drawn in the recursive funtion, so you can see how it is filled. Might help you figure out how the algorithm works.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;

/** A very fast flood fill */
public class LinearFloodFill {
      private Frame frame;
      private BufferedImage bufferedImage;
      private int image[];
      private int fillcolor;
      private int startcolor;
      private int width;
      private int height;


    /** The progress is drawn on the specified frame. */
      public LinearFloodFill(Frame frame) {
            this.frame = frame;
      }
     
     
      /** Fills the image width fillcolor starting at the specified position */
      public synchronized void fill(int image[], int width, int height, int startx, int starty, int fillcolor) {
            this.image = image;
            this.width = width;
            this.height = height;
            this.fillcolor = fillcolor;
            startcolor = image[starty*width+startx];
            bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            linearFloodFill4(startx, starty, -1, -1, -2);
      }

      /**
       * Fills the line at (x, y). Then fills the line above and below the current line.
       * The border is defined as any color except the start color.
       */

      private int linearFloodFill4(int x, int y, int prevleft, int prevright, int prevy) {
            draw();
            int yOff = y*width;
            // fill left of (x,y) until border or edge of image
           int left=x;
            do {
                  image[yOff+left] = fillcolor;
                  left--;
            } while ((left >= 0) && (image[yOff+left] == startcolor));
            left++;
            // fill right of (x, y) until border or edge of image
           int right=x;
            do {
                  image[yOff+right]=fillcolor;  
                  right++;
            } while ((right < width) && (image[yOff+right] == startcolor));
            right--;

            // check the line above this one. This is done by checking all the pixels
           // above current line. To speed it up we do not check the line segment
           // that we will fill next, or the line segment that was filled last.
           if (y>0) {
                  int topyOff = (y-1)*width;
                  if ((y-1) == prevy) {
                        int min = Math.min(prevleft, right);
                        for (int i=left; i<=min; i++) {
                              if (image[topyOff+i] == startcolor) {
                                    i = linearFloodFill4(i, y-1, left, right, y);
                              }
                        }
                        for (int i=Math.max(prevright, left); i<=right; i++) {
                              if (image[topyOff+i] == startcolor) {
                                    i = linearFloodFill4(i, y-1, left, right, y);
                              }
                        }
                  } else {
                        for (int i=left;i<=right;i++) {
                              if (image[topyOff+i] == startcolor) {
                                    i = linearFloodFill4(i, y-1, left, right, y);
                              }
                        }
                  }
            }
                 
            // check the line belov this one.
           if (y<height-1) {
                  int botyOff = (y+1)*width;
                  if (prevy == y+1) {
                        int min = Math.min(prevleft, right);
                        for (int i=left; i<=min; i++) {
                              if (image[botyOff+i] == startcolor) {
                                    i = linearFloodFill4(i, y+1, left, right, y);
                              }
                        }
                        for (int i=Math.max(prevright, left); i<=right; i++) {
                              if (image[botyOff+i] == startcolor) {
                                    i = linearFloodFill4(i, y+1, left, right, y);
                              }
                        }
                  } else {
                        for (int i=left;i<=right;i++) {
                              if (image[botyOff+i] == startcolor) {
                                    i = linearFloodFill4(i, y+1, left, right, y);
                              }
                        }
                  }
            }
           
            return right;
      }


      /**
       * Draws the last image to the frame.
       */

      public void draw() {
            if (frame != null && bufferedImage != null) {
                  Insets insets = frame.getInsets();
                  bufferedImage.setRGB(0, 0, width, height, image, 0, width);
                  Graphics g = frame.getGraphics();
                  g.drawImage(bufferedImage, insets.left, insets.top, null);
            }
      }
     
     
     
      /**
       * Tests the flood fill.
       */

      public static void main(String args[]) {
            Frame frame = new Frame();
            frame.setBounds(350, 250, 300, 300);
            frame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});
            frame.show();

            // Create a flower. This should be replaced by the image you want to fill.
           int pixels[] = new int[256*256];
            for (int i=0; i<pixels.length; i++) {
                  pixels[i] = 0xffff0000;
            }
            for (double angle=0; angle<Math.PI*2; angle+=0.001) {
                  double radius = 50+Math.sin(angle*10)*45;
                  double x = 128+Math.cos(angle)*radius;
                  double y = 128+Math.sin(angle)*radius;
                  pixels[(int)y*256+(int)x] = 0xff000000;
            }

            // Flood fill the created image with a green color, starting at the center.
           // The image is drawn to the frame as it is filled.
           LinearFloodFill filler = new LinearFloodFill(frame);
            filler.fill(pixels, 256, 256, 128, 128, 0xff00ff00);
            filler.draw();
      }
}


Enjoy!!

Pages: 1 [2]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (16 views)
2014-04-15 18:08:23

BurntPizza (14 views)
2014-04-15 03:46:01

UprightPath (27 views)
2014-04-14 17:39:50

UprightPath (12 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (51 views)
2014-04-10 04:04:31

BurntPizza (110 views)
2014-04-08 23:06:04

tom_mai78101 (211 views)
2014-04-05 13:34:39

trollwarrior1 (180 views)
2014-04-04 12:06:45

CJLetsGame (185 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!