打字游戏

packageTuesDay;

importjava.awt.Color;

importjava.awt.Font;

importjava.awt.Graphics;

importjava.awt.event.KeyEvent;

importjava.awt.event.KeyListener;

importjava.security.Key;

importjavax.swing.JPanel;

publicclassMyKeyPanelextendsJPanelimplementsKeyListener,Runnable{

int[]X=newint[10];//存储10个字母坐标

int[]Y=newint[10];

char[]Z=newchar[10];//存储10个字母

//类执行的时候必定经过的地方

publicMyKeyPanel(){

for(inti=0;i<10;i++){

X[i]=(int)(Math.random()*600);

Y[i]=(int)(Math.random()*600);

Z[i]=(char)(Math.random()*26+97);//加上97数字变成小写字母

System.out.println(Z[i]);

}

}

publicvoidpaint(Graphicsg){

super.paint(g);

setBackground(Color.yellow);

g.setFont(newFont("微软雅黑",Font.BOLD,40));

for(inti=0;i<10;i++){

//字符变成字符串的方法加上个”“

g.drawString(Z[i]+"",X[i],Y[i]);

}

}

@Override

//键盘按住

publicvoidkeyPressed(KeyEvente){

//TODOAuto-generatedmethodstub

System.out.println("keyPressed");

charc=e.getKeyChar();

for(inti=0;i<10;i++){

if(c==Z[i]){

Y[i]=0;

X[i]=(int)(Math.random()*600);

Z[i]=(char)(Math.random()*26+97);

break;//只要有一次相同则跳出循环

}

}

//switch(e.getKeyCode()){

//caseKeyEvent.VK_A:

//System.out.println("AAAAAAA");

//break;

//

//default:

//break;

//}

}

//键盘释放

@Override

publicvoidkeyReleased(KeyEvente){

//TODOAuto-generatedmethodstub

}

//键盘敲击

@Override

publicvoidkeyTyped(KeyEvente){

//TODOAuto-generatedmethodstub

}

@Override

publicvoidrun(){

//TODOAuto-generatedmethodstub

while(true){

try{

Thread.sleep(5);

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

for(inti=0;i<10;i++){

Y[i]++;

if(Y[i]>800){

Y[i]=0;

Z[i]=(char)(Math.random()*26+97);

X[i]=(int)(Math.random()*600);

}

}

repaint();

}

}

}