I am Working on a game where the user is a character who must avoid falling rocks. The game works fine until i try to play it again without restarting it. The problem occurs when the game over screen is displayed and i click on a PlayAgain button which has an action listener. In the actionPerformed() method i attempt to reset the game to play as if i had just started it. Instead the DataProc method runs and does not display anything to the GUI, as this is happening the GUI seems to be frozen on the GameOver screen.
I am a beginner at java, especially java swing, so any tips rather than just a solution to my problem would be appreciated.
Sorry for the large amount of code posted I'm not sure what is needed to answer my question.
That is too much code for us to read and go through, though I do notice you are using == true or == false Never use them. Not if (b == true)… or if (b == true)… but if (b)… amd if (!b)… Using == is not only poror style but also error‑prone; you might write = by mistake.
What is the state of your game at the beginning and how are you restoring that state? What are you calling from the listener on the replay button? Do you enable or disable that button ever?
Your problem is that when you run DataProc from the actionPerformed method it is running in the GUI thread. It a long running task that contains a while(true) statement. This will be consuming 100% of the GUI thread's time so it won't have time to update the use interface.
You need to move DataProc to a background thread. Look up SwingWorker for a class which should help you fix the problem. Also read the swing section of Oracle's java tutorial. I believe it has a section on using threads with swing.
I tried using card layout, but the problem persisted. Eventually the Screen did change it just waited until DataProc() was done running. So i then tried multithreading, it fixed the problem of it not Changing screens, but now my keyListeners will not work. I think this is because i am using "this" in my addKeyListener() method.
this is where i start the new thread. this code resides in DataProc class.
I start doInBackground in my actionPerformed() method.
the KeyListener() methods are located in DataProc class