The code is as follows
Import Java.awt.*;import java.awt.event.*;p ublic class main{public static frame F =new frame (); public static void Main (string[] agrs) {f.setsize (500, 400); F.setlocation (Toolkit.getdefaulttoolkit () getscreensize (). Width-f.getsize (). width)/2, ( Toolkit.getdefaulttoolkit (). Getscreensize (). Height-f.getsize (). Height)/2); F.settitle ("Swing Simple Typing game"); F.setbackground (Color.Black); Mypanel MP =new Mypanel (); F.add (MP); Thread T =new thread (MP); T.start (); F.addkeylistener (MP); Mp.addkeylistener (MP); Mp.addmouselistener (MP); F.setvisible (TRUE); F.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) { System.exit (0); } }); }}class Mypanel extends PanEl implements runnable,keylistener,mouselistener{private static final long serialversionuid = 1L; int x[]=new int[20]; int y[]=new int[20]; Char C[]=new char[20]; int score=1000; int level=1; Boolean flag=true; Public Mypanel () {for (int i=0;i<20;i++) {x[i]= (int) (Math.random () *1020); y[i]= (int) (Math.random () *768)-700; C[i]= (char) (Math.random () *26+65); }} public void Paint (Graphics g) {G.setcolor (Color.Blue); G.setfont (New Font ("Arial", font.bold,30)); if (flag==false) {g.setcolor (color.green); G.setfont (New Font ("script", font.bold,50)); g.DrawString ("Over", Main.f.getsize (). Width/2,main.f.getsize (). HEIGHT/2); } for (int i=0;i<20;i++) {g.drawstring (c[i]+ "", X[i],y[i]); } g.drawstring ("Score:" +score,20,50); g.DrawString ("rank:" +level,850,50); public void Run () {while (flag) {for (int i=0;i<20;i++) { y[i]++; if (y[i]>768) {y[i]=0; x[i]= (int) (Math.random () * (Main.f.getsize (). width)); C[i]= (char) (Math.random () *26+65); score-=100; if (score<=0) {score=0; Flag=false; C=new CHAR[20]; }}} try { Thread.Sleep (20-3*level); } catch (Interruptedexception e) {e.printstacktrace (); } this.repaint (); }} public void mouseclicked (MouseEvent e) {System.out.println (E.getx ()); System.out.println (E.gety ()); } public void keypressed (KeyEvent e) {int maxy=-1000; int index=-1; if (E.getkeycode () >=e.vk_a&&e.getkeycode () <=e.vk_z) {for (int i=0;i<20;i++) { if (E.getkeycode () ==c[i]) {if (Y[i]>maxy) { Maxy=y[i]; Index=i; }}} if (Index!=-1) { y[index]=0; x[index]= (int) (Math.random () * (Main.f.getsize (). width)); C[index]= (char) (Math.random () *26+65); Score +=50; if ((score-500)/500>level) {level++; }} else{score-=100; if (score<=0) {score=0; Flag=false; C=new CHAR[20]; } repaint (); }}} public void keyreleased (KeyEvent arg0) {} public void Ke ytyped (KeyEvent arg0) {} public void mouseentered (MouseEvent arg0) { } public void mouseexited (MouseEvent arg0) {} public void mousepressed (MouseEvent arg0) { } public void mousereleased (MouseEvent arg0) {}}
<ignore_js_op>
Detailed Description: http://java.662p.com/thread-3684-1-1.html
Swing Simple Typing game source