| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1759 人关注过本帖
标题:种子填充实例运行出问题
只看楼主 加入收藏
BobMaster
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2018-1-4
结帖率:85.71%
收藏
 问题点数:0 回复次数:0 
种子填充实例运行出问题
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);
}
}  
搜索更多相关主题的帖子: Stack int new Point while 
2019-06-13 01:20
快速回复:种子填充实例运行出问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.077114 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved