Share
Explore BrainMass

Java memory game program

I am stumped with this java driver program. I have been able to obtain parts of the first sample session. Please see my progress below.

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

public class MemoryProg extends JFrame
{
private static final int WIDTH = 350;
private static final int HEIGHT = 200;
private JTextField nameBox;
//**************************************************************************

public MemoryProg()
{
setTitle("Message");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
}
//**************************************************************************

public void createContents()
{
JOptionPane.showInputDialog("Enter color number 1:");
nameBox = new JTextField(15);
add (nameBox);

}
//**************************************************************************
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "How good is your memory?
n" +
"Try to memorize this color sequence: nn" +
"red white yellow green blue");
new MemoryProg7();

}
//*************************************************************************
}//end MemoryProg

Attachments

Solution Preview

Here is the code. Compiled and tested in NetBeans. Good luck and thanks for using Brainmass.com!

Instructions:
1) Copy the source code below into a text file and name it MemoryProg7.java
2) Compile the code - you can use javac.exe -g MemoryProg7.java
3) Run the program - java.exe MemoryProg7

Code:

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

public class MemoryProg7 extends JApplet {

private static final int WIDTH = 350;
private static final int HEIGHT = 200;

JLabel colLabel=new JLabel("Enter Color Number 1: ");
JTextField colText=new JTextField(8);
JButton butHint=new JButton("Hint");

int colorIntake = 1;

public void init() {

//Hint button event listener
butHint.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
...

Solution Summary

A java program with a color memory game. When launched you will see a question "How good is your memory? try to memorize following color sequence". Then the form will appear where the user can enter his guess. If the user correctly enters all the colors, the program prints a "Congratulations" message.

$2.19