Screen Sometimes Blank at Game Start - java

I've been working on a simple game engine. All was going pretty well, and I got the foundation finished. By that, I mean, the game sets up with user defined display settings, initializes stuff, and starts the game loop. Everything works great, except I have an issue in both Windowed mode and full screen:
In Windowed mode, sometimes the screen will appear blank, until I resize or minimize the window.
In full screen, I only get a blank screen if I use the default Display resolution. Most of the other resolutions appear just fine.
Here is some code relevant to displaying the screen:
This is performed when the use presses the play button on the display settings dialog
public void actionPerformed(ActionEvent e) {
DisplayMode dm = modes[resolutionBox.getSelectedIndex()];
boolean fs = fullscreenBox.getSelectedItem().equals ("Fullscreen");
boolean vs = vSyncCheckBox.isSelected();
game.initScreen (dm, fs, vs);
runGame (game);
f.dispose();
}
});
private final void runGame(EGame g)
{
Thread t = new Thread (g);
t.start();
}
This is the run method in the EGame class
public final void run() {
start();
isRunning = true;
gameLoop();
endGame();
}
The user of the engine will call the method setGameCanvas() in the start() method. Other things can go in the start() method, but for my test games, it just that method:
protected final void setGameCanvas(EGameCanvas c)
{
screen.remove (canvas);
canvas = c;
canvas.addKeyListener (keyboard);
screen.add (canvas);
}
And finally, here is the gameLoop:
private final void gameLoop()
{
// Final setup before starting game
canvas.setBuffer (2);
screen.addKeyListener (keyboard);
canvas.addKeyListener (keyboard);
int TARGET_FPS = screen.getTargetFps();
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
long lastLoopTime = System.nanoTime();
long now = 0;
long lastFpsTime = 0;
long updateLength = 0;
double delta = 0;
int fps = 0;
while (isRunning)
{
now = System.nanoTime();
updateLength = now - lastLoopTime;
lastLoopTime = now;
delta = updateLength / ((double) OPTIMAL_TIME);
lastFpsTime += updateLength;
fps++;
if (lastFpsTime >= 1000000000)
{
screen.setFps (fps);
lastFpsTime = 0;
fps = 0;
}
keyboard.poll();
keyboardEvents();
update (delta);
render();
try {
Thread.sleep( (lastLoopTime-System.nanoTime() + OPTIMAL_TIME)/1000000 );
}
catch (Exception ex) {}
}
}
Now, keep in mind, neither of my issues existed, until I added the run method and ran it in a separate thread. I can about guarantee it's a threading issue.
But, I'm no expert, so any advice would be appreciated.

