For a college project I was tasked with making an adventure game in Java. I have used threads and the swing library among other things, but now I have encountered a problem.
// The following is in the constructor of a subclass of JPanel
gameViewThread = new Thread(() -> {
double previous = System.currentTimeMillis();
double lag = 0.0;
double current;
double elapsed;
while(true){
current = System.currentTimeMillis();
elapsed = current - previous;
previous = current;
lag += elapsed;
System.out.println("Checking up on stuff...");
while(OverworldMap.initialised && lag >= 17) {
generateBackground();
lag -= 17;
}
}
});
On line 11 of the above pasted code, I have a print statement which I would like to remove. (I don't want to be flooding the console with not needed information) However, when I remove that statement, then the visuals do not update. I have tested this time and time again, and I have made sure that OverworldMap.initialised returns true. When running the code in debug mode, I made sure that generateBackground() runs. So to me, it looks like the visuals are just not updating unless you 'wake up' the System.
NOTE: generateBackground() generates and saves an image in a BufferedImage variable, and repaint() makes sure it is drawn in the correct location. (repaint() is called in the main thread 30 times a second) These two methods do work. They worked before I tried shifting the background generation (Think tile animations from Pokemon Fire Red o/e) to a different thread. (I didn't want the logic and animation to interfere with one another.(I had low frame rate))