//I ended up not using the online example at all, it was only making it more difficult
//this is my doing from top to bottom
package isay;
import javax.swing.*;//using this for the delay on the gameplay
import java.awt.*;//used for grid
import java.awt.event.*;//used for event listener
import java.util.Random;//using this for the random button selector
/**
*
* @author Sir Rudy Typealot
*/
publicclass ISay extends JFrame
{
private final int windowTall=700;//height of window in pixels
private final int windowWide=700;//width of window in pixels
private boolean Blue=(false);
private boolean Red=(false);
private boolean Green=(false);
private boolean Yellow=(false);
private JButton[] ColorButton;
private JPanel[] BtnLayout;
private JButton sequencer=new JButton();
Dimension btnSize= new Dimension (125, 125);
public ISay()
{
setTitle("I Say Let's Play!!!");//sets the title of the game
setSize(windowWide, windowTall);//sets the dimensions of window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//closes the window when x button is pressed
//this for section of code will create and add the 4 color buttons and it will also create the action listener for the corresponding buttons
ColorButton=new JButton[4];
BtnLayout= new JPanel[5];
for(int w=0; w<5; w++)
{
BtnLayout[w]=new JPanel();
}
sequencer=new JButton("Start/Reset");
for (int x=0; x<4; x++)
{
ColorButton[x]=new JButton();
ColorButton[x].setPreferredSize(btnSize);
ColorButton[x].addActionListener(new ColorButtonListener());
}
BtnLayout[0].add(ColorButton[0]);
BtnLayout[1].add(ColorButton[1]);
BtnLayout[2].add(ColorButton[2]);
BtnLayout[3].add(ColorButton[3]);
BtnLayout[4].add(sequencer);
add(BtnLayout[0], BorderLayout.NORTH);
add(BtnLayout[1], BorderLayout.WEST);
add(BtnLayout[2], BorderLayout.SOUTH);
add(BtnLayout[3], BorderLayout.EAST);
add(BtnLayout[4], BorderLayout.CENTER);
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.yellow);
sequencer.addActionListener(new sequencerListener());//action listener for the sequence creator
pack();
setVisible(true);//displays the window
setResizable(false);
}
privateclass sequencerListener implements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
int sequence[]=newint[10];
Random rand=new Random();
for (int r = 0; r < 10; r++)
{
sequence[r] = rand.nextInt(4)+1;
if(sequence[r]==1)
{
ColorButton[0].setBackground(Color.black);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.yellow);
System.out.print("Blue, ");
}
if(sequence[r]==2)
{
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.black);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.yellow);
System.out.print("Red, ");
}
if(sequence[r]==3)
{
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.black);
ColorButton[3].setBackground(Color.yellow);
System.out.print("Green, ");
}
if (sequence[r]==4)
{
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.black);
System.out.print("Yellow, ");
}
}
}
}
privateclass ColorButtonListener implements ActionListener
{
publicvoid actionPerformed(ActionEvent e)
{
for(int x=0; x<10;x++)
{
if(e.getSource() == ColorButton[0])
{
Blue=true;
ColorButton[0].setBackground(Color.black);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.yellow);
e.setSource(0);
Blue=false;
}
elseif(e.getSource() == ColorButton[1])
{
Red=true;
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.black);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.yellow);
e.setSource(0);
Red = false;
}
elseif(e.getSource() == ColorButton[2])
{
Green=true;
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.black);
ColorButton[3].setBackground(Color.yellow);
e.setSource(0);
Green = false;
}
elseif(e.getSource() == ColorButton[3])
{
Yellow=true;
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.black);
e.setSource(0);
Yellow = false;
}
}
}
}
publicstaticvoid main(String[] args)
{
new ISay();
}
}
I can't figure out how to show the sequence then let the user follow the sequence and verify he pressed the correct button. I don't know how to use the timer for the delay on the sequence.
yeah, it's my final project for my java class. the only thing standing between me and my associates degree. I've been up all night working on it and have to turn it in an hour. I'm in a tough situation right now so any help is appreciated.
Yeah, the sequencer action listener needs a timer to create a delay in order to allow the player enough time to see it. right now all it does is go straight to the last button in the sequence.
the other part is the button action listener, in that one i have to verify the users choice to check for accuracy.
Unfortunately I haven't done much AWT or Swing, and I've only messed with JavaFX a little, so you probably know more than me. But based on some quick research would javax.swing.Timer be helpful to you?
Yes, the only problem is that I have to stick to material from the book. I wish I could use other material, I've found many useful examples online but none that I could use. I do appreciate you trying though.
It has a paragraph on delays lol, and I can't use the Thread function. I'm hoping with what i have i can get a passing grade all i need is a 75. I'm about to head to school to see if there is anyone there for tutoring that might be able to help. sadly the last time i went for help i knew more than the tutors about programming. my school is transitioning from a technical school to an actual college so they have some catching up to do.