| final int XCELLS=5; //每行拼图的数目 final int YCELLS=4;//每列拼图的数目 final int ALLCELLS=20;//分割元素的数目 final int EMPTY=19;//将第20单元,即cells[19]置成 //空白图像 |
| class Cell {int sx,sy; //起始位置 int cx,cy; //当前位置 Image img; //单元图像 public Cell(Image img,int x,int y) //Cell类构造函数 {this.img=img; sx=x;sy=y;} //给起始位置赋值为x,y } |
| MediaTracker tracker=new MediaTracker(this) //为当前使用类建立1个MediaTracker实体,用于跟踪类 //上的图像 cells[EMPTY]=new Cell(createEmpty(),toPoint(EMPTY).x,toPoint(EMPTY).y); tracker.addImage(cells[EMPTY].img,0); //调用createEmpty()方法产生空白图像,并加入到所跟踪 //的cells数组中20单元 void setPosition(int x,int y) //设置单元图像当前位置 {cx=x;cy=y;} |
| int position[][]=new int[XCELLs][YCELLS] |
| Thread imageThread=null; //定义线程imageThread,初始 //值为空 public void run() {imageThread.setPriority(Thread.MINPRIORITY);//设置线 //程执行优先级别 try {imageThread.sleep(2000);//线程睡眠等待2000ms }catch(InterruptedException e){} first=changeArray();//调用changeArray()方法随机改变图 //像单元位置 while(!loaded)//判断图像若未被跟踪载入,则调用相关 //方法跟踪并加载图像 {repaint(); try {imageThread.sleep(100); }catch(InterruptedException e){System.out.println(e);} } } |
| boolean changeArray() { int source[]=new int[20]; int full[]=new int[20]; for(int i=0;i<ALLCELLS;i++) { int r=(int)(Math.random()*20); while(full[r]!=0) r=(r+(int)(Math.random()*20))%20; source[i]=r; full[r]=1; } int pos=0; for(int i=0;i<ALLCELLS;i++,pos++) { Point p=toPoint(source[pos]); cells[pos].setPosition(p.x,p.y); position[p.x][p.y]=pos; } x=cells[EMPTY].cx; y=cells[EMPTY].cy; return(false); } |
| Image crop(int pos) {//pos参数为调用函数给出的图像单元位置号 Point p=toPoint(pos);//将位置号转化为坐标形式 ImageFilter filter=new CropImageFilter(xside*p.x,yside*p.y,xside,yside); //在给定坐标和长宽的绝对矩形区域内创建分割图像过 //滤器实体filter ImageProducer producer=new FilteredImageSource(baseImage.getSource(),filter;) //由原图像和分割图像过滤器实体创建新的图像 //产生器producer return createImage(producer);//由图像产生器producer产生 //图像并返回 } |
关注此文的读者还看过: