⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typetrainapplet.java

📁 Applet小游戏 附件是我自己做的一个打字游戏,其中用到了java的多线程, 打开play.html 即可看到游戏画面. 其源代码中的四个applet 参数: <param
💻 JAVA
字号:
package game;import java.awt.*;import java.awt.event.*;import java.applet.*;import java.util.Random;import javax.swing.*;public class TypeTrainApplet extends JApplet {    private boolean isStandalone = false;    boolean isClose = false;    AudioClip hitSound;//惡柧壒澚\u5BF9徾    ImageIcon startIcon = new ImageIcon(TypeTrainApplet.class.getResource("start.jpg"));    ImageIcon pauseIcon = new ImageIcon(TypeTrainApplet.class.getResource("pause.jpg"));    ImageIcon stopIcon = new ImageIcon(TypeTrainApplet.class.getResource("stop.jpg"));    volatile int totalCount = 0;//totalNumber Counter    volatile int rightCount = 0;//rightNumber Counter    volatile int errorCount = 0;//errorNumber Counter    int stepLen;    int stepInterval;    int columNum;    int createInterval;    int colWidth;    volatile char pressKeyChar;    int statusCode = 0; //\u8BB0\u5F55熰\u620F強\u5904揑忬\u6001丆懘拞1丗\u8FD0峴\u6001丄丆2丗\u6682掆\u6001 0丗掆巭\u6001丅    private JPanel infoPnl = new JPanel();    private GridLayout gridLayout1 = new GridLayout();    private JButton jButton1 = new JButton();    private JButton jButton2 = new JButton();    private JLabel totalLbl_1 = new JLabel();    private JLabel totalLbl_2 = new JLabel();    private JLabel rightLbl_1 = new JLabel();    private JLabel errorLbl_2 = new JLabel();    private JLabel rightLbl_2 = new JLabel();    private JLabel errorLbl_1 = new JLabel();    private Canvas canvas = new Canvas();    //Get a parameter value    public String getParameter(String key, String def) {        return isStandalone ? System.getProperty(key, def) :            (getParameter(key) != null ? getParameter(key) : def);    }    //Construct the applet    public TypeTrainApplet() {    }    //Initialize the applet    public void init() {        hitSound = getAudioClip( (TypeTrainApplet.class).getResource("click1.wav"));//弶巒壔壒澚\u5BF9徾        try {            stepLen = Integer.parseInt(this.getParameter("stepLen", "2"));        }        catch(Exception e) {            e.printStackTrace();        }        try {            stepInterval = Integer.parseInt(this.getParameter("stepInterval", "50"));        }        catch(Exception e) {            e.printStackTrace();        }        try {            columNum = Integer.parseInt(this.getParameter("columNum", "10"));        }        catch(Exception e) {            e.printStackTrace();        }        try {            createInterval = Integer.parseInt(this.getParameter("createInterval", "500"));        }        catch(Exception e) {            e.printStackTrace();        }        try {            jbInit();        }        catch(Exception e) {            e.printStackTrace();        }    }    //Component initialization    private void jbInit() throws Exception {        this.setSize(new Dimension(400, 400));        this.addKeyListener(new java.awt.event.KeyAdapter() {            public void keyReleased(KeyEvent e) {                this_keyReleased(e);            }        });        infoPnl.setLayout(new GridLayout(8, 1));        jButton1.setIcon(startIcon);        jButton1.setBackground(Color.white);        jButton1.setToolTipText("Start/Pause");        jButton1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(ActionEvent e) {                jButton1_actionPerformed(e);            }        });//\u8BBE抲\u5F00巒埪\u94AE揑\u56FE\u6807        jButton2.setIcon(stopIcon);        jButton2.setToolTipText("Stop");        jButton2.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(ActionEvent e) {                jButton2_actionPerformed(e);            }        });        jButton2.setBackground(Color.white);        jButton2.setEnabled(false);//       // jButton1.setText("jButton1");       // jButton2.setText("jButton2");       // jButton3.setText("jButton3");        totalLbl_1.setForeground(new Color(131, 72, 105));        totalLbl_1.setText("鉦澤");        totalLbl_2.setForeground(new Color(131, 79, 105));        totalLbl_2.setText("jLabel2");        rightLbl_1.setForeground(new Color(118, 72, 105));        rightLbl_1.setText("惓妋澤");        errorLbl_2.setForeground(new Color(118, 72, 105));        errorLbl_2.setText("jLabel4");        rightLbl_2.setForeground(new Color(118, 72, 105));        rightLbl_2.setText("jLabel5");        errorLbl_1.setForeground(new Color(118, 72, 105));        errorLbl_1.setText("幐攕澤");        canvas.setBackground(new Color(197, 217, 158));        canvas.setForeground(new Color(112, 85, 217));        infoPnl.setBackground(new Color(203, 243, 164));        infoPnl.setBounds(0,0,24,400);        this.getContentPane().add(infoPnl,  BorderLayout.EAST);        infoPnl.add(jButton1, null);        infoPnl.add(jButton2, null);        infoPnl.add(totalLbl_1, null);        infoPnl.add(totalLbl_2, null);        infoPnl.add(rightLbl_1, null);        infoPnl.add(rightLbl_2, null);        infoPnl.add(errorLbl_1, null);        infoPnl.add(errorLbl_2, null);        this.getContentPane().add(canvas, BorderLayout.CENTER);    }    // reDisplay the result    private void drawResult() {        totalLbl_2.setText("" + totalCount );        rightLbl_2.setText("" + rightCount );        errorLbl_2.setText("" + errorCount );    }    // clear counters    private void resetGame() {        totalCount = 0;        rightCount = 0;        errorCount = 0;        drawResult();    }    //Start the applet    public void start() {        isClose = false;    }    //Stop the applet    public void stop() {        statusCode = 0;//掆巭熰\u620F        isClose = true;    }    //Destroy the applet    public void destroy() {    }    //Get Applet information    public String getAppletInfo() {        return "Applet Information";    }    //Get parameter info    public String[][] getParameterInfo() {        String[][] pinfo =            {            {"stepLen", "int", ""},            {"stepInterval", "int", ""},            {"columNum", "int", ""},            {"createInterval", "int", ""},            };        return pinfo;    }    //static initializer for setting look & feel    static {        try {            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());            //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());        }        catch(Exception e) {        }    }    private class DropCharThread extends Thread {        char c; //the player iputted char        int colIndex;        int x, y;        private static final int ACTION_DRAW_FONT = 1; //夋帤晞        private static final int ACTION_CLEAR_FONT = 2; //惔帤晞        public DropCharThread(char c, int colIndex) {            this.c = c;            this.colIndex = colIndex;            this.x = (colIndex - 1) * colWidth + colWidth / 2;        }        public void run() {            if(isClose) return ;//\u53D1\u73B0鈞岥\u5173\u95ED\u9A6C忋曉夞            draw(ACTION_DRAW_FONT);            try {                while (c != pressKeyChar && y < canvas.getHeight() && statusCode != 0) {                    synchronized (canvas) {                        while (statusCode == 2) {                            canvas.wait();                        }                    }                    draw(ACTION_CLEAR_FONT);                    y += stepLen;                    draw(ACTION_DRAW_FONT);                    Thread.sleep(stepInterval);                }            } catch (InterruptedException ex) {                ex.printStackTrace();            }            if(!isClose) {                pressKeyChar = ' ';                draw(ACTION_CLEAR_FONT);                if (statusCode != 0) {//熰\u620F杤桳掆巭                    totalCount++; //\u7EDF\u8BA1\u603B悢                    if (y < canvas.getHeight()) {                        hitSound.play();//\u51FB拞\u65F6攄曻壒澚                        rightCount++; //\u51FB拞                    } else {                        errorCount++; //懪晄拞                    }                    drawResult();                }            }        }        private void draw( int actionType ) {            synchronized( canvas ) {                Graphics g = canvas.getGraphics();                if ( actionType == ACTION_CLEAR_FONT ) {                    g.setXORMode(canvas.getBackground());                }                g.setFont(new Font("Times New Roman", Font.PLAIN, 20));                g.drawString("" + c, x, y);            }        }    }    private class GenerateDropThread extends Thread {        Random random = new Random(); //悘婘悢        public void run() {            try {                while(statusCode!=0){                    synchronized(canvas){                        while(statusCode==2){                            canvas.wait();                        }                    }                    DropCharThread dropCharThread = new DropCharThread(                         getRandomChar(),random.nextInt(columNum) + 1);                    dropCharThread.start();                    Thread.sleep(createInterval);                }            } catch (InterruptedException ex) {                ex.printStackTrace();            }        }        private char getRandomChar() {            int temp = 65 + random.nextInt(26);            return (char) temp;        }    }    public void this_keyReleased(KeyEvent e) {        if (!e.isActionKey()) {            int iChar;            iChar = (int)e.getKeyCode();            if( iChar > 90 ) {                iChar = iChar - 32;            }            pressKeyChar = (char)iChar;        }    }    public void jButton1_actionPerformed(ActionEvent e) {        if(statusCode == 0) {            resetGame();            statusCode = 1;            colWidth = canvas.getWidth() / columNum ;            GenerateDropThread gdThread = new GenerateDropThread();            gdThread.start();//\u4EA7惗婍\u542F\u52A8            jButton1.setIcon(pauseIcon);//愗\u6362\u4E3A\u6682掆揑\u56FE\u6807            jButton2.setEnabled(true);//掆巭埪\u94AE寖妶        } else if (statusCode == 1) { //樃\u8FD0峴-亜\u6682掆            statusCode = 2;            jButton1.setIcon(startIcon);        } else { //樃\u6682掆亅亜\u8FD0峴            statusCode = 1;            jButton1.setIcon(pauseIcon);//愗\u6362\u4E3A\u6682掆揑\u56FE\u6807            synchronized (canvas) {//捠\u8FC7canvas捠抦強桳\u6682掆揑\u7EBF掱\u7EE7\u7EED\u8FD0峴                canvas.notifyAll();            }        }        this.requestFocus();//Applet愙庴岝\u6807丆埲曋懘愙庴埪\u952E帠審    }    void jButton2_actionPerformed(ActionEvent e) {        statusCode = 0;        synchronized (canvas) {            canvas.notifyAll();        }        jButton2.setEnabled(false);        jButton1.setIcon(startIcon);    }    public static void main( String[] args){        TypeTrainApplet tta = new TypeTrainApplet();        tta.init();        tta.start();    }}

⌨️ 快捷键说明

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