




已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
/主類MemoryGameimport javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.LinkedList;public class MemoryGame extends JFrame implements ActionListener JMenuBar bar; JMenu menuGrade,menuResult,menuIcon; JMenuItem oneGradeItem,twoGradeItem,threeGradeItem; JMenuItem oneGradeResult,twoGradeResult,threeGradeResult; JMenuItem carImageIcon,animalImageIcon; File fileOneGrade,fileTwoGrade,fileThreeGrade,gradeFile; String imageName; MemoryTestArea memoryArea; ShowRecordDialog showDiolag=null; int m=5,n=6; final int imageNumber=7; MemoryGame() fileOneGrade=new File(初級記憶排行榜.txt); fileTwoGrade=new File(中級記憶排行榜.txt); fileThreeGrade=new File(高級記憶排行榜.txt); bar=new JMenuBar(); menuGrade=new JMenu(選擇級別); oneGradeItem=new JMenuItem(初級); twoGradeItem=new JMenuItem(中級); threeGradeItem=new JMenuItem(高級); menuGrade.add(oneGradeItem); menuGrade.add(twoGradeItem); menuGrade.add(threeGradeItem); menuResult=new JMenu(查看排行榜); oneGradeResult=new JMenuItem(初級排行榜); twoGradeResult=new JMenuItem(中級排行榜); threeGradeResult=new JMenuItem(高級排行榜); menuResult.add(oneGradeResult); menuResult.add(twoGradeResult); menuResult.add(threeGradeResult); menuIcon=new JMenu(選擇圖標); carImageIcon=new JMenuItem(汽車圖標); animalImageIcon=new JMenuItem(動物圖標); animalImageIcon.addActionListener(this); carImageIcon.addActionListener(this); menuIcon.add(carImageIcon); menuIcon.add(animalImageIcon); bar.add(menuGrade); bar.add(menuResult); bar.add(menuIcon); setJMenuBar(bar); oneGradeItem.addActionListener(this); twoGradeItem.addActionListener(this); threeGradeItem.addActionListener(this); oneGradeResult.addActionListener(this); twoGradeResult.addActionListener(this); threeGradeResult.addActionListener(this); if(!fileOneGrade.exists() try fileOneGrade.createNewFile(); catch(IOException exp) if(!fileTwoGrade.exists() try fileTwoGrade.createNewFile(); catch(IOException exp) if(!fileThreeGrade.exists() try fileThreeGrade.createNewFile(); catch(IOException exp) setBounds(100,100,400,360); setVisible(true); memoryArea=new MemoryTestArea(); imageName=new StringimageNumber; for(int i=0;iimageName.length;i+) imageNamei=new String(car+i+.jpg); m=5; n=6; gradeFile=fileOneGrade; memoryArea.initBlock(m,n,imageName,gradeFile); add(memoryArea,BorderLayout.CENTER); showDiolag=new ShowRecordDialog(); validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void actionPerformed(ActionEvent event) if(event.getSource()=oneGradeItem) m=5; n=6; gradeFile=fileOneGrade; memoryArea.initBlock(m,n,imageName,gradeFile); if(event.getSource()=twoGradeItem) m=6; n=7; gradeFile=fileTwoGrade; memoryArea.initBlock(m,n,imageName,gradeFile); if(event.getSource()=threeGradeItem) m=7; n=8; gradeFile=fileThreeGrade; memoryArea.initBlock(m,n,imageName,gradeFile); if(event.getSource()=carImageIcon) for(int i=0;iimageName.length;i+) imageNamei=new String(car+i+.jpg); memoryArea.setImageName(imageName); memoryArea.initBlock(m,n,imageName,gradeFile); if(event.getSource()=animalImageIcon) for(int i=0;iimageName.length;i+) imageNamei=new String(ani+i+.jpg); memoryArea.setImageName(imageName); memoryArea.initBlock(m,n,imageName,gradeFile); if(event.getSource()=oneGradeResult) showDiolag.setGradeFile(fileOneGrade); showDiolag.showRecord(); showDiolag.setVisible(true); if(event.getSource()=twoGradeResult) showDiolag.setGradeFile(fileTwoGrade); showDiolag.showRecord(); showDiolag.setVisible(true); if(event.getSource()=threeGradeResult) showDiolag.setGradeFile(fileThreeGrade); showDiolag.showRecord(); showDiolag.setVisible(true); public static void main(String args) new MemoryGame(); /MemoryTestArea類import javax.swing.*; import java.awt.event.*;import java.awt.*;import java.util.*;import java.io.*;public class MemoryTestArea extends JPanel implements ActionListener,Runnable int row,col; File gradeFile; ArrayList allBlockList; String imageFileName; LinkedList openIconList; LinkedList openBlockList; int success=0; Thread hintThead; JButton hintButton; int usedTime=0; JTextField showUsedTime,hintMessage; javax.swing.Timer timer; Record record; JPanel center,south; MemoryTestArea() setLayout(new BorderLayout(); allBlockList=new ArrayList(); openIconList=new LinkedList(); openBlockList=new LinkedList(); hintThead=new Thread(this); hintMessage=new JTextField(); hintMessage.setHorizontalAlignment(JTextField.CENTER); hintMessage.setEditable(false); hintMessage.setFont(new Font(宋體,Font.BOLD,18); center=new JPanel(); south=new JPanel(); hintButton=new JButton(提示); hintButton.addActionListener(this); showUsedTime=new JTextField(8); showUsedTime.setEditable(false); showUsedTime.setHorizontalAlignment(JTextField.CENTER); south.add(new JLabel(用時:) ; south.add(showUsedTime); south.add(new JLabel(提示圖標位置(導致用時增加):) ; south.add(hintButton); add(south,BorderLayout.SOUTH); add(hintMessage,BorderLayout.NORTH); timer=new javax.swing.Timer(1000,this); record=new Record(); public void initBlock(int m,int n,String name,File f) row=m; col=n; gradeFile=f; center.removeAll(); imageFileName=name; ImageIcon icon=new ImageIconimageFileName.length; for(int i=0;iicon.length;i+) iconi=new ImageIcon(imageFileNamei); if(allBlockList.isEmpty() for(int i=0;irow*col;i+) allBlockList.add(new Block(); else allBlockList.clear(); for(int i=0;irow*col;i+) allBlockList.add(new Block(); for(int i=0;iallBlockList.size();i+) allBlockList.get(i).addActionListener(this); allBlockList.get(i).setOpenStateIcon(iconi%row); Collections.shuffle(allBlockList); center.setLayout(new GridLayout(row,col); for(int i=0;iallBlockList.size();i+) center.add(allBlockList.get(i); add(center,BorderLayout.CENTER); if(timer.isRunning() timer.stop(); hintMessage.setText(您需要用鼠標單擊出+col+個同樣圖標的方塊); usedTime=0; showUsedTime.setText(null); validate(); public void setImageName(String name) imageFileName=name; public void actionPerformed(ActionEvent e) if(e.getSource() instanceof Block) if(!timer.isRunning() timer.start(); Block block=(Block)e.getSource(); ImageIcon openStateIcon=block.getOpenStateIcon(); block.setIcon(openStateIcon); if(openIconList.size()=0) openIconList.add(openStateIcon); openBlockList.add(block); success=1; else ImageIcon temp=openIconList.getLast(); if(temp=openStateIcon&!(openBlockList.contains(block) success=success+1; openIconList.add(openStateIcon); openBlockList.add(block); if(success=col) for(int i=0;iallBlockList.size();i+) allBlockList.get(i).setEnabled(false); for(int j=0;jopenBlockList.size();j+) Block b=openBlockList.get(j); b.setDisabledIcon(b.getOpenStateIcon(); timer.stop(); record.setTime(usedTime); record.setGradeFile(gradeFile); record.setVisible(true); else if(temp!=openStateIcon)&(!(openBlockList.contains(block) openIconList.clear(); openBlockList.clear(); openIconList.add(openStateIcon); openBlockList.add(block); success=1; for(int i=0;iallBlockList.size();i+) if(allBlockList.get(i)!=block) allBlockList.get(i).setIcon(null); if(e.getSource()=hintButton) if(!hintThead.isAlive() hintThead=new Thread(this); for(int i=0;iallBlockList.size();i+) allBlockList.get(i).removeActionListener(this); usedTime=usedTime+10; try hintThead.start(); catch(IllegalThreadStateException ex) if(e.getSource()=timer) usedTime+; showUsedTime.setText(您的用時:+usedTime+秒); public void run() for(int i=0;iallBlockList.size();i+) allBlockList.get(i).setIcon(allBlockList.get(i).getOpenStateIcon(); try Thread.sleep(1200); catch(InterruptedException exp) for(int i=0;iallBlockList.size();i+) allBlockList.get(i).addActionListener(this); for(int i=0;iallBlockList.size();i+) if(!openBlockList.contains(allBlockList.get(i) allBlockList.get(i).setIcon(null); /Block 類import javax.swing.*;import java.awt.event.*;public class Block extends JButton ImageIcon openStateIcon; public ImageIcon getOpenStateIcon() return openStateIcon; public void setOpenStateIcon(ImageIcon icon) openStateIcon=icon; /Record類import java.io.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class Record extends JDialog implements ActionListener int time=0; JTextField yourName; JLabel label; JButton enter,cancel; File gradeFile=null; public Record() setBounds(100,100,330,160); setResizable(false); setModal(true); setVisible(false); enter=new JButton(確定); cancel=new JButton(取消); yourName=new JTextField(8); yourName.setText(匿名); enter.addActionListener(this); cancel.addActionListener(this); setLayout(new GridLayout(2,1); label=new JLabel(); add(label); JPanel p=new JPanel(); p.add(yourName); p.add(enter); p.add(cancel); add(p); public void setGradeFile(File f) gradeFile=f; setTitle(保存成績到+gradeFile.getName(); label.setText(保存成績到+gradeFile.getName(); validate(); public void setTime(int time) this.time=time; public void actionPerformed(ActionEvent e) if(e.getSource()=enter) LinkedList list=new LinkedList(); try RandomAccessFile out=new RandomAccessFile(gradeFile,rw); out.seek(out.length(); out.writeUTF(yourName.getText(); out.writeInt(time); out.close(); catch(Exception event) setVisible(false); if(e.getSource()=cancel) setVisible(false); /ShowRecordDialog 類import java.io.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class ShowRecordDialog extends JDialog implements ActionListener File gradeFile; JButton clear; JTextArea showArea=null; TreeSet treeSet; public ShowRecordDialog() treeSet=new TreeSet(); showArea=new JTextArea(6,4); showArea.setFont(new Font(楷體,Font.BOLD,20); clear=new JButton(清空排行榜); clear.addActionListener(this); add(new JScrollPane(showArea),BorderLayout.CENTER); add(clear,BorderLayout.SOUTH); setBounds(100,100,320,185); setModal(true); addWindowListener(new WindowAdapter() public void windwoClosing(WindowEvent e) setVisible(false); ); public void setGradeFile(File f) gradeFile=f; setTitle(f.getName(); public void showRecord() showArea.setText(null); treeSet.clear(); try RandomAccessFile in=new RandomAccessFile(gradeFile,rw); long fileLength=in.length(); long readPosition=0; while(readPositionfileLength) String name=in.readUTF(); int time=in.readInt(); readPosition=in.getFilePointer(); People people=new People(name,time); treeSet.add(people); in.close(); Iterator iter=treeSet.iterator(); while(iter.hasNext() People p=iter.next(); showArea.append(姓名:+p.getName()+,成績: +p.getTime()+秒); showArea.append(n); catch(IOException exp)System.out.println(exp); public void actionPerformed(ActionEvent e) if(e.getSource()=clear) try File f=gradeFile.getAbsoluteFile(); gradeFile.delete(); f.createNewFile(); showArea.setText(排行榜被清空); catch(Exception ee) /ShowRecord類import java.io.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class ShowRecord extends JDialog implements ActionListener File gradeFile; JButton clear; JTextArea showArea=null; People people; TreeSet treeSet; public ShowRecord(File f) treeSet=new TreeSet(); showArea=new JTextArea(6,4); clear=new JButton(清空排行榜); clear.addActionListener(this); add(new JScrollPane(showArea),BorderLayout.CENTER); add(clear,BorderLayout.SOUTH); setBounds(100,100,320,185)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025租房合同樣本匯編范本
- 2025保密合同范本
- 2025裝修涂料供貨合同書
- 2025辦公室室內(nèi)裝修合同書
- 2025建筑工程設計版合同
- 2025青年創(chuàng)業(yè)者夏季招聘困局:合同簽訂難題多維權(quán)之路漫漫
- 2025個人借款合同協(xié)議書
- 2025有關貨車司機勞動合同
- 2025幕墻工程的采購合同范本
- 2025標準商業(yè)代理合同范本
- 業(yè)主委員會備案申請表填寫模板
- DL∕T 5210.6-2019 電力建設施工質(zhì)量驗收規(guī)程 第6部分:調(diào)整試驗
- 2024年工程承包合同書范文
- 有限空間作業(yè)風險辨識管控臺帳
- JGJT397-2016 公墓和骨灰寄存建筑設計規(guī)范
- 拖拉機濕式離合器
- 中學教材、教輔資料征訂管理制度
- 急性胰腺炎急診診治專家共識2024解讀
- 管道安裝工程款承諾書
- 全國青少年信息素養(yǎng)大賽python初賽必做題模擬五卷
- 權(quán)利保障 于法有據(jù) 說課課件-2023-2024學年高中政治統(tǒng)編版選擇性必修二法律與生活
評論
0/150
提交評論