I believe the issue is that you're trying to modify GUI-related components outside of the event dispatch thread - that is the only thread allowed to modify the graphical interface, otherwise you might encounter issues like this.
To check this try if the following solves the issue:
try {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeAndWait( new Runnable() {
#Override
public void run() {
// Do rendering and other stuff here...
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
If that helps, I'd recommend taking a look on the Concurrency in Swing tutorial.

Related

JFrame Won't Close

I am working on a personal project to experiment with 2D physics. Right now, I am trying to set up the JFrame so that it updates 60 times per second. However, when I call the method to start updating the JFrame, I cannot close the JFrame. But if I leave out the part where I call the method, I can close the JFrame. This is the main code:
import javax.swing.JFrame;
import utils.Frame;
public class Engine {
Frame w;
boolean running = false;
JFrame f;
public void start() {
init();
updater();
}
public void init() {
w = new Frame();
running = true;
f = w.create(500, 500, "Work please");
}
public void updater() {
int fps = 60;
double timePerTick = 1000000000 / fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();
long timer = 0;
while (running) {
now = System.nanoTime();
delta += now - lastTime;
timer += now - lastTime;
lastTime = now;
if (delta >= timePerTick) {
//render
delta -= timePerTick;
}
}
}
}
And this is the code for Frame:
import javax.swing.JFrame;
public class Frame {
public JFrame create(int width, int height, String title) {
JFrame f = new JFrame();
f.setSize(width, height);
f.setTitle(title);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
return f;
}
}
If I don't call updater() in start(), I can close the JFrame. I know that I leave running = true, but it should still close because that would work on my old computer. And even if I need to make running = false, I already tried that by adding a WindowListener to f in Frame. It would call a method to make running = false, but for some reason the WindowListener wouldn't active when I hit the close button. Thank you for your help in advance. If this will help you in any way, I had to download a direct package of the Eclipse IDE from the website because every time I used the installer, every project would get the error "Failed to init ct.sym ..." And for some reason, when I run a Java program with a JFrame in it, instead of the usual Jar logo, the Java mascot "Duke" shows up as the icon, which has been throwing me off.
Make sure you have two methods that control whether the program is running.
However make sure both methods are synchronized by using the-
synchronized
-keyword.
public synchronized void start() {
if (running) return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
if (!running) return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

How to pause program execution without blocking a Timer (Java Swing)?

I have a Timer that shows an animation on the screen when a game event takes place. What I want to do is have the main execution pause until this animation finishes, but everything I try seems to block the Timer from running. I've tried using Thread.sleep() and calling wait() and notify() on a lock object, but with the same result. The Timer listener's actionPerformed() is never called.
This code sets up the timer:
protected void showMovingEffect(int steps, Direction dir, AnimatedImageSet imgs) {
effectsPane.runAnimation(imgs, dir, steps);
waiting = true;
while (waiting) {
synchronized(animationWaitLock) {
try {
animationWaitLock.wait();
} catch (InterruptedException e) {}
}
}
}
And the Timer and listener inside the EffectsPane object:
private void runAnimation(AnimatedImageSet ais, Direction dir, int iters) {
System.out.println("run");
imgs = ais;
top = 0;
left = 0;
topStep = dir.getRowIncrement() * 10;
leftStep = dir.getColIncrement() * 10;
iterations = iters;
index = 0;
active = true;
timer.start();
}
public void actionPerformed(ActionEvent e) {
System.out.println(index);
top += topStep;
left += leftStep;
index++;
currentImage = imgs.getImage(index);
repaint();
if (index == iterations) {
active = false;
timer.stop();
synchronized(animationWaitLock) {
animationWaitLock.notify();
}
waiting = false;
}
}
The System.out.println call at the start of the actionPerformed() is never called, so either the wait() call is also pausing the Timer, or something is blocking it. If I comment out the sleep/wait lines in showMovingEffect(), the animation runs, but the program does not pause.
One approach would be to display a modal dialog while the animation proceeds. As discussed here, user interaction will be foreclosed, but background GUI updates in response to the javax.swing.Timer will continue. You can allow the user to dismiss the dialog at any time, as shown here, but you may want to abandon the animation at that point.
no input from the user is needed.
You can block user interaction without displaying a dialog by entering a SecondaryLoop after the animation starts.
SecondaryLoop loop = Toolkit.getDefaultToolkit()
.getSystemEventQueue().createSecondaryLoop();
timer.start();
loop.enter();
When the animation concludes, exit() the SecondaryLoop:
public void actionPerformed(ActionEvent e) {
…
if (index == iterations) {
timer.stop();
loop.exit ();
…
}
}
You can try this:
timer.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// your code
}
});

Swing animation pause and resume

I want to add possibility to pause/resume to my game. Unfortunately my solution only pauses the game, but does not resume it (when I click resume button nothing happens - the image is still). I also tried to call wait/notify methods on thread, but it also did not work - I got Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException. What is the best solution to make pause/resume?
Game Loop
public void run() {
init();
long startTime;
long reTime;
long waitTime;
while (running) {
startTime = System.nanoTime();
int CarPosX = car.zwrocPolozenieX();
int CarPosY = car.zwrocPolozenieY();
update(b, CarPosX, CarPosY);
render(b);
//draw();
repaint();
if(b<4390) {
b=b+szybkoscMapy;
}
reTime = System.nanoTime() - startTime;
waitTime = targetTime - reTime/100000000;
try {
Thread.sleep(waitTime);
}
catch(Exception e) {
}
}
}
public void init() {
if(thread == null) {
thread = new Thread(this);
thread.start();
}
b=0;
running = true;
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
map = new Map(levelNumber);
car = new Car(map);
car.setxpos(338);
car.setypos(150);
}
Listeners
pause_button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bp.running = false;
}
});
resume_button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bp.running = true;
}
});
Use javax.swing.Timer to pace the animation. Typical controls that invoke the timer's start() and stop() methods are examined here. This fleet simulation, which uses such a Timer, may also be if interest.

Java android - ImageView scrolling causes crashing?

