Search This Blog

Source Code:Typing Speed Calculator Application in Java using Applet



   1:  /*
   2:   * created by Aditya Vijayvergia
   3:   * uploaded on thecodingedge.blogspot.in
   4:   */
   5:  package typingspeed;
   6:   
   7:  import java.awt.*;
   8:  import java.awt.event.KeyAdapter;
   9:  import java.awt.event.KeyEvent;
  10:  import java.awt.event.KeyListener;
  11:  import java.awt.event.MouseAdapter;
  12:  import java.awt.event.MouseEvent;
  13:  import java.awt.event.WindowAdapter;
  14:  import java.awt.event.WindowEvent;
  15:  import java.util.logging.Level;
  16:  import java.util.logging.Logger;
  17:   
  18:  /**
  19:   *
  20:   * @author Aditya Vijayvergia
  21:   */
  22:   
  23:  public class TypingSpeed
  24:  { 
  25:      private TextArea text;
  26:      private TextField input,time,output;
  27:      //private Button start;
  28:      private Boolean isStarted=false;
  29:      private String in="",s="";
  30:      private int point;
  31:      TypingSpeed()
  32:      {
  33:          s="the is because from indeed given key otherwise problem frame at";
  34:          s+="\npeople class like never nowadays freedom implement fundamental";
  35:          s+="\nthis not done tells speak what is how good that when you say it";
  36:          s+="\nnecessary because might where be there another named speech";
  37:          new SetFrame();
  38:      }
  39:      
  40:  private class SetFrame extends Frame
  41:  {
  42:      private int position=-1;
  43:      private String next="";
  44:      
  45:      
  46:      SetFrame()
  47:      {
  48:          setLayout(new FlowLayout());
  49:          text=new TextArea(6,60);
  50:          output=new TextField(25);
  51:          output.setBackground(Color.yellow);
  52:          //start=new Button("Start");
  53:          time=new TextField(2);
  54:          input=new TextField(25);
  55:          text.setText(s);
  56:          text.setEditable(false);
  57:          time.setEditable(false);
  58:          add(text);
  59:          add(time);
  60:          add(output);
  61:          add(input);
  62:          input.setText("click here to start");
  63:          output.setText("this box shows the required input");
  64:          //add(start);
  65:          setSize(600,300);
  66:          input.addMouseListener(new MouseAdapter() 
  67:          {
  68:               @Override
  69:               public void mousePressed(MouseEvent e) {
  70:                   
  71:                   if(!isStarted)
  72:                   {
  73:                      input.setText("");
  74:                      output.setText("");
  75:                      next=getNext();
  76:                      output.setText(next);
  77:                      Timer t=new Timer();
  78:                      t.start();
  79:                   }
  80:               }
  81:          });
  82:          input.addKeyListener(new KeyAdapter(){
  83:              @Override
  84:              public void keyTyped(KeyEvent e) {
  85:                  if(e.getKeyChar()==' ')
  86:                  {
  87:                      in=input.getText();
  88:                      in=in.trim();
  89:                      input.setText("");
  90:                      if(in.equals(next))
  91:                      {
  92:                          point+=in.length();
  93:                      }
  94:                      next=getNext();
  95:                      output.setText(next);
  96:                  }
  97:              }            
  98:   
  99:          });
 100:           
 101:          addWindowListener(new WindowAdapter(){
 102:              @Override
 103:              public void windowClosing(WindowEvent e)
 104:              {
 105:                  System.exit(0);
 106:              }
 107:          });
 108:          setVisible(true);
 109:          setTitle("Typing Speed Calculator");
 110:          add(new Label("                Created by Aditya Vijayvergia"));
 111:          add(new Label("                Upladed on  thecodingedge.blogspot.in"));
 112:      }
 113:      private String getNext()
 114:      {
 115:         String right="";
 116:          while(s.charAt(++position)!=' ')
 117:              right+=s.charAt(position);
 118:          return right;
 119:      }
 120:      
 121:  }
 122:  private class Timer extends Thread
 123:  {
 124:     private float wpm;
 125:      @Override
 126:      public void run() {
 127:          //input.setVisible(true);
 128:          for(int i=60;i>0;i--)
 129:                  {
 130:                      
 131:                      isStarted=true;
 132:                      time.setText(i+"");
 133:                      try {
 134:                          Thread.sleep(1000);
 135:                      } catch (InterruptedException ex) {
 136:                          Logger.getLogger(TypingSpeed.class.getName()).log(Level.SEVERE, null, ex);
 137:                      }
 138:                  }
 139:          time.setText(0+"");
 140:          output.setText("Time Up");
 141:          input.setEditable(false);
 142:          input.setBackground(Color.yellow);
 143:          wpm=(float)point/5;
 144:          input.setText("your speed = "+wpm+" wpm");
 145:      }
 146:  }
 147:      /**
 148:       * @param args the command line arguments
 149:       */
 150:   
 151:      public static void main(String[] args) {
 152:          // TODO code application logic here
 153:          new TypingSpeed();   
 154:      }
 155:  }

No comments:

Post a Comment