Saturday, February 27, 2016

Simple Typing Game

This program shows the use of DocumentListener to listen for changes in a textfield. Three different words will appear on the screen and user needs to type them all one by one. When a correct word is typed, the corresponding word will dissappear and a variable named "score" will be added by 1. The variable "displayed" is used to count the number of words that have been displayed on the screen.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.util.*;

public class TypingTutor extends JFrame{
    JButton b=null;
    JPanel p=null;
    JTextField tf=null;
    JLabel l=null;
    JLabel l2=null;
    int score=0;
    int displayed=0;
    ArrayList<String>  wordlist;
    WordRun word1=null;
    WordRun word2=null;
    WordRun word3=null;
    
    public TypingTutor(){
        super("Typing Tutor");
        setSize(400,400);
        setLayout(new BorderLayout());
        
        b=new JButton("Start");
        b.addActionListener(new ButtonHandler());
        add(b,BorderLayout.NORTH);
        
        p=new DrawPanel();
        p.setPreferredSize(new Dimension(300,300));
        add(p,BorderLayout.CENTER);
        
        JPanel p2=new JPanel();
        p2.setLayout(new FlowLayout());
        
        tf=new JTextField(10);
        tf.getDocument().addDocumentListener(new ListenText());
        p2.add(tf);

        l=new JLabel("0");
        p2.add(l);
        
        l2=new JLabel("0");
        p2.add(l2);
        
        add(p2,BorderLayout.SOUTH);
        
        wordlist=new ArrayList<String>();
        wordlist.add(0,"abandon");
        wordlist.add(1,"abate");
        wordlist.add(2,"abbreviate");
        wordlist.add(3,"abdicate");
        wordlist.add(4,"aberration");
        wordlist.add(5,"abhorrence");
        wordlist.add(6,"ability");
        wordlist.add(7,"ablaze");
        wordlist.add(8,"abnormal");
        wordlist.add(9,"aboard");
        
        word1=new WordRun();
        word2=new WordRun();
        word3=new WordRun();
        
        word1.st=null;
        word2.st=null;
        word3.st=null;
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    class DrawPanel extends JPanel{
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2=(Graphics2D)g;
            
            Font f=new Font("Monospaced",Font.BOLD,14);
            FontMetrics fm=this.getFontMetrics(f);
            g2.setColor(Color.BLACK);
            g2.setFont(f);
            if(word1.st!=null){
                g2.drawString(word1.st,word1.posx,word1.posy);
            }
            if(word2.st!=null){
                g2.drawString(word2.st,word2.posx,word2.posy);
            }
            if(word3.st!=null){
                g2.drawString(word3.st,word3.posx,word3.posy);
            }
        }
    }
    class WordRun implements Runnable{
        String st=null;
        int posx;
        int posy;
        
        public void run(){
            try{
                Random rand=null;
                while(true){
                    if((st==null)||(posy==p.getHeight())){
                        rand=new Random();
                        Thread.currentThread().sleep(rand.nextInt(1000));
                        st=wordlist.get(rand.nextInt(10));
                        posy=0;
                        do{
                            posx=rand.nextInt(p.getWidth());
                        }while(posx>(p.getWidth()-100));
                        tf.setText("");
                        displayed++;
                        l2.setText(displayed+"");
                    }
                    else{
                        Thread.currentThread().sleep(20);
                        posy++;
                    } 
                    repaint();
                }
            }catch(Exception e){
            }    
        }
    }
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand()=="Start"){
                Thread t1=new Thread(word1);
                Thread t2=new Thread(word2);
                Thread t3=new Thread(word3);
                t1.start();
                t2.start();
                t3.start();
            }
        }
    }
    class ListenText implements DocumentListener{
        public void changedUpdate(DocumentEvent e) {
        }
        public void removeUpdate(DocumentEvent e) {    
        }
        public void insertUpdate(DocumentEvent e) {
            if(tf.getText().equals(word1.st)){
                word1.st=null;
                word1.posy=0;
                score++;
                l.setText(score+"");
            }
            if(tf.getText().equals(word2.st)){
                word2.st=null;
                word2.posy=0;
                score++;
                l.setText(score+"");
            }
            if(tf.getText().equals(word3.st)){
                word3.st=null;
                word3.posy=0;
                score++;
                l.setText(score+"");
            }
        }
    }
    public static void main(String[] args){
        TypingTutor ap=new TypingTutor();
    }
}

3 comments:

  1. i cannot run it on java, help me please :'(

    ReplyDelete
  2. create a TypingTutor class without a main method and just copy and paste this code into the brackets

    ReplyDelete