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.*;
public class LinearFloodFill { private Frame frame; private BufferedImage bufferedImage; private int image[]; private int fillcolor; private int startcolor; private int width; private int height;
public LinearFloodFill(Frame frame) { this.frame = frame; } 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); }
private int linearFloodFill4(int x, int y, int prevleft, int prevright, int prevy) { draw(); int yOff = y*width; int left=x; do { image[yOff+left] = fillcolor; left--; } while ((left >= 0) && (image[yOff+left] == startcolor)); left++; int right=x; do { image[yOff+right]=fillcolor; right++; } while ((right < width) && (image[yOff+right] == startcolor)); right--;
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); } } } } 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; }
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); } } 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();
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; }
LinearFloodFill filler = new LinearFloodFill(frame); filler.fill(pixels, 256, 256, 128, 128, 0xff00ff00); filler.draw(); } } |