Thursday, 4 July 2013

Alphabets Typing Game

Alphabets Typing Game Java Program

Java Code:


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


public class AlphabetPanel extends JPanel
{
       Text[] alphabet = new Text[26];
      
       public AlphabetPanel()
       {
              this.setLayout(new FlowLayout());
              for (char letter = 'A'; letter <= 'Z'; letter++)
              {
                     alphabet[letter - 'A'] = new Text(String.valueOf(letter));
                     this.add(alphabet[letter - 'A']);
              }
             
              this.setFocusable(true);
              this.addKeyListener(new KeyAdapter() {
       public void keyTyped(KeyEvent e)
              {
                     if (e.getKeyChar() == 32){
                           reset();
                     }
                     char letter = Character.toUpperCase(e.getKeyChar()); // convert the letter to upper case
                     if (letter < 'A' || letter > 'Z') return; // ignore invalid characters
                     if (isVovel(letter))
                     {
                           setLetterColor(letter, Color.green);
                     }
                     else
                     {
                           setLetterColor(letter, Color.red);
                     }}});
       }

       // resets all characters to black
       public void reset()
       {
              for (int i = 0; i < alphabet.length; i++)
              {
                     alphabet[i].setLetterColor(Color.black);
              }
       }
       // checks if a letter has been seen
       public boolean hasLetterBeenSeen(char letter)
       {
              return getLetterColor(letter) != Color.black;
       }
       // checks if the letter is a Vovel
       public boolean isVovel(char letter)
       {
              if (letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U') return true;
              return false;
       }
       // sets a specific letter to a given color
       public void setLetterColor(char letter, Color color)
       {
              alphabet[letter - 'A'].setLetterColor(color);
       }
      
       // retrieves the current color of a specific letter
       public Color getLetterColor(char letter)
       {
              return alphabet[letter - 'A'].getLetterColor();
       }
      
      
      
public static void main(String[] args)
       {
              JFrame frame = new JFrame ("Letter");
              frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
              frame.getContentPane().add(new AlphabetPanel()); // add the alphabet panel to the frame
              frame.getContentPane().setBackground(Color.white);
              frame.pack();
              frame.setVisible(true);
       }

}
import java.awt.*;

import javax.swing.*;

public class Text extends JPanel {

       public static final int space = 2;
       private Color LetterColor = Color.black;
       private Color UnderlineColor = Color.black;
       private String text;
       private boolean Underline = true;
       private boolean Visible = true;
       private Font Font;
      
      
       // Constructor that Initializes the text, sets the Font, and sets the value
      
       public Text(String T) {
              text = T;
              Font = new Font("new", Font.ITALIC, 25);
              this.setPreferredSize(new Dimension(40,40));
       }
      
        // Sets the text to be displayed by this object.
      
       public void setText(String a) {
              text = a;
              repaint();
       }
      
      
        // Returns the text string displayed by this text
        
      
       public String getText() {
              return text;
       }
      
      
        //Returns true if the text is whitespace consisting of
      
       public boolean isWhitespace() {
              return text == null || text.trim().equals("");
       }


      
        // text to be shown the next time
        
       public void showText() {
              Visible = true;
              repaint();
       }
      
       // text to be hidden the next time
        
       public void hideText() {
              Visible = false;
              repaint();
       }
      
      
        // Returns if the text will be shown the next time
        
       public boolean isTextVisible() {
              return Visible;
       }
      
      
       // Underline to be shown the next time
        
       public void showUnderline() {
              Underline = true;
              repaint();
       }
      
      
       // Underline to be hidden the next time
        
       public void hideUnderline() {
              Underline = false;
              repaint();
       }
      
       // Returns true if the Underline will be shown the next
      
       public boolean isUnderlined() {
              return Underline;
       }
               
       public Color getLetterColor() {
              return LetterColor;
       }

      
       // Sets the display color used when drawing the text.
        
       public void setLetterColor(Color LetterColor) {
              this.LetterColor = LetterColor;
              repaint();
       }
        
       public Color getUnderlineColor() {
              return UnderlineColor;
       }


       // Sets the display color used when drawing the
      
       public void setUnderlineColor(Color UnderlineColor) {
              this.UnderlineColor = UnderlineColor;
              repaint();
       }
      

       // Draws the text panel to the screen based on the
        
       public void paintComponent(Graphics g) {
              super.paintComponent(g);
              g.setFont(Font);
             
              int w = this.getWidth();
              int h = this.getHeight();
             
              if (Visible) {
                     g.setColor(LetterColor);
                     g.drawString(text, space, 35);
              }
             
              if (Underline) {
                     g.setColor(UnderlineColor);
                     g.fillRect(space, h - space, (text.length()+1)*7, h);
              }
       }
}

 Snapshots:

 

No comments:

Post a Comment