typetrainappletbak.java~3~

来自「applet java 的编程程序」· JAVA~3~ 代码 · 共 347 行

JAVA~3~
347
字号
package chapter11;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class TypeTrainAppletBak
    extends JApplet
{
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel infoPnl = new JPanel();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton(); //Construct the applet
    JLabel totalLbl_1 = new JLabel("总数");
    JLabel totalLbl_2 = new JLabel(); //计算总共下落的字母数标签
    JLabel rightLbl_1 = new JLabel("正确数");
    JLabel rightLbl_2 = new JLabel(""); //计算正确击中的字母数标签
    JLabel errorLbl_1 = new JLabel("失败数");
    JLabel errorLbl_2 = new JLabel(""); //计算未击中的字母数标签

    Canvas canvas = new Canvas(); //画板
    ImageIcon startIcon = new ImageIcon(chapter11.TypeTrainAppletBak.class.
                                        getResource(
                                            "start.gif"));

    ImageIcon pauseIcon = new ImageIcon(chapter11.TypeTrainAppletBak.class.
                                        getResource(
                                            "pause.gif"));

    ImageIcon stopIcon = new ImageIcon(chapter11.TypeTrainAppletBak.class.
                                       getResource(
                                           "stop.gif"));

    static int stepLen = 2; //每次下落的步长
    static int STEP_TIMEOUT = 50; //每移动一个像素的间隔时间,以毫秒为单位
    static int COLUMN_COUNT = 10; //分成多少列
    static int GENERATE_INTERVAL = 500; //创建一个新的下落线程的时间间隔,以毫秒为单位

    int colWidth; //每栏的宽度

    volatile int totalCount = 0;
    volatile int rightCount = 0;
    volatile int errorCount = 0;

    volatile char pressKeyChar; //记录按下的按键
    int statusCode = 0; //1运行态、2暂停态 0 停止态

    Random random = new Random(); //随机数

    private static final int ACTION_DRAM_FONT = 1; //画字符
    private static final int ACTION_CLEAR_FONT = 2; //清字符
    public TypeTrainAppletBak()
    {

    }

    //Initialize the applet
    public void init()
    {
        try
        {
            jbInit();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    //Component initialization
    private void jbInit()
        throws Exception
    {
        this.getContentPane().setLayout(borderLayout1);
        infoPnl.setBackground(Color.cyan);
        infoPnl.setLayout(new GridLayout(8, 1));
        this.addKeyListener(new TypeTrainApplet_this_keyAdapter(this));
        jButton1.setIcon(startIcon);
        jButton1.addActionListener(new TypeTrainApplet_jButton1_actionAdapter(this));
        jButton2.setIcon(stopIcon);
        jButton2.addActionListener(new TypeTrainApplet_jButton2_actionAdapter(this));
        infoPnl.add(jButton1);
        infoPnl.add(jButton2);
        infoPnl.add(totalLbl_1);
        infoPnl.add(totalLbl_2);
        infoPnl.add(rightLbl_1);
        infoPnl.add(rightLbl_2);
        infoPnl.add(errorLbl_1);
        infoPnl.add(errorLbl_2);
        getContentPane().add(infoPnl, BorderLayout.EAST);

        canvas.setBackground(Color.yellow);
        canvas.setFocusable(true);
        getContentPane().add(canvas, BorderLayout.CENTER);
    }

    /**
     * <b>功能说明:</b><br>
     * 返回一个随机字符
     * @return 随机字符
     */
    private char getRandomChar()
    {
        int temp = 97 + random.nextInt(26);
        return (char) temp;
    }

    /**
     * <b>功能说明:</b><br>
     * 将统计结果画到界面上
     */
    private void drawResult()
    {
        totalLbl_2.setText("" + totalCount);
        rightLbl_2.setText("" + rightCount);
        errorLbl_2.setText("" + errorCount);
    }

    /**
     * 重置现场
     */
    private void resetGame()
    {
       totalCount = 0;
       rightCount = 0;
       errorCount = 0;
       drawResult();
    }
    //Start the applet
    public void start()
    {
        this.requestFocus();
    }
    /**获取用户点击按键所对应的字符*/
    public void this_keyPressed(KeyEvent e)
    {
        if (!e.isActionKey())
        {
            pressKeyChar = e.getKeyChar();
        }
    }

    public void jButton1_actionPerformed(ActionEvent e)
    {
        if (statusCode == 0) //从结束->开始
        {
            statusCode = 1;
            colWidth = canvas.getWidth() / COLUMN_COUNT;
            GenerateDropThread gdThread = new GenerateDropThread(); //总控线程运行
            gdThread.start();
            jButton1.setIcon(pauseIcon);
            jButton2.setEnabled(true);
        }
        else if (statusCode == 1) //从运行->暂停
        {
            statusCode = 2;
            jButton1.setIcon(startIcon);
        }
        else //从暂停->运行
        {
            statusCode = 1;
            jButton1.setIcon(pauseIcon);
            synchronized (canvas)
            {
                canvas.notifyAll();
            }
        }
        this.requestFocus();
    }

    public void jButton2_actionPerformed(ActionEvent e)
    {
        statusCode = 0;
        synchronized (canvas)
        {
            canvas.notifyAll();
        }
        jButton2.setEnabled(false);
        jButton1.setIcon(startIcon);
        resetGame();
    }

    /**下落字体线程*/
    //////////////////////////////////////////////////////////////////////////////
    public class DropCharThread
        extends Thread
    {
        char c; //对应的字线
        int colIndex; //在哪列下落

        int x, y; //行列的坐标
        public DropCharThread(char c, int colIndex)
        {
            this.c = c;
            this.colIndex = colIndex;
            this.x = (colIndex - 1) * colWidth + colWidth / 2; //所在的横坐标
        }

        public void run()
        {
            draw(ACTION_DRAM_FONT);
            try
            {
                while (c != pressKeyChar && y < 400 && statusCode != 0)
                {
                    synchronized (canvas)
                    {
                        while (statusCode == 2)
                        {
                            canvas.wait();
                        }
                    }
                    move();
                    Thread.sleep(STEP_TIMEOUT);
                }
            }
            catch (InterruptedException ex)
            {
            }

            pressKeyChar = ' ';
            draw(ACTION_CLEAR_FONT);
            if (statusCode != 0)
            {
                totalCount++; //统计总数
                if (y < 400)
                {
                    rightCount++; //打中
                }
                else
                {
                    errorCount++; //打不中
                }
            }
            drawResult();
        }

        /**向下移动一个像素*/
        private void move()
        {
            draw(2);
            y += stepLen;
            draw(1);
        }

        /**
         * <b>功能说明:</b><br>
         * 画字母
         * @param actionType 1:画字母 2: 清除字母
         */
        private void draw(int actionType)
        {
            synchronized (canvas) //必须对资源canvas进行同步,否则会产生线程不安全
            {
                Graphics g = canvas.getGraphics();
                if (actionType == 2)
                {
                    g.setXORMode(canvas.getBackground()); //清除
                }
                g.setFont(new Font("Times New Roman", Font.PLAIN, 12));
                g.drawString("" + c, x, y);
            }
        }
    }

    //////////////////////////////////////////////////////////////////////////////

    /**创建下落字体线程的主线程*/
    //////////////////////////////////////////////////////////////////////////////
    public class GenerateDropThread
        extends Thread
    {
        public void run()
        {
            try
            {
                while (statusCode != 0) //产生下落线程
                {
                    synchronized (canvas)
                    {
                        while (statusCode == 2)
                        {
                            canvas.wait();
                        }
                    }
                    DropCharThread dropCharThread = new DropCharThread(getRandomChar(),
                        random.nextInt(COLUMN_COUNT) + 1);
                    dropCharThread.start();
                    Thread.sleep(GENERATE_INTERVAL);
                }
            }
            catch (InterruptedException ex)
            {
            }
        }
    }
//////////////////////////////////////////////////////////////////////////////

}

class TypeTrainApplet_jButton2_actionAdapter
    implements ActionListener
{
    private TypeTrainAppletBak adaptee;
    TypeTrainApplet_jButton2_actionAdapter(TypeTrainAppletBak adaptee)
    {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e)
    {

        adaptee.jButton2_actionPerformed(e);
    }
}

class TypeTrainApplet_jButton1_actionAdapter
    implements ActionListener
{
    private TypeTrainAppletBak adaptee;
    TypeTrainApplet_jButton1_actionAdapter(TypeTrainAppletBak adaptee)
    {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e)
    {
        adaptee.jButton1_actionPerformed(e);
    }
}

class TypeTrainApplet_this_keyAdapter
    extends KeyAdapter
{
    private TypeTrainAppletBak adaptee;
    TypeTrainApplet_this_keyAdapter(TypeTrainAppletBak adaptee)
    {
        this.adaptee = adaptee;
    }

    public void keyPressed(KeyEvent e)
    {
        adaptee.this_keyPressed(e);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?