I have a function which is meant to scroll my Image-view so that it falls 5 pixels every 0.1 seconds, however as soon as I run this function I get Sorry Application has stopped responding
You can check my code below.:
void startGameLoop(){
boolean alive=true;
while(alive){
int x = theSprite.getScrollX();
int y = theSprite.getScrollY();
y-=5;
theSprite.scrollTo(x, y);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Any help on this would be much appreciated.
If you put this code Thread.sleep(100); on the main thread that will happen.
Use a Handler with delay instead. The handler can change things like ImageView because it operates on the main Ui Thread.
Something like this..
private final Runnable timerRunnable = new Runnable() {
#Override
public void run() {
int x = theSprite.getScrollX();
int y = theSprite.getScrollY();
y-=5;
theSprite.scrollTo(x, y);
if (!closing)
startTimer();
}
};
handler = new Handler();
startTimer();
/**
* Start periodically callbacks.
*/
private void startTimer() {
runOnUiThreadDelay(timerRunnable, 400L);
}
public void runOnUiThreadDelay(final Runnable runnable, long delayMillis) {
handler.postDelayed(runnable, delayMillis);
}

JProgressBar not working properly

So my JProgressBar I have set up doesn't work the way I want it. So whenever I run the program it just goes from 0 to 100 instantly. I tried using a ProgressMonitor, a Task, and tried a SwingWorker but nothing I tried works.
Here is my program:
int max = 10;
for (int i = 0; i <= max; i++) {
final int progress = (int)Math.round(
100.0 * ((double)i / (double)max)
);
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(BandListGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
jProgressBar2.setValue(progress);
}
});
}
#MadProgrammer Here is my attempt at making a swing worker and writing each name to the document and updating the progress bar. The program gets to around 86 percent and stops, never creating the finished document. The program creates a blank document. Here are the two methods first is the SwingWorker object I made:
public class GreenWorker extends SwingWorker<Object, Object> {
#Override
protected Object doInBackground() throws Exception {
int max = greenList.size();
XWPFParagraph tmpParagraph;
XWPFRun tmpRun;
FileInputStream file =
new FileInputStream(location + "GreenBandList.docx");
gDocument = new XWPFDocument(OPCPackage.open(file));
for (int i = 0; i < max; i++) {
tmpParagraph = gDocument.getParagraphs().get(0);
tmpRun = tmpParagraph.createRun();
if (greenList.get(i).length() == 1) {
tmpRun.setBold(true);
tmpRun.setText(greenList.get(i));
tmpRun.setBold(false);
} else {
tmpRun.setText(greenList.get(i));//Write the names to the Word Doc
}
int progress = Math.round(((float) i / max) * 100f);
setProgress(progress);
}
return null;
}
}
And here is the code for the button that starts it and has my property change event.
private void GenerateGreenList() throws IOException, InterruptedException {
//Need to fix the bug that removes the Letter Header in Yellow Band list
//********************************************************************\\
//Delete the old list and make a new one
File templateFile = new File(location + "\\backup\\GreenTemplate.docx");
FileUtils.deleteQuietly(new File(location + "GreenBandList.docx"));
FileUtils.copyFile(templateFile, new File(location +
"GreenBandList.docx"));
//Get the New Entries
String[] entries = jTextPane3.getText().split("\n");
for (String s : entries) {
if (s != null) {
greenList.add(s);
}
}
//Resort the list
Collections.sort(greenList);
//Write the names to the document
GreenWorker worker = new GreenWorker();
worker.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
jProgressBar2.setValue((Integer) evt.getNewValue());
}
}
});
worker.execute();
if (worker.isDone()) {
try {
gDocument.write(new FileOutputStream(new File(location + "GreenBandList.docx")));
////////////////////////////////////////////////////////////
} catch (IOException ex) {
Logger.getLogger(BandListGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
JOptionPane.showMessageDialog(null, "Green Band List Created!");
jProgressBar2.setValue(0);
}
}
I used the property change listener from one of your other posts but I don't really understand what the one you wrote does or what it does in general?
Swing is a single threaded environment, that is, there is a single thread which is responsible for processing all the events that occur within the system, including repaint events. Should anything block this thread for any reason, it will prevent Swing from processing any new events, including, repaint events...
So all this ...
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(BandListGenerator.class.getName()).log(Level.SEVERE, null, ex); }
jProgressBar2.setValue(progress);
}
});
Is constantly pausing the Event Dispatching Thread, preventing it from actually doing any updates (or at least spacing them randomly)...
It's also likely that your outer loop is been run from within the context of the EDT, meaning that until it exists, nothing in the Event Queue will be processed. All your repaint requests will be consolidated down to a single paint request and voila, instant filled progress bar...
You really should use a SwingWorker - I know you said you tried one, but you've not shown any code as to your attempt in this regards, so it's difficult to know why it didn't work, however...
SwingWorker and JProgressBar example
SwingWorker and JProgressBar example
SwingWorker and JProgressBar example
SwingWorker and JProgressBar example
SwingWorker and dual welding JProgressBar example
SwingWorker and JProgressBar example
And forgive me if we haven't said this a few times before :P
You are evoking Thread.sleep inside the EvokeLater which means that it is running on another thread than your for loop. i.e., your for loop is completing instantaneously (well, however long it takes to loop from 1 to 100, which is almost instantaneously).
Move Thread.sleep outside of EvokeLater and it should work as you intend.
int max = 10;
for (int i = 0; i <= max; i++) {
final int progress = (int)Math.round(
100.0 * ((double)i / (double)max)
);
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
jProgressBar2.setValue(progress);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(BandListGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
}
Edit: agree with #MadProgrammer. It appears this is just an illustrative question, but you should make sure whatever you're trying to accomplish here you use a SwingWorker for.

Categories