Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7864703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:51:47+00:00 2026-06-02T23:51:47+00:00

Okay so i am making a simple reaction game and my code is as

  • 0

Okay so i am making a simple reaction game and my code is as below. Now when startGame() is called, if I comment out the while loop everything happens okay but when I run it with the thread.sleep(1000) the thing is just stuck! the whole program executes and nothing happens. Please take 2 minutes to run the code and help me debug it. I am using the thread.sleep(1000) to see if the the user can click the lit up jPanelArray JPanel. But I believe because of the thread.sleep the GUI doesn’t change AT ALL when the start game button is pressed. Help is really appreciated.

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.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JExternFrame extends JFrame implements ActionListener{
    public Color randomColor(){
        return new Color((.8f) * generator.nextFloat() + .04f,(.8f) * generator.nextFloat() + .04f,(.8f) * generator.nextFloat() + .04f);
    }
    public JPanel [][]jPanelArray;
    public JButton startButton;
    int []currentLitUp;
    public Random generator;
    public JPanel main;
    public JExternFrame(){
        super("The Reaction Game");
        generator = new Random();
        setSize(400,400);
        startButton = new JButton("Start Game");
        startButton.setOpaque(true);
        startButton.setBorderPainted(false);
        startButton.setBackground(randomColor());
        add(startButton,BorderLayout.NORTH);
        main = new JPanel();
        main.setBackground(randomColor());
        add(main);
        mouseClick myMouseClick = new mouseClick();
        startButton.addActionListener(this);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    public void startGame(){
        startButton.setVisible(false);
        int numOfClicks = generator.nextInt(8)+5;
        int sizeX = 15;
        int sizeY = 15;
        currentLitUp = new int[2];
        jPanelArray = new JPanel[sizeX][sizeY];
        mouseClick mouseClicked = new mouseClick();
        main.setLayout(new GridLayout(sizeX,sizeY));
        for(int i = 0; i < sizeX; i++){
            for(int j = 0; j < sizeY; j++){
                jPanelArray[i][j] = new JPanel();
                jPanelArray[i][j].setBackground(Color.GRAY);
                main.add(jPanelArray[i][j]);
                jPanelArray[i][j].addMouseListener(mouseClicked);
            }
        }
        while (numOfClicks != 0){   
            numOfClicks--;
            currentLitUp[0] = generator.nextInt(14);
            currentLitUp[1] = generator.nextInt(14);
            jPanelArray[currentLitUp[0]][currentLitUp[1]].setBackground(randomColor());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            jPanelArray[currentLitUp[0]][currentLitUp[1]].setBackground(Color.GRAY);
            currentLitUp[0] = -1;
            currentLitUp[1] = -1;
        }
        System.exit(0);
    }
    public class mouseClick implements MouseListener{

        @Override
        public void mouseClicked(MouseEvent e) {
            if(e.getSource() == jPanelArray[currentLitUp[0]][currentLitUp[1]]){
                System.out.print("Correct");
            }
            else System.out.print("INCORRECT");

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }

    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == startButton){
            startGame();
        }
    }
    public static void main(String[] args){
         new JExternFrame();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:51:49+00:00Added an answer on June 2, 2026 at 11:51 pm

    Your actionPerformed() method is invoked by Swing on the Event Dispatch Thread. In your actionPerformed() method, you call startGame() and this method loops continually. This means that you never let the Event Dispatch Thread continue doing it’s work.

    You need to investigate SwingWorker, and SwingUtilities.invokeLater().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pull code comment blocks out of JavaScript files. I'm making a
okay i'm making a game using c++ (for the engine) and openGL, now i've
Okay, so I'm making an online text-based game, and I'm stuck right now with
Okay When I was making macros I couldn't get the loop to work proper
Okay so, I have to use an interface in a code I'm making involving
I am making card game in C# WPF. For now I have singleplayer mode,
Okay, so at the moment I am making a game in as3 with the
Okay, so I'm making a table right now for Box Items. Now, a Box
Okay, so I am basically making a script to pass post data using cURL.
Okay, I think I'm just making a stupid mistake here, but I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.