Explore BrainMass
Share

Java memory game program

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

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

© BrainMass Inc. brainmass.com October 25, 2018, 2:04 am ad1c9bdddf
https://brainmass.com/computer-science/memory-management/java-memory-game-program-285145

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
See Also This Related BrainMass Solution

Java Tests the User's Ability to Memorize a Sequence of Colors

See the attachment.
Assist with writing a Java program that tests the user's ability to memorize a sequence of colors. As shown in the sample session, the program starts off by displaying a dialog box with a list of colors that are to be memorized - red, white, yellow, green, and blue. The user then enters the colors one at a time in a text box. If the user makes a mistake, the program prints a "Sorry" message. If the user correctly enters all the colors, the program prints a "Congratulations" message. Note that when the sorry or congratulations message is printed, the window's original components get cleared away.

Also provide a hint button that causes the current color's first letter to appear in the text box. For example, since the first color is red, the first hint should be r. The hint button must cause focus to be put on the text box (i.e., the cursor should appear within the text box without the user having to click there with the mouse). To cause focus to be put on a component, use the requestFocusInWindow method.

View Full Posting Details