?? mycanvas.java
字號:
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyCanvas extends JPanel implements MouseListener {
boolean hasAddActionListener=false;//設置方格的動作監聽器的標志位,TRUE為已經添加上動作事件,FALSE是尚未添加動作事件
Cell cell[];//定義方格
Rectangle cellNull;//定義空方格區域
public static int pictureID=1;//當前選擇的圖片代號
public MyCanvas() {
this.setLayout(null);
this.setSize(400,400);
cellNull=new Rectangle(200,200,100,100);//空方格區域在第三行每三列
cell=new Cell[9];
Icon icon;
for (int i = 0; i < 3; i++) {//為9個方格加載圖片,并初使化坐標,形成三行三列
for(int j=0;j<3;j++){
icon=new ImageIcon("pictrue/pic_"+pictureID+"_"+(i*3+j+1)+".jpg");
cell[i*3+j]=new Cell(icon);
cell[i*3+j].setLocation(j*100,i*100);
this.add(cell[i*3+j]);
}
}
this.remove(cell[8]);//移除最后一個多余的方格
}
public void reLoadPictrue(){//當選擇其它圖形進行拼圖時,需重新加載新圖片
Icon icon;
for (int i = 0; i < 3; i++) {
for(int j=0;j<3;j++){
icon=new ImageIcon("pictrue/pic_"+pictureID+"_"+(i*3+j+1)+".jpg");
cell[i*3+j].setIcon(icon);
}
}
}
public boolean isFinish(){//判斷是否拼合成功
for(int i=0;i<8;i++){
int x=cell[i].getBounds().x;
int y=cell[i].getBounds().y;
if(y/100*3+x/100!=i)
return false;
}
return true;
}
public void Start(){//對方格進行重新排列,打亂順序
while(cell[0].getBounds().x<=100&&cell[0].getBounds().y<=100){//當第一個方格距左上角較近時
int x=cellNull.getBounds().x;
int y=cellNull.getBounds().y;
int direction=(int)(Math.random()*4);//產生0-4,對應空方格的上下左右移動
if(direction==0){//空方格左移動,與左側方格互換位置,左側方格右移動
x-=100;
if(test(x,y)){
for(int j=0;j<8;j++){
if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){//依次尋找左側的按鈕
cell[j].move("RIGHT",100);
cellNull.setLocation(x,y);
break;//找到后跳出for循環
}
}
}
}else if(direction==1){//RIGHT
x+=100;
if(test(x,y)){
for(int j=0;j<8;j++){
if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){
cell[j].move("LEFT",100);
cellNull.setLocation(x,y);
break;
}
}
}
}else if(direction==2){//UP
y-=100;
if(test(x,y)){
for(int j=0;j<8;j++){
if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){
cell[j].move("DOWN",100);
cellNull.setLocation(x,y);
break;
}
}
}
}else{//DOWN
y+=100;
if(test(x,y)){
for(int j=0;j<8;j++){
if((cell[j].getBounds().x==x)&&(cell[j].getBounds().y==y)){
cell[j].move("UP",100);
cellNull.setLocation(x,y);
break;
}
}
}
}
}
if(!hasAddActionListener)//如果尚未添加動作事件,則添加
for(int i=0;i<8;i++)//為第個方格添加動作事件,這樣單擊按鈕就能移動了
cell[i].addMouseListener(this);
hasAddActionListener=true;
}
private boolean test(int x,int y){
if((x>=0&&x<=200)||(y>=0&&y<=200))
return true;
else
return false;
}
// public void paint(Graphics g){
//
// for(int i=0;i<=300;i+=100)
// g.drawLine(0, i, 300, i);
// for(int i=0;i<=300;i+=100)
// g.drawLine(i, 0, i, 300);
// for(int i=0;i<8;i++)
// cell[i].repaint();
// }
public void mouseClicked(MouseEvent arg0) { }
public void mouseEntered(MouseEvent arg0) { }
public void mouseExited(MouseEvent arg0) { }
public void mouseReleased(MouseEvent arg0) { }
public void mousePressed(MouseEvent arg0) {//方格的鼠標事件,因為用到了MyCanvas中的一些方法,因此沒有在Cell類中處理鼠標事件
Cell button=(Cell)arg0.getSource();
int x1=button.getBounds().x;//得到所單擊方格的坐標
int y1=button.getBounds().y;
int x2=cellNull.getBounds().x;//得到空方格的坐標
int y2=cellNull.getBounds().y;
if(x1==x2&&y1-y2==100)//進行比較,如果滿足條件則進行交換
button.move("UP",100);
else if(x1==x2&&y1-y2==-100)
button.move("DOWN",100);
else if(x1-x2==100&y1==y2)
button.move("LEFT",100);
else if(x1-x2==-100&&y1==y2)
button.move("RIGHT",100);
else
return;//不滿足就不進行任何處理
cellNull.setLocation(x1,y1);
this.repaint();
if(this.isFinish()){//進行是否完成的判斷
JOptionPane.showMessageDialog(this,"恭喜你完成拼圖,加油!");
for(int i=0;i<8;i++)
cell[i].removeMouseListener(this);//如果已完成,撤消鼠標事件,鼠標單擊方格不在起作用
hasAddActionListener=false;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -