Brittni77 Brittni77 - 28 days ago 6
Java Question

JAVA Simon Game

I need to finish up this simon game for my beginner Java class. Basically the game needs to randomize 3 of the COLOR letters on the top label "R", "B", "Y" or "G" to begin playing the game. The player will press the colors in this sequence and if they are correct it will add another letter/color if they are wrong they will lose. I have the main part of the game set up including color ect... What I am having a hard time with is printing the letters of the buttons pushed on the bottom label and also getting the top label to randomize 3 to start with. I can get the game to print random letters on the top label when a button is pushed but it wont do this own its own to start and it wont add the letters on?

HERE IS MY CODE:
//10/30/16
package simongame1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.security.SecureRandom;

public class SimonGame1 extends JFrame implements ActionListener
{
public SecureRandom rand;
public String randomLetters="";
JButton[] numberButtons = new JButton[4];
JPanel numberButtonPanel = new JPanel();
JLabel labelTop;
JLabel labelBottom;
String messageTop = "";
String messageBottom = "";
JButton red;
JButton green;
JButton yellow;
JButton blue;
public final String letters= "RGBY";
public JLabel labelDisplay;


//constructor?
public SimonGame1()
{


super("Simon Game!");
labelDisplay = new JLabel ("");
setLayout(new BorderLayout());
red = new JButton();
green = new JButton();
blue = new JButton();
yellow = new JButton();
labelTop = new JLabel("");
//getRandomLetter();
labelBottom = new JLabel("");
//System.out.print (randomLetters);
rand = new SecureRandom();


numberButtons = new JButton[4];
numberButtonPanel = new JPanel();
numberButtonPanel.setLayout(new GridLayout(3,3));


for(int i = 0; i < 0; i++)
{
numberButtons[i] = new JButton();
numberButtonPanel.add(numberButtons[i]);
numberButtons[i].addActionListener(this);
}
numberButtonPanel.add(red);
red.setBackground(Color.red);
red.addActionListener(this);

numberButtonPanel.add(blue);
blue.setBackground(Color.blue);
blue.addActionListener(this);

numberButtonPanel.add(yellow);
yellow.setBackground (Color.yellow);
yellow.addActionListener(this);

numberButtonPanel.add (green);
green.setBackground (Color.green);
green.addActionListener(this);

add(labelTop, BorderLayout.NORTH);
add(labelBottom, BorderLayout.SOUTH);

add(numberButtonPanel, BorderLayout.CENTER);


//if (messageTop = messageBottom)


}
// TODO code application logic here

@Override
public void actionPerformed(ActionEvent e)
{
rand = new SecureRandom();

randomLetters+= getRandomLetter();
labelTop.setText (randomLetters);
labelBottom.setText (letters);

System.out.println(e.getID());
if(e.getSource() == red)
{
System.out.println("R");
}
else if (e.getSource()==blue)
{
System.out.println ("B");
}
else if (e.getSource()==yellow)
{
System.out.println ("Y");
}
else if (e.getSource()==green)
{
System.out.println ("G");
}

}


public char getRandomLetter()
{

char randChar = letters.charAt(rand.nextInt (4));
return randChar;
}
}

AND HERE IS THE TEST
package simongame1;
import javax.swing.JFrame;
public class NewClass {
public static void main(String[] args) {
{
SimonGame1 simpleFrame = new SimonGame1();
simpleFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
simpleFrame.setSize(300, 300);
simpleFrame.setVisible(true);
}

}
}


WHY IS THIS ONE CLICK BEHIND? If i click the red button it doenst show anything but if i click the blue one after the red thats when it prints "R" finally...

labelBottom.setText (messageBottom);
**//WHY IS IT ONE LETTER BEHIND?**
System.out.println(e.getID());
if(e.getSource() == red)
{
messageBottom += "R";
//System.out.println("R");
}
else if (e.getSource()==blue)
{
messageBottom += "B";
//System.out.println ("B");
}
else if (e.getSource()==yellow)
{
messageBottom += "Y";
//System.out.println ("Y");
}
else if (e.getSource()==green)
{
messageBottom += "G";
//System.out.println ("G");
}

Answer

When you initialize your label, before you add it to the panel I would just generate the label with the letters you need and set the text of the label right there. That way when you add it you will for sure have the characters in there. Right now you are only setting the text of labelTop when the actionPerformed method is called, which is only called when a button is pressed.

So it should look something like:

public SimonGame1() {
     //....

     rand = new SecureRandom();
     labelTop = new JLabel( "" );

     //initialize with 3
     for(int i = 0; i < 3; i++)
     {
         randomLetters += getRandomLetter();
     } 
     labelTop.setText( randomLetters );

     //...

     add(labelTop, BorderLayout.NORTH);

}

also this block of code is not used at all because the condition is never met, since you initialize i to 0 and then say as long as it is less than 0:

for(int i = 0; i < 0; i++)
{
       numberButtons[i] = new JButton();
       numberButtonPanel.add(numberButtons[i]);
       numberButtons[i].addActionListener(this);
}

EDIT:

since you added another part of the question, you asked why it is waiting an iteration before adding the character you just clicked in your actionPerformed method, and that is because you are setting the text before you add the next character to the string. So your actionPerformed method should look like:

public void actionPerformed(ActionEvent e)
{

    if(e.getSource() == red)
    {
            messageBottom += "R";
            //System.out.println("R");
    }
    else if (e.getSource()==blue)
    {
         messageBottom += "B";
         //System.out.println ("B");
    }
    else if (e.getSource()==yellow)
    {
        messageBottom += "Y";
        //System.out.println ("Y");
    }
    else if (e.getSource()==green)
    {
        messageBottom += "G";
        //System.out.println ("G");
    }

    labelBottom.setText (messageBottom);
}
Comments