?? playarea.java.bak
字號:
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.applet.*;
import java.net.*;
public class PlayArea extends JPanel implements ActionListener,Runnable{
private int row,col;//行列數
private int count=0;//記錄點擊次數
private ArrayList<ImageButton> allButtonList;//所有的按鈕數組鏈表
private String imageNames[];//圖片名稱數組
private LinkedList<ImageIcon> openIconList;//已經打開的圖片的集合(鏈表)
private LinkedList<ImageButton> openButtonList;//已經翻開圖像的Button集合
private File currentFile;//當前等級打開的相應的文件
private int success=0;//記錄翻開的圖片數目
private Record record;//記錄具體查看Record類定義
private Thread hintThread;
private JButton hintButton;//提示按鈕
private int usedTimes=0;//所用時間記錄
private JTextField showUsedTimes,hintMessage;//提示消息用的文本域
private javax.swing.Timer timer;//時間控件
private JPanel center,south;
private AudioPlay successMusic=null;//翻開相同圖片的聲音
private AudioPlay failMusic=null;//翻開不同圖片的聲音
private AudioPlay finishMusic=null;//完成的聲音
private AudioPlay hintMusic=null;//提示的聲音
public PlayArea(){
setLayout(new BorderLayout());
//下面是初始化不同狀況的聲音
successMusic=new AudioPlay("success.wav");
failMusic=new AudioPlay("fail.wav");
finishMusic=new AudioPlay("finish.wav");
hintMusic =new AudioPlay("hint.wav");
//生成要處理button 和 ImageIcon的集合
allButtonList=new ArrayList<ImageButton>();
openIconList=new LinkedList<ImageIcon>();
openButtonList=new LinkedList<ImageButton>();
hintThread=new Thread(this);
hintMessage=new JTextField();
hintMessage.setHorizontalAlignment(JTextField.CENTER);
hintMessage.setEditable(false);
hintMessage.setFont(new Font("宋體",Font.BOLD,18));
hintMessage.setText("點擊次數越多成績越差");
center=new JPanel();
south=new JPanel();
record=new Record();
hintButton=new JButton("提示");
hintButton.addActionListener(this);
showUsedTimes=new JTextField(12);//顯示時間用的
showUsedTimes.setEditable(false);//不可以編輯
showUsedTimes.setHorizontalAlignment(JTextField.CENTER);//水平居中
south.add(new JLabel("用時:"));
south.add(showUsedTimes);
south.add(new JLabel(" 提示圖標位置(導致用時增加):"));
south.add(hintButton);
add(south,BorderLayout.SOUTH);
add(hintMessage,BorderLayout.NORTH);
timer=new javax.swing.Timer(1000,this);//每一秒觸發一次事件
invalidate();
}
//游戲的初始化
public void initImageButton(int r,int c,String[] imagenames,File currentFile){
row=r;
col=c;
count=0;
this.currentFile=currentFile;
imageNames=imagenames;
openButtonList.clear();
openIconList.clear();
center.removeAll();
center.setLayout(new GridLayout(row,col));
if(timer.isRunning()){
timer.stop();
}
ImageIcon icon[]=new ImageIcon[imageNames.length];
for(int i=0;i<icon.length;i++){
icon[i]=new ImageIcon(imageNames[i]);
}
if(allButtonList.isEmpty()){
for(int i=0;i<row*col;i++){
allButtonList.add(new ImageButton());
}
}else{
allButtonList.clear();
for(int i=0;i<row*col;i++){
allButtonList.add(new ImageButton());
}
}
//給每個button綁定一個圖片 并添加監聽事件
for(int i=0;i<allButtonList.size();i++){
allButtonList.get(i).addActionListener(this);
allButtonList.get(i).setImageIcon(icon[i%row]);
}
Collections.shuffle(allButtonList); //隨機置換按鈕鏈表
for(int i=0;i<col*row;i++){
center.add(allButtonList.get(i));
//allButtonList.get(i).showImageIcon();
}
hintMessage.setText("您需要用鼠標單擊出"+col+"個同樣圖標的方塊");
usedTimes=0;
add(center,BorderLayout.CENTER);
showUsedTimes.setText(null);
validate();
}//end initImageButton
public void actionPerformed(ActionEvent e){
//如果點擊的是顯示圖片的按鈕則有下列事件發生
if(e.getSource() instanceof ImageButton){
count++;//點擊一次記錄就加一
if(!timer.isRunning()){
timer.start();
}
ImageButton imagebutton=(ImageButton)e.getSource();
imagebutton.showImageIcon();//當前點擊的按鈕顯示圖片
if(openIconList.size()==0){//游戲開始或重置的情況
successMusic.play();
openIconList.add(imagebutton.getImageIcon());
openButtonList.add(imagebutton);
success=1;
}else{//非開始或重置情況
if(imagebutton.getImageIcon()==openIconList.getLast()&&!(openButtonList.contains(imagebutton))){
//點擊了相同的圖片,但不是點擊了已經出現的圖片
success+=1;
//這里應該是播放翻開相同圖片的聲音
successMusic.play();
openIconList.add(imagebutton.getImageIcon());
openButtonList.add(imagebutton);
if(success==col){//這里是游戲完成的情況
//這里應該是播放恭喜過關的聲音
finishMusic.play();
for(int i=0;i<allButtonList.size();i++){
allButtonList.get(i).setEnabled(false);
}
for(int i=0;i<openButtonList.size();i++){
openButtonList.get(i).setDisabledIcon(openButtonList.get(i).getImageIcon());
}
timer.stop();
record.setTime(usedTimes);
record.setCount(count);
record.openFile(currentFile);
}
}
else if(imagebutton.getImageIcon()!=openIconList.getLast()&&!(openButtonList.contains(imagebutton))){
//選擇的圖片和前面的不同
//這里應該是播放翻開了不同的圖片的聲音
failMusic.play();
for(int i=0;i<openButtonList.size();i++){
openButtonList.get(i).setIcon(null);
}
openButtonList.clear();
openIconList.clear();
openIconList.add(imagebutton.getImageIcon());
openButtonList.add(imagebutton);
success=1;
}
}
}
if(e.getSource()==hintButton){
//提示
hintMusic.play();
if(!hintThread.isAlive())
hintThread=new Thread(this);
for(int i=0;i<allButtonList.size();i++)
allButtonList.get(i).removeActionListener(this);
usedTimes=usedTimes+10;
try{
hintThread.start();
}
catch(IllegalThreadStateException ex){}
}
if(e.getSource()==timer){
usedTimes++;
showUsedTimes.setText("你用時:"+usedTimes+" 秒"+success);
}
}
public void run(){
for(int i=0;i<allButtonList.size();i++){
allButtonList.get(i).showImageIcon();
}
try{ Thread.sleep(1500);
}
catch(InterruptedException exp){}
for(int i=0;i<allButtonList.size();i++){
allButtonList.get(i).addActionListener(this);
if(!(openButtonList.contains(allButtonList.get(i)))){
allButtonList.get(i).setIcon(null);
}
}
hintMusic.stop();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -