0

I have a Board that contains tiles and the Tiles contain Obstacles. An obstacle can be one of the several types (wall, laser, ...).

Every round of the game I execute the board, which exectutes all the Obstacles. Once an obstacle has been executed and something has changed the GUI is notified and the affected tiles are refreshed.

In the GUI a tile is an OverLayLayout that contains overlapping images (JLabels), so all the overlapping images are visible.

Now the problem is when I execute them, it goes to fast, you can't anything happen. E.g. you don't see the laser beam appear, the player will be damaged but you didn't see it happen...

So I tried to let the game wait for e.g. 500ms so the user can see the GUI update. I've tried (but might have done it wrong): timers, swingtimers, countdownlatch, ... but I can't seem to get it to work. Any ideas on how to fix this? Or maybe an alternative for the waiting?

Thanks in advance!

Update: The execute in board looks like this:

Iterator<Obstacle> it = obstacles.iterator();
while (it.hasNext())
    it.next().execute(this);

CountDownLatch countDown = new CountDownLatch(1);
setChanged();
notifyObservers(countDown);
countDown.await();

The update function of the GUI board (which is notified) looks like this:

final CountDownLatch countDown = (CountDownLatch) arg;
SwingUtilities.invokeLater(new Runnable(){
   public void run() {
      try {
        updateUI();
        wait(500);
      } catch (InterruptedException ex) { }
            countDown.countDown();
   }
});
  • 4
    Without some code, it's like a blind janitor trying to get out of a casino (FYI casinos are built like labyrints to keep people inside). – Radu Murzea May 23 '12 at 19:10
  • 2
    While we're at it, I've got a problem in my Java project right now. I've tried a lot of things and it just doesn't work. Any ideas how to fix it, OP? – Marko Topolnik May 23 '12 at 19:25
  • 1
    Never call `updateUI()` in a Swing application unless you're changing the layout. Don't call wait(...) in the event thread unless your goal is to make your GUI unresponsive. Why not just use a Swing Timer? – Hovercraft Full Of Eels May 23 '12 at 21:21
  • What should I call instead of updateUI()? revalidate and repaint()? – Seppe Magiels May 23 '12 at 22:03

1 Answers1

4

Without seeing your code, it's hard to identify the problem; I suspect you're blocking the event dispatch thread.

This tile-based game uses an instance of javax.swing.Timer to animate play. Press A to enable animation, and click on a free square to see the effect. You may be able adapt the approach to your game.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    It seems there was a small problem with the refreshing of the GUI after the panel was redrawn... Now the Swing Timer works like a charm! Thx! – Seppe Magiels May 24 '12 at 18:55