记忆测试系统JAVA实现.docx
《记忆测试系统JAVA实现.docx》由会员分享,可在线阅读,更多相关《记忆测试系统JAVA实现.docx(21页珍藏版)》请在冰豆网上搜索。

记忆测试系统JAVA实现
//主类MemoryGame
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjava.util.LinkedList;
publicclassMemoryGameextendsJFrameimplementsActionListener{
JMenuBarbar;
JMenumenuGrade,menuResult,menuIcon;
JMenuItemoneGradeItem,twoGradeItem,threeGradeItem;
JMenuItemoneGradeResult,twoGradeResult,threeGradeResult;
JMenuItemcarImageIcon,animalImageIcon;
FilefileOneGrade,fileTwoGrade,fileThreeGrade,gradeFile;
StringimageName[];
MemoryTestAreamemoryArea;
ShowRecordDialogshowDiolag=null;
intm=5,n=6;
finalintimageNumber=7;
MemoryGame(){
fileOneGrade=newFile("初级记忆排行榜.txt");
fileTwoGrade=newFile("中级记忆排行榜.txt");
fileThreeGrade=newFile("高级记忆排行榜.txt");
bar=newJMenuBar();
menuGrade=newJMenu("选择级别");
oneGradeItem=newJMenuItem("初级");
twoGradeItem=newJMenuItem("中级");
threeGradeItem=newJMenuItem("高级");
menuGrade.add(oneGradeItem);
menuGrade.add(twoGradeItem);
menuGrade.add(threeGradeItem);
menuResult=newJMenu("查看排行榜");
oneGradeResult=newJMenuItem("初级排行榜");
twoGradeResult=newJMenuItem("中级排行榜");
threeGradeResult=newJMenuItem("高级排行榜");
menuResult.add(oneGradeResult);
menuResult.add(twoGradeResult);
menuResult.add(threeGradeResult);
menuIcon=newJMenu("选择图标");
carImageIcon=newJMenuItem("汽车图标");
animalImageIcon=newJMenuItem("动物图标");
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(IOExceptionexp){}
}
if(!
fileTwoGrade.exists()){
try{fileTwoGrade.createNewFile();
}
catch(IOExceptionexp){}
}
if(!
fileThreeGrade.exists()){
try{fileThreeGrade.createNewFile();
}
catch(IOExceptionexp){}
}
setBounds(100,100,400,360);
setVisible(true);
memoryArea=newMemoryTestArea();
imageName=newString[imageNumber];
for(inti=0;iimageName[i]=newString("car"+i+".jpg");
}
m=5;
n=6;
gradeFile=fileOneGrade;
memoryArea.initBlock(m,n,imageName,gradeFile);
add(memoryArea,BorderLayout.CENTER);
showDiolag=newShowRecordDialog();
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicvoidactionPerformed(ActionEventevent){
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(inti=0;iimageName[i]=newString("car"+i+".jpg");
}
memoryArea.setImageName(imageName);
memoryArea.initBlock(m,n,imageName,gradeFile);
}
if(event.getSource()==animalImageIcon){
for(inti=0;iimageName[i]=newString("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);
}
}
publicstaticvoidmain(Stringargs[]){
newMemoryGame();
}
}
//MemoryTestArea类
importjavax.swing.*;
importjava.awt.event.*;
importjava.awt.*;
importjava.util.*;
importjava.io.*;
publicclassMemoryTestAreaextendsJPanelimplementsActionListener,Runnable{
introw,col;
FilegradeFile;
ArrayListallBlockList;
StringimageFileName[];
LinkedListopenIconList;
LinkedListopenBlockList;
intsuccess=0;
ThreadhintThead;
JButtonhintButton;
intusedTime=0;
JTextFieldshowUsedTime,hintMessage;
javax.swing.Timertimer;
Recordrecord;
JPanelcenter,south;
MemoryTestArea(){
setLayout(newBorderLayout());
allBlockList=newArrayList();
openIconList=newLinkedList();
openBlockList=newLinkedList();
hintThead=newThread(this);
hintMessage=newJTextField();
hintMessage.setHorizontalAlignment(JTextField.CENTER);
hintMessage.setEditable(false);
hintMessage.setFont(newFont("宋体",Font.BOLD,18));
center=newJPanel();
south=newJPanel();
hintButton=newJButton("提示");
hintButton.addActionListener(this);
showUsedTime=newJTextField(8);
showUsedTime.setEditable(false);
showUsedTime.setHorizontalAlignment(JTextField.CENTER);
south.add(newJLabel("用时:
"));
south.add(showUsedTime);
south.add(newJLabel("提示图标位置(导致用时增加):
"));
south.add(hintButton);
add(south,BorderLayout.SOUTH);
add(hintMessage,BorderLayout.NORTH);
timer=newjavax.swing.Timer(1000,this);
record=newRecord();
}
publicvoidinitBlock(intm,intn,Stringname[],Filef){
row=m;
col=n;
gradeFile=f;
center.removeAll();
imageFileName=name;
ImageIconicon[]=newImageIcon[imageFileName.length];
for(inti=0;iicon[i]=newImageIcon(imageFileName[i]);
}
if(allBlockList.isEmpty()){
for(inti=0;iallBlockList.add(newBlock());
}
}
else{
allBlockList.clear();
for(inti=0;iallBlockList.add(newBlock());
}
}
for(inti=0;iallBlockList.get(i).addActionListener(this);
allBlockList.get(i).setOpenStateIcon(icon[i%row]);
}
Collections.shuffle(allBlockList);
center.setLayout(newGridLayout(row,col));
for(inti=0;icenter.add(allBlockList.get(i));
}
add(center,BorderLayout.CENTER);
if(timer.isRunning()){
timer.stop();
}
hintMessage.setText("您需要用鼠标单击出"+col+"个同样图标的方块");
usedTime=0;
showUsedTime.setText(null);
validate();
}
publicvoidsetImageName(Stringname[]){
imageFileName=name;
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()instanceofBlock){
if(!
timer.isRunning())
timer.start();
Blockblock=(Block)e.getSource();
ImageIconopenStateIcon=block.getOpenStateIcon();
block.setIcon(openStateIcon);
if(openIconList.size()==0){
openIconList.add(openStateIcon);
openBlockList.add(block);
success=1;
}
else{
ImageIcontemp=openIconList.getLast();
if(temp==openStateIcon&&!
(openBlockList.contains(block))){
success=success+1;
openIconList.add(openStateIcon);
openBlockList.add(block);
if(success==col){
for(inti=0;iallBlockList.get(i).setEnabled(false);
}
for(intj=0;jBlockb=openBlockList.get(j);
b.setDisabledIcon(b.getOpenStateIcon());
}
timer.stop();
record.setTime(usedTime);
record.setGradeFile(gradeFile);
record.setVisible(true);
}
}
elseif((temp!
=openStateIcon)&&(!
(openBlockList.contains(block)))){
openIconList.clear();
openBlockList.clear();
openIconList.add(openStateIcon);
openBlockList.add(block);
success=1;
for(inti=0;iif(allBlockList.get(i)!
=block)
allBlockList.get(i).setIcon(null);
}
}
}
}
if(e.getSource()==hintButton){
if(!
hintThead.isAlive())
hintThead=newThread(this);
for(inti=0;iallBlockList.get(i).removeActionListener(this);
usedTime=usedTime+10;
try{
hintThead.start();
}
catch(IllegalThreadStateExceptionex){}
}
if(e.getSource()==timer){
usedTime++;
showUsedTime.setText("您的用时:
"+usedTime+"秒");
}
}
publicvoidrun(){
for(inti=0;iallBlockList.get(i).setIcon(allBlockList.get(i).getOpenStateIcon());
try{Thread.sleep(1200);
}
catch(InterruptedExceptionexp){}
for(inti=0;iallBlockList.get(i).addActionListener(this);
for(inti=0;iif(!
openBlockList.contains(allBlockList.get(i)))
allBlockList.get(i).setIcon(null);
}
}
//Block类
importjavax.swing.*;
importjava.awt.event.*;
publicclassBlockextendsJButton{
ImageIconopenStateIcon;
publicImageIcongetOpenStateIcon(){
returnopenStateIcon;
}
publicvoidsetOpenStateIcon(ImageIconicon){
openStateIcon=icon;
}
}
//Record类
importjava.io.*;
importjava.util.*;
importjavax.swing.*;
importjava.awt.event.*;
importjava.awt.*;
publicclassRecordextendsJDialogimplementsActionListener{
inttime=0;
JTextFieldyourName;
JLabellabel;
JButtonenter,cancel;
FilegradeFile=null;
publicRecord(){
setBounds(100,100,330,160);
setResizable(false);
setModal(true);
setVisible(false);
enter=newJButton("确定");
cancel=newJButton("取消");
yourName=newJTextField(8);
yourName.setText("匿名");
enter.addActionListener(this);
cancel.addActionListener(this);
setLayout(newGridLayout(2,1));
label=newJLabel();
add(label);
JPanelp=newJPanel();
p.add(yourName);
p.add(enter);
p.add(cancel);
add(p);
}
publicvoidsetGradeFile(Filef){
gradeFile=f;
setTitle("保存成绩到"+gradeFile.getName());
label.setText("保存成绩到"+gradeFile.getName());
validate();
}
publicvoidsetTime(inttime){
this.time=time;
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==enter){
LinkedListlist=newLinkedList();
try{
RandomAccessFileout=newRandomAccessFile(gradeFile,"rw");
o
|
|