注册 登录
编程论坛 JAVA论坛

种子填充实例运行出问题

BobMaster 发布于 2019-06-13 01:20, 1954 次点击
import java.awt.*;
import java.applet.*;
import java.awt.image.ImageProducer;
import java.awt.image.MemoryImageSource;
import java.util.Stack;
public class scanseed extends Applet {
    private static final long serialVersionUID = 1L;
    int red=Color.red.getRGB();
    int blue=Color.red.getRGB();
    int black=Color.red.getRGB();
    int  y,ymax,ymin,w=200,h=200;
    Image image;
    int  pixels[]=new int[w*h];
    double x;
    double dy,dx;
    Point []p=new Point[10];
    public void init()
    {
        p[0] = new Point(15, 15);
        p[5] = new Point(145, 35);
        p[4] = new Point(145, 95);
        p[3] = new Point(115, 115);
        p[2] = new Point(65, 115);
        p[1] = new Point(15, 75);
        p[6] = new Point(85, 65);
        p[7]=p[0];
        Point q=new Point(4,6);
        boundary();   
        scan(pixels,q,black,blue,red);  
        ImageProducer ip=new MemoryImageSource(w,h,pixels,0,w);
           image=createImage(ip);
   }
public void boundary()
       {
           for(int i=0;i<7;i++)
           {
               dy=p[i+1].y-p[i].y;     
               if(dy!=0){
               dx=(p[i+1].x-p[i].x)/dy;}
               if(dy>0) x=p[i].x;
               else  x=p[i+1].x;
               ymax=(Math.max(p[i].y,p[i+1].y));
               ymin=(Math.min(p[i].y,p[i+1].y));
               for(y=ymin+1;y<=ymax;y++)
               {
                   x=x+dx;
                   if(pixels[y*w+(int)(x+0.5)]==red)
                   pixels[y*w+(int)(x+0.5)+1]=red;//保证是偶数
                   else
                   pixels[y*w+(int)(x+0.5)]=red;
               }
           }  
       }
public void scan(int[]pixels,Point point,int old_color,int new_color,int boundary_color) {
int x,y,savex,xright,xleft;
Point p;
Stack stack=new Stack();
stack.push(point);
boolean span_need_fill;
while(!stack.empty()) {
    p=(Point)(stack.pop());
    x=p.x;
    y=p.y;
    savex=x;
    while(pixels[y*w+x]!=boundary_color) {
        pixels[y*w+x]=new_color;
        x++;
    }
    xright=x-1;
    x=savex-1;
    while(pixels[y*w+x]!=boundary_color) {
    pixels[y*w+x]=new_color;
    x--;
        }
    xleft=x+1;
    x=xleft;
    y=y+1;
    while(x<=xright) {
        span_need_fill=false;
        while(pixels[w*y+x]==old_color&&x<=xright) {
            span_need_fill=true;
            x++;
        }
        if(span_need_fill) {
            p=new Point(x-1,y);
            stack.push(p);
            span_need_fill=false;
        }
        while(pixels[y*w+x]!=old_color&&x<=xright)x++;
    }
    x=xleft;
    y=y-2;
    while(x<=xright) {
        span_need_fill=false;
        while(pixels[w*y+x]==old_color) {
            span_need_fill=true;
            x++;
        }
        if(span_need_fill) {
            p=new Point(x-1,y);
            stack.push(p);
            span_need_fill=false;
        }
        while(pixels[y*w+x]!=old_color&&x<=xright)x++;
    }
}   
}   
public void paint(Graphics g)
{
    g.drawImage(image, 0, 0, null);
}
}  
0 回复
1