MyPanel.java
资源名称:字母打字游戏.rar [点击查看]
上传用户:hbszxlzwj
上传日期:2009-12-03
资源大小:9k
文件大小:8k
源码类别:
游戏
开发平台:
Java
- //MyPanel.java
- import java.awt.event.*;
- import java.awt.*;
- import java.util.Date;
- public class MyPanel extends Panel
- {
- public MyPanel(Color bgcolor)
- {
- this.setBackground(bgcolor);//预设背景色
- //设置右击快捷菜单
- for(int k=0;k<100;k++)
- {
- xiaoshi[k]=true;
- }
- p1.add(new MenuItem("增加字母数目"));
- p1.add(new MenuItem("加快下落速度"));
- p1.add(new MenuItem("加快变化速度"));
- p1.add(new MenuItem("加快时间间隔"));
- p1.addSeparator();
- p1.add(new MenuItem("减少字母数目"));
- p1.add(new MenuItem("减慢下落速度"));
- p1.add(new MenuItem("降低变化速度"));
- p1.add(new MenuItem("减缓时间间隔"));
- //xuanze(6);
- xuanze(3);
- for(int i=0;i<6;i++)
- {
- fenshu+=zeng[i];
- }
- this.add(p1);
- }
- int mey=2;
- int ss;
- int es;
- int start=0;
- int hour=0;
- int minute=0;
- int fenshu=0;
- Date d1;
- Date d2;
- int shot=0;
- public String changestr(double a)
- {
- String str="0.0000";
- try{ str=""+String.valueOf(a);
- str=str.substring(0,3);}
- catch(Exception exc){}
- return str;
- }
- PopupMenu p1=new PopupMenu();
- boolean reda;
- boolean greena;
- boolean bluea;
- int w;
- int h;
- int xuhao[]=new int[26];
- Image backimg;
- Graphics backg;
- //int num=6;
- int num=2;
- int score=0;
- int total=0;
- char zifu[]=new char[26];
- boolean up=true;
- //随机产生字母
- int jiji=0;
- public void xuanze(int num)
- { jiji=0;
- total+=num;
- for(int k=0;k<num;k++)
- {
- xiaoshi[k]=true;
- }
- if(num>=26){xuhao[25]=25;num=25;}
- else if(num<=1)
- {
- num=1;
- }
- int number1=0;
- int number=0;
- for(int i1=0;i1<num;i1++)
- {
- if(num<=24)
- {
- number1=26;
- }
- else
- {
- number1=25;
- }
- number=(int)(Math.floor((number1*Math.random())));
- while(selected(number,i1))
- {
- number=(int)(Math.floor((number1*Math.random())));
- //System.out.println(number+"");
- }
- xuhao[i1]=number;
- //System.out.println(""+xuhao[i1]+" "+i1+"");
- }
- }
- public boolean selected(int number,int ii)
- {
- for(int j=0;j<ii;j++)
- {
- if(number==xuhao[j])
- return true;
- }
- return false;
- }
- int i=0;
- int delay=100;
- int add=3;
- int dd=0;
- public void update(Graphics g)
- {i++;
- w=size().width;
- h=size().height;
- if(i<=1){d1=new Date();
- ss=d1.getSeconds();}
- if((i%4==0))
- {
- d2=new Date();
- es=d2.getSeconds();
- if(es<ss){es+=60;}
- start=es-ss;
- if(start<=60&&start>=59){if(xiaoshi[95])
- minute++;
- xiaoshi[95]=false;
- }
- if(minute==60){
- if(xiaoshi[96]){
- minute=0;hour++;}
- xiaoshi[96]=false;
- }
- }
- if(red>=254){reda=false;}
- if(red<=1){reda=true;}
- if(green>=254){greena=false;}
- if(green<=1){greena=true;}
- if(blue>=252){bluea=false;}
- if(blue<=3){bluea=true;}
- if(i%5==0){
- if(reda){red++;}
- else{red--;}
- if(greena){green+=2;}else{green-=2;}
- if(bluea){blue+=3;}else{blue-=3;}}
- backg.setColor(new Color(red,green,blue));
- backg.fillRect(0,0,w,h);
- backg.setFont(null);
- backg.setColor(Color.white);
- String defenlv=changestr(((double)(score)/(double)(fenshu)*100));
- int sudu=shot*60/(hour*3600+minute*60+start+1);
- String mingzhonglv=changestr(((double)shot/(double)(total)*100));
- backg.drawString("字母:"+num+" 用时"+hour+":"+minute+":"+start+" 发出:"
- +total+" 命中:"+shot+" 得分:"+score+" 应得:"
- +fenshu+" 速度:"+sudu+"键/分",1,20);
- backg.drawString("命中率:"
- +mingzhonglv+"%"+" 得分率"+defenlv+"%",1,40);
- if(i%delay==0){xuanze(num);
- xiaoshi[95]=true;
- xiaoshi[96]=true;
- px=new int[num];
- zeng=new int[num];
- py=new int[num]; mey=(int)(num*Math.random())-1;
- for(int k=0;k<num;k++)
- {
- px[k]=(int)((w-20)*Math.random());
- if(k<mey)
- {py[k]=(int)((20)*Math.random())+size().height-20;}
- else
- {{py[k]=(int)((20)*Math.random());}}
- zeng[k]=(int)(juli*Math.random())+1;
- fenshu+=zeng[k];
- }}
- paint(g);}
- int zeng[]={3,5,3,7,4,2};
- int juli=2;
- int red=22;
- int green=22;
- int blue=22;
- public void drawchar(Graphics g,int x,int y,char key)
- {
- g.setColor(Color.orange);
- g.fillOval(x,y,20,20);
- g.setColor(Color.blue);
- g.setFont(new Font("TimesRoma",Font.BOLD,18));
- g.drawString(""+(char)key,x+4,y+16);
- }
- int x=-1;
- public void paint(Graphics g)
- {
- for(int mm=1;mm<3600000;mm++) {}
- if(backimg==null)
- {
- w=size().width;
- h=size().height;
- backimg=createImage(w,h);
- backg=backimg.getGraphics();
- if(i<=1)
- {}
- for(int i=0;i<zifu.length;i++)
- {
- zifu[i]=(char)(i+97);
- }
- }
- for(int k=0;k<num;k++)
- {
- if(xiaoshi[k])
- {
- if(k<mey)
- {
- drawchar(backg,px[k],py[k]-=zeng[k],zifu[xuhao[k]]);
- }
- else{
- drawchar(backg,px[k],py[k]+=zeng[k],zifu[xuhao[k]]);
- }
- if(this.i%1000==0)
- {
- drawchar(backg,px[k]+zeng[k],py[k]+=zeng[k],zifu[xuhao[k]]);
- }
- }
- }
- for(int k=0;k<num;k++)
- {
- if(xiaoshi[k])
- {
- if(x==xuhao[k])
- {
- xiaoshi[k]=false;
- shot++;
- score+=zeng[k];
- jiji++;
- backg.setColor(Color.orange);
- backg.fill3DRect(px[k]+6,py[k]+5,6,size().height-py[k],true);
- backg.setColor(Color.magenta);
- dddd=px[k]-15;
- backg.fill3DRect(px[k]-15,size().height-10,50,10,true);
- if(jiji==num)
- {
- backg.setColor(new Color(red,green,blue));
- try{
- xuanze(num);
- for(int i=0;i<num;i++)
- fenshu+=zeng[i];
- }
- catch(Exception ex){}
- }
- }
- }
- }
- backg.setColor(new Color(255-red,255-green,255-blue));
- backg.fill3DRect(dddd,size().height-10,40,10,true);
- g.drawImage(backimg,0,0,w,h,this);
- if(up)
- {
- repaint(time);
- }
- }
- int time=30;
- boolean xiaoshi[]=new boolean[100];
- //int px[]={80,255,170,240,290,350};
- // int py[]={200,340,50,30,60,70};
- int px[]={40,40,40,40,40,40};
- int py[]={700,700,700,700,700,700};
- public boolean keyDown(Event e, int key)
- {
- if(up==false)
- {up=true;repaint();}
- x=key-97;
- return super.keyDown(e, key);
- }
- public boolean keyUp(Event e, int key)
- {
- x=-1;
- return super.keyDown(e, key);
- }
- public boolean mouseDown(Event e, int p2, int p3)
- {
- if(e.metaDown())
- {
- up=false;
- p1.show(this,p2,p3);}
- else up=true;
- return super.mouseDown(e, p2, p3);
- }
- int dddd=2000;
- public boolean action(Event e, Object what)
- {
- this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
- if("增加字母数目".equals(what))
- {
- num++;
- if(num>=11)
- {
- delay=(size().height-50)/(juli/2);
- }
- if(num>=26)
- num=26;
- xuanze(num);
- px=new int[num];
- zeng=new int[num];
- py=new int[num];
- mey=(int)(num*Math.random())-1;
- mey=(int)(num*Math.random())-1;
- for(int k=0;k<num;k++)
- {
- px[k]=(int)((w-20)*Math.random());
- if(k<mey)
- {
- py[k]=(int)((20)*Math.random())+size().height-20;
- }
- else
- {
- py[k]=(int)((20)*Math.random());
- }
- zeng[k]=(int)(juli*Math.random())+1;fenshu+=zeng[k];
- }
- }
- if("减少字母数目".equals(what))
- {
- num--;
- if(num>=11)
- {delay=(size().height-50)/(juli/2);}
- if(num<=1)
- num=1;
- xuanze(num);
- px=new int[num];
- zeng=new int[num];
- py=new int[num];
- mey=(int)(num*Math.random())-1;
- for(int k=0;k<num;k++)
- {
- px[k]=(int)((w-20)*Math.random());
- if(k<mey)
- {
- py[k]=(int)((20)*Math.random())+size().width-20;
- }
- else
- {
- py[k]=(int)((20)*Math.random());
- }
- zeng[k]=(int)(juli*Math.random())+1;fenshu+=zeng[k];
- }
- }
- if("加快下落速度".equals(what))
- {juli+=3;}
- if("加快变化速度".equals(what))
- {delay-=5;if(delay<=10){delay=10;}}
- if("加快时间间隔".equals(what))
- {time+=3;}
- if("减慢下落速度".equals(what))
- {juli-=3;if(juli<=3){juli=3;}}
- if("降低变化速度".equals(what))
- {delay+=5;}
- if("减缓时间间隔".equals(what))
- {time-=3;if(time<=4){time=4;}}
- this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
- up=true;
- return super.action(e, what);
- }
- }