/* Typing Speed Tester Game
   RJM Programming
   August, 2014 */
   

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.math.BigDecimal;


public class TypingSpeed extends JFrame implements ActionListener, KeyListener {
  Date start, end;
  int score = 0, goes = 0;
  int totalwords = 0, totalsecs = 0, totalletters = 0;
  long offset = 0;
  String expected = " ", instructions = " ", text = "", ptext = "", lastc = "x";
  boolean pausing = true;
  JPanel p;
  JLabel label1;
  JTextArea plabel;
  JTextArea tbox1;
  JButton leftbutton, rightbutton;
  FontMetrics fontm;
  int x = 0, y = 0;
  
  public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame ourf = new TypingSpeed();
                ourf.setVisible(true);
            }
        });

  }
  
  public TypingSpeed() {
     initUI();
  }
  
  
  public final void initUI() {
     label1 = new JLabel("");
     tbox1 = new JTextArea(12,80);

     leftbutton = new JButton("Clear");
     leftbutton.setOpaque(true);
     leftbutton.setBackground(Color.RED);
     leftbutton.addActionListener(this);
     rightbutton = new JButton("Ready for Typing Below ...");
     rightbutton.setOpaque(true);
     rightbutton.setBackground(Color.YELLOW);
     rightbutton.addActionListener(this);
     
     p = new JPanel();
     plabel = new JTextArea(""); 
     p.add(plabel);
     
     add(leftbutton, "East");
     add(label1, "North");
     add(tbox1, "South");
     add(p, "Center");
     add(rightbutton, "West");
     
     tbox1.addKeyListener(this);
     tbox1.requestFocus();

     setTitle("Find Out How Fast You Type");
     setPreferredSize(new Dimension(800,600));
     setMinimumSize(getPreferredSize());
     setResizable(true);
     setLocationRelativeTo(null);
     setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
  
  public void huh_paintComponent(Graphics g) {
     Font ourf;
     int oursz = 42;
     String fontn = "Courier";
     int fontt = Font.PLAIN;
     
     ourf = new Font(fontn, fontt, oursz);
     g.setFont(ourf);
     
     fontm = getFontMetrics(ourf);
     x = (getSize().width - fontm.stringWidth(ptext)) / 2;
     y = (getSize().height + fontm.getHeight()) / 2;
     
     g.drawString(ptext, x, y);
  }
  
  public void actionPerformed(ActionEvent e) {
     if (e.getSource() == leftbutton) {
       text = "";
       ptext = "";
       label1.setText(text);
       tbox1.setText(text);
       setTitle("Clearing ... Ready for More ...");
       totalwords = 0;
       totalsecs = 0; 
       totalletters = 0;
       offset = 0;
       pausing = true;
     } else if (e.getSource() == rightbutton && pausing) {
       offset += ((end.getTime() - start.getTime()) / 1000);
       if (lastc.indexOf(".") != -1) {
        long seconds = ((end.getTime() - start.getTime()) / 1000) + offset;
        BigDecimal ourbd = new BigDecimal((double)((double)(totalwords + 1) / ((double)seconds / 60.0)));
        ourbd = ourbd.setScale(1, ourbd.ROUND_HALF_UP);
        String ours = ourbd.toString();
        setTitle("Over " + String.valueOf(seconds) + " seconds, your " + totalletters + " letters, for " + (totalwords + 1) + " words, represents typing speed of " + ours + " words per minute");
        ptext = "\n\n\n\n\n\n\n\n\n\n\n              " + ours + " words/minute          \n\n\n\n\n\n\n\n\n\n";
        plabel.setText(ptext);
       }
       String vs = label1.getText();
       if (vs.substring(vs.length() -1) != lastc) {
        if (text.length() >= 90) {
         label1.setText(" ... " + (vs + lastc).substring((vs + lastc).length() - 90));
        } else {
         label1.setText((vs + lastc));
        }
       }
       pausing = false;
       rightbutton.setText("Restart");
       rightbutton.setBackground(Color.GREEN);
       setTitle("Pausing ... " + this.getTitle());
     } else if (e.getSource() == rightbutton) {
       start = new Date();
       pausing = true;
       rightbutton.setText("Pause");
       rightbutton.setBackground(Color.YELLOW);
       setTitle(this.getTitle().replace("Pausing ... ", ""));
     }
  }
  
  
  public void keyTyped(KeyEvent e) {
     if (text == "") {
       rightbutton.setText("Pause");
       rightbutton.setBackground(Color.YELLOW);
       start = new Date();
     } else if (rightbutton.getText() == "Restart") {
       start = new Date();
       pausing = true;
       rightbutton.setText("Pause");
       rightbutton.setBackground(Color.YELLOW);
     }
     totalletters++;
     lastc = String.valueOf(e.getKeyChar());
     if (e.getKeyChar() == 32) totalwords++;
     if (e.getKeyChar() == 10) totalwords++;
     text = text + e.getKeyChar();
     end = new Date();
     long seconds = ((end.getTime() - start.getTime()) / 1000) + offset;

     if (seconds > 0) {
       BigDecimal ourbd = new BigDecimal((double)((double)totalwords / ((double)seconds / 60.0)));
       ourbd = ourbd.setScale(1, ourbd.ROUND_HALF_UP);
       String ours = ourbd.toString();
       setTitle("Over " + String.valueOf(seconds) + " seconds, your " + totalletters + " letters, for " + totalwords + " words, represents typing speed of " + ours + " words per minute");
       ptext = "\n\n\n\n\n\n\n\n\n\n\n              " + ours + " words/minute          \n\n\n\n\n\n\n\n\n\n";
       plabel.setText(ptext);
       //p.repaint();
     }
  }
  
  
  public void keyPressed(KeyEvent e) {
     if (text.length() >= 90) {
       label1.setText(" ... " + text.substring(text.length() - 90));
     } else {
       label1.setText(text);
     }
  }
  
  
  public void keyReleased(KeyEvent e) {
  }
  
  

}


