package com.typegame;
import javax.swing.*;
import java.awt.*;
import java.lang.Thread;
import java.awt.event.*;
import java.util.Vector;
import java.util.Enumeration;
class MyJLabel extends JLabel{
MainFrame MyLFrame;
char keyP;
Jt one;
class Jt extends Thread{
JLabel Jtj;
int speed;
int x,y;
int xun;
int killTop;
boolean Goin;
JLabel Kill;
public Jt(JLabel JJ)
{
Jtj=JJ;
Jtj.setFont(new Font("SansSerif", 0, 1));
Jtj.setForeground(Color.WHITE);
x=(int)(Math.random()*500);
y=0;
xun=1;
Goin=true;
}
/**
* 多线程调用的方法
* @author 方强
*
*/
public void run()
{
speed=(int)(MyLFrame.speed);
while(xun>0)
{
if(Goin==false)
{
killTop=killTop-12;
Kill.setBounds(Jtj.getX()+12,killTop,Jtj.getWidth(),Jtj.getHeight());
synchronized(this)//线程同步安全锁
{
if(Kill.getY()<(Jtj.getY()+30))
{
Kill.setVisible(false);
Jtj.setVisible(false);
this.stop();
}
}
}
y=y+4;//下降坐标Y
Jtj.setBounds(x,y, 100, 100);//设置下降
//回收机制
synchronized(this){
if(Goin==true)
{
if(MyLFrame.qufen==false)
{
if(Jtj.getText().equals(String.valueOf(MyLFrame.yeschar)))
{
MyLFrame.yeschar='0';
Kill=new JLabel();
killTop=MyLFrame.getHeight();
Kill.setIcon(new ImageIcon("GIF/Boom.gif"));
Kill.setForeground(Color.WHITE);
MyLFrame.getContentPane().add(Kill,null);
Goin=false;
}
}
else
{
if(Jtj.getText().equalsIgnoreCase(String.valueOf(MyLFrame.yeschar)))
{
Goin=false;
MyLFrame.yeschar='0';
Kill=new JLabel("aa");
killTop=MyLFrame.getHeight();
Kill.setIcon(new ImageIcon("GIF/Boom.gif"));
Kill.setForeground(Color.WHITE);
MyLFrame.getContentPane().add(Kill,null);
}
}
}
if(y>500)//判断是否下落到底
{
Jtj.setVisible(false);
xun=0;
MyLFrame.bad=MyLFrame.bad+1;//下落到底,计数器加1
MyLFrame.badJ.setText("你漏掉了: "+String.valueOf(MyLFrame.bad));//修改显示漏掉个数
this.stop();
}
try
{
sleep(speed);
}
catch(Exception e)
{
e.toString();
}
}
}
}
}
public MyJLabel(char name,MainFrame This)
{
super(String.valueOf(name));
one = new Jt(this);
MyLFrame=This;
}
}
/**
* 游戏窗体
* @author 方强
*
*/
class MainFrame extends JFrame {
int bad,count,speed,start;
boolean qufen;
char yeschar;
JTextField badJ = new JTextField();
threads two = new threads(this);
Vector onon = new Vector();
Enumeration e;
/**
* 多线程类
* @author 方强
*
*/
public class threads extends Thread{
MainFrame tt;
Icon thIcon;
threads(MainFrame zz)
{
tt=zz;
this.setDaemon(false);
}
/**
* 生成图标线程
*/
public void run()
{
while(true)
{
if(e.hasMoreElements())
{
MyJLabel x =(MyJLabel)e.nextElement();
tt.getContentPane().add(x);
x.setVisible(true);
if(qufen==true)//不区分大小写
{
thIcon = new ImageIcon("GIF/"+ x.getText() +".GIF");
}
else//区分大小写
{
if(((int)x.getText().charAt(0))>94)//小写
{
thIcon = new ImageIcon("GIF/"+ x.getText() +"_s.GIF");
}
else//大写
{
thIcon = new ImageIcon("GIF/"+ x.getText() +".GIF");
}
}
x.setIcon(thIcon);
x.one.start();
}
else
{
}
try
{
this.sleep(tt.start);
}
catch(Exception e)
{
e.toString();
}
}
}
}
/**
* 游戏窗体有参构造
* @param star 出现速度
* @param cou 字母个数
* @param spee 下落速度
* @param big 是否区分大小写
*/
public MainFrame(int star,int cou,int spee,boolean big) {
this.getContentPane().setBackground(Color.WHITE);
start = star;
count=cou;
speed= spee;
bad = 0;
yeschar=1;
qufen = big;
char ss;
int ff;
System.out.println(this.getFont());
for(int x=1;x<this.count+1;x++)
{
ff=(int)(Math.random()*25);//产生随机数
if(Math.random()*10>5)//验证大小写
{
ss=(char)(ff+97);
}
else
{
ss=(char)(ff+65);
}
onon.addElement(new MyJLabel(ss,this));
}
e = onon.elements();
two.start();
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
this.setSize(800,600);
this.setVisible(true);
this.setSize(800,600);
}
void jbInit() throws Exception {
this.getContentPane().setLayout(null);
this.getContentPane().add(badJ,null);
this.addKeyListener(new MainFrame_this_keyAdapter(this));
badJ.setFocusable(false);
badJ.setBounds(0,0,100,30);
badJ.setText("你漏掉了:0");
this.setTitle("打字游戏--方强作业");
}
void this_keyPressed(KeyEvent e) {
this.yeschar=e.getKeyChar();
}
}
/**
* 初始化设置窗口
* @author 方强
*
*/
class startFrame extends JFrame {
int iStart,iCount,iSpeed;
JTextField txtStart = new JTextField();
JTextField txtCount = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextField txtSpeed = new JTextField();
JButton cmdOk = new JButton();
JRadioButton oneR = new JRadioButton("不分大小写");
JRadioButton twoR = new JRadioButton("分大小写");
ButtonGroup select = new ButtonGroup();
JButton about = new JButton("关于");
public startFrame() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setTitle("打字游戏--方强作业");
select.add(oneR);
select.add(twoR);
cmdOk.setBounds(new Rectangle(119, 114, 104, 28));
cmdOk.setText("开始");
about.setBounds(new Rectangle(223, 114, 104, 28));
about.addMouseListener(new startFrame_about_mouseAdapter(this));
cmdOk.addMouseListener(new startFrame_cmdOk_mouseAdapter(this));
txtSpeed.setText("100");//初始默认值
txtSpeed.setBounds(new Rectangle(86, 73, 149, 23));
jLabel3.setFont(new java.awt.Font("SansSerif", 0, 15));
jLabel3.setForeground(Color.red);
jLabel3.setText("下落速度");
jLabel3.setBounds(new Rectangle(8, 74, 99, 24));
jLabel2.setFont(new java.awt.Font("SansSerif", 0, 15));
jLabel2.setForeground(Color.red);
jLabel2.setText("字母个数");
jLabel2.setBounds(new Rectangle(8, 36, 99, 24));
jLabel1.setFont(new java.awt.Font("SansSerif", 0, 15));
jLabel1.setForeground(Color.red);
jLabel1.setDebugGraphicsOptions(0);
jLabel1.setText("出现速度");
jLabel1.setBounds(new Rectangle(8, 6, 99, 24));
txtStart.setSelectionStart(0);
txtStart.setText("1000");/