0

I am attempting to make the game Simon using Java Swing. However, I am unable to figure out how to change the background color of a JButton, wait for several seconds, and then change the background color back to the original color. Although several some other questions have suggested the use of Swing Timers, I am unsure of how to successfully implement these into my existing code.

I have attempted to simply using Thread.sleep() in order to pause for 2 seconds before changing the color, but the colors do not appear to be changing.

Here is my current code:

import java.util.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.*;

public class SimonGame extends JFrame{

    private JButton begin;

    Color dullRed = new Color(255, 128, 128);
    Color dullYellow = new Color(255, 255, 128);
    Color dullBlue = new Color(128, 128, 255);
    Color dullGreen = new Color(128, 255, 128);

    Color backgroundColor = new Color(1,1,1);

    private JButton red;
    private JButton yellow;
    private JButton blue;
    private JButton green;

    // no-argument constructor
    public SimonGame()
    {      
        createUserInterface();      
    }

    // create and position GUI components; register event handlers
    private void createUserInterface()
    {
        // get content pane for attaching GUI components
        Container contentPane = getContentPane();

        // enable explicit positioning of GUI components
        contentPane.setLayout( null );

        begin = new JButton();
        begin.setBounds( 200,100,200,50 );
        begin.setText("Begin Game");
        contentPane.add( begin );
        begin.addActionListener(

                new ActionListener() // anonymous inner class
                {
                    // event handler called when submitJButton is pressed
                    public void actionPerformed( ActionEvent event )
                    {
                        try {
                            createPattern();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                } // end anonymous inner class

                ); // end call to addActionListener

        red = new JButton();
        red.setBounds( 300,300,100,100 );
        red.setBackground(dullRed);
        red.setBorder(null);
        contentPane.add( red );
        red.addActionListener(

                new ActionListener() // anonymous inner class
                {
                    // event handler called when submitJButton is pressed
                    public void actionPerformed( ActionEvent event )
                    {
                        //submitJButtonActionPerformed( event );
                    }

                } // end anonymous inner class

                ); // end call to addActionListener

        yellow = new JButton();
        yellow.setBounds( 300,200,100,100 );
        yellow.setBackground(dullYellow);
        yellow.setBorder(null);
        contentPane.add( yellow );
        yellow.addActionListener(

                new ActionListener() // anonymous inner class
                {
                    // event handler called when submitJButton is pressed
                    public void actionPerformed( ActionEvent event )
                    {
                        //submitJButtonActionPerformed( event );
                    }

                } // end anonymous inner class

                ); // end call to addActionListener

        green = new JButton();
        green.setBounds( 200,300,100,100 );
        green.setBackground(dullGreen);
        green.setBorder(null);
        contentPane.add( green );
        green.addActionListener(

                new ActionListener() // anonymous inner class
                {
                    // event handler called when submitJButton is pressed
                    public void actionPerformed( ActionEvent event )
                    {
                        //submitJButtonActionPerformed( event );
                    }

                } // end anonymous inner class

                ); // end call to addActionListener

        blue = new JButton();
        blue.setBounds( 200,200,100,100 );
        blue.setBackground(dullBlue);
        blue.setBorder(null);
        contentPane.add( blue );
        blue.addActionListener(

                new ActionListener() // anonymous inner class
                {
                    // event handler called when submitJButton is pressed
                    public void actionPerformed( ActionEvent event )
                    {
                        //submitJButtonActionPerformed( event );
                    }

                } // end anonymous inner class

                ); // end call to addActionListener


        setTitle( "Simon Game" ); // set title bar string
        setSize( 600, 600 );     // set window size
        setVisible( true );
    }

    private void setDefaultColors(){
        red.setBackground(dullRed);
        yellow.setBackground(dullYellow);
        blue.setBackground(dullBlue);
        green.setBackground(dullGreen);
    }

    private void createPattern() throws InterruptedException
    {
        Random rand = new Random();

        int iter = 4;
        int generatedPattern[] = new int[iter];

        for(int i = 0; i<iter; i++)
        {
            int randomColor = rand.nextInt(4) +1;

            generatedPattern[i] = randomColor;

            switch(randomColor)
            {
                case 1:
                    red.setBackground(Color.RED);
                    Thread.sleep(2000);
                    red.setBackground(dullRed);
                    break;
                case 2:
                    yellow.setBackground(Color.YELLOW);
                    Thread.sleep(2000);
                    yellow.setBackground(dullYellow);
                    break;
                case 3:
                    blue.setBackground(Color.BLUE);
                    Thread.sleep(2000);
                    blue.setBackground(dullBlue);
                    break;
                case 4:
                    green.setBackground(Color.GREEN);
                    Thread.sleep(2000);
                    green.setBackground(dullGreen);
                    break;
                default:
                    System.out.print("The program is super broken");
                    break;
            }
        }
    }

    public static void main( String[] args )
    {
        SimonGame application = new SimonGame();
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    } // end method main

}

I would greatly appreciate any assistance.

Thank you.

John
  • 3
  • 3
  • 2
    Have a look at [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for the "why" and [How to use Swing Timers](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for a possible "how" – MadProgrammer Jun 01 '17 at 21:46
  • 1
    [For example](https://stackoverflow.com/questions/13750659/in-java-how-do-i-repaint-a-panel-from-an-actionperformed-thread-while-it-is-curr/13750859#13750859) and [example](https://stackoverflow.com/questions/23820488/java-swing-jbutton-time-delays-flicker/23820554#23820554) – MadProgrammer Jun 01 '17 at 21:47
  • 1
    *"Although several some other questions have suggested the use of Swing Timers, I am unsure of how to successfully implement these into my existing code."* 1) Try doing something & if you fail.. 2) Get back to us with a specific question on that. - `Thread.sleep()` is **not** the way to go in this instance. – Andrew Thompson Jun 01 '17 at 22:12
  • 1
    *"Although several some other questions have suggested the use of Swing Timers"* - a `Timer` requires you to expand the way you think about the problem. A `Timer` is just a pseudo loop, each "tick" is an iteration through the loop. If you can understand how a loop works, you can understand how a `Timer` works – MadProgrammer Jun 01 '17 at 22:52
  • 1
    What I would also recommend is, you pre-define the sequence you want to display, then play that sequence, this will then allow you to determine if the user inputs the same sequence, as [demonstrated here](https://stackoverflow.com/questions/33727830/java-button-pausing-graphical-updates/33727882#33727882) – MadProgrammer Jun 01 '17 at 23:03
  • Possible duplicate of [Java Button pausing graphical updates](https://stackoverflow.com/questions/33727830/java-button-pausing-graphical-updates) – Catalina Island Jun 02 '17 at 09:34

0 Answers0