User Tools

Site Tools


memoryapp

MemoryApp

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class MemoryApp extends JPanel implements KeyListener, ActionListener
{
    public static int WIDTH=1200;
    public static int HEIGHT=600;
    private Font titleFont, bigFont;
    private int x,y;
    private Timer flashTimer, timer2;
    private char ch;
    private String message, userInput;
    private String sequence;


    public MemoryApp() 
    {

        //initialize variables here...
        titleFont = new Font("Roman", Font.BOLD, 18);
        bigFont = new Font("Helvetica", Font.PLAIN, 32);

        flashTimer = new Timer(1000, this); //1000=1 seconds

        flashTimer.start();

        int r = (int)(26*Math.random());
        ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(r);
        message = "Next Character is "+ch;
        sequence = ""+ch;
        userInput ="";
    }

    public static void main(String[] args) {
        MemoryApp app= new MemoryApp();
        JFrame window = new JFrame("Memory Example");
        window.setSize(WIDTH, HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(app);
        window.addKeyListener(app);
        //window.pack();
        window.setVisible(true);

    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(),getHeight());
        g.setColor(Color.BLUE);
        g.setFont(titleFont);
        g.drawString("Memory 0.1", 20, 20);
        g.setColor(Color.BLACK);
        g.setFont(bigFont);
        g.drawString(userInput, 30, 100);
        g.drawString(message, 30, HEIGHT/2);
        g.fillRect(x, y, 10, 10);

    }

    // These 3 methods need to be declares to implement the KeyListener Interface
    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {
        char keyChar = e.getKeyChar();
        String s = "" + keyChar;
        String letter = s.toUpperCase();
        userInput += letter;
        if(userInput.length() < sequence.length())
        {
            message = userInput + "...keep going";
        } else if (userInput.equals(sequence)) {// right
            message="Thats right";
            int r = (int)(26*Math.random());
            String next = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".substring(r, r+1);
            sequence += next;
            message = "That's right, the Next Character is "+next;
            flashTimer.restart();
        } else {
            int r = (int)(26*Math.random());
            ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(r);
            userInput="";
            sequence = ""+ch;
            message = "No,the new Sequence starts with "+ch;
            flashTimer.restart();
        }
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource()==flashTimer){
            message = "Now type the sequence";
            flashTimer.stop();

        } else {
            timer2.stop();
            int r = (int)(26*Math.random());
            ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(r);
            message = "try Again.  The new Letter is "+ch;
            flashTimer.restart();;
        }
        repaint();
    }

}
memoryapp.txt · Last modified: 2022/05/13 12:52 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki