拼图游戏图片的分割与移动

 图片分割 

public Image[] splitImage(String file, int rows, int cols) 

{   

Image t = new ImageIcon(file).getImage();   

int w = t.getWidth(this) / cols;   

int h = t.getHeight(this) / rows;   

return splitImage(t, rows, cols, w, h);   

}    

public Image[] splitImage(String file, int rows, int cols, int w, int h) 

{   

Image t = new ImageIcon(file).getImage();   

return splitImage(t, rows, cols, w, h);   

}    

public Image[] splitImage(Image t, int rows, int cols, int w, int h) 

{    

Image[] result = new Image[rows * cols]; 

   try 

{    

for (int i = 0; i < result.length; i++) 

{     

result[i] = createImage(w, h);     

Graphics g = result[i].getGraphics();     

g.translate((-i % cols) * w, (-i / cols) * h);     

g.drawImage(t, 0, 0, w * cols, rows * h, 0, 0,         

t.getWidth(this), t.getHeight(this), this);      

 }    

} catch (Exception e) 

{   

}    

return result;   

}  

 } 

 图片移动

向上移动          

if(x1>0&&btn[x1-1][y1].isVisible()&&btn[x1-1][y1].getIcon()==null)     

{      

btn[x1-1][y1].setIcon(btn[x1][y1].getIcon());//获取当前图标给上一个控件为图标       

btn[x1][y1].setIcon(null);//当前图标设置为空      

step++;

 

}               

向下移动      

if(x1<m&&btn[x1+1][y1].isVisible()&&btn[x1+1][y1].getIcon()==null)     

{      

btn[x1+1][y1].setIcon(btn[x1][y1].getIcon());//获取当前图标给下一个控件为图标     

 btn[x1][y1].setIcon(null);//当前图标设置为空     

 step++;

 }            

向左移动      

if(y1>0&&btn[x1][y1-1].isVisible()&&btn[x1][y1-1].getIcon()==null)     

{          

btn[x1][y1-1].setIcon(btn[x1][y1].getIcon());//获取当前图标给左一个控件为图标       

btn[x1][y1].setIcon(null);//当前图标设置为空      

step++;

 }               

向右移动 

if(y1<n-1&&btn[x1][y1+1].isVisible()&&btn[x1][y1+1].getIcon()==null)

{      

btn[x1][y1+1].setIcon(btn[x1][y1].getIcon());//获取当前图标给右一个控件为图标      

btn[x1][y1].setIcon(null);//当前图标设置为空    

step++;

}           

btn[x1][y1].setBackground(Color.white); 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值