Question

First of all, apologies for how long winded this is.

I'm trying to make a simple roulette game that allows a user to add players, place bets for these players, and spin the roulette wheel, which is represented as a simple JLabel that updates it's text with each number it passes.

However, I've run into a bug that I'm having a lot of trouble with: the JLabel only updates the text for the last element in my loop.

Basically, my solution works like this:

When a user presses a button labelled "Spin" (given that users have been added to the game), I call a method from a class called SpinWheelService, which is an Observable singleton which in turn calls the notifyObservers() method:

public void actionPerformed(ActionEvent e) {

    String cmd = e.getActionCommand();
    String description = null;

    if (ADD_PLAYER.equals(cmd)) { 

        addDialog();

    } else if (PLACE_BET.equals(cmd)) { 

        betDialog();

    } else if (SPIN.equals(cmd)) { 

        SpinWheelService.sws.setSpinWheelService();

    } else if (DISPLAY.equals(cmd)) {
        System.out.println("Display selected!");
    }

}

Here is my SpinWheelService class:

package model;

import java.util.*;

public class SpinWheelService extends Observable {

    public static SpinWheelService sws = new SpinWheelService();

    public void setSpinWheelService() {
        setChanged();
        notifyObservers();
    }

}

The only listener registered for SpinWheelService is this class, where GameEngine is my game engine that handles internal game logic, WheelCallbackImpl is a class that updates the View:

class SpinWheelObserver implements Observer {

GameEngine gameEngine;
ArrayList<SimplePlayer> players;
WheelCallbackImpl wheelCall;

int n;

public SpinWheelObserver(GameEngine engine, WheelCallbackImpl wheel, ArrayList<SimplePlayer> playerList) {
    players = playerList;
    gameEngine = engine;
    wheelCall = wheel;
}

public void update(Observable sender, Object arg) {

    // check if any players are present
    if (players.size() == 0) {
        System.out.println("Empty player array!");
        return;
    }

    do {

        gameEngine.spin(40, 1, 300, 30, wheelCall);

        n = wheelCall.playback();

    } while (n== 0);


}

}

The main point of note here is my gameEngine.spin() method, which is this:

public class GameEngineImpl implements GameEngine {

private List<Player> playerList = new ArrayList<Player>();

// method handles the slowing down of the roulette wheel, printing numbers at an incremental delay
public void delay(int millis) {
    try {
        Thread.sleep(millis);
    } catch (InterruptedException e) {
        System.out.println("Sleep method failed.");
    }
}

public void spin(int wheelSize, int initialDelay, int finalDelay,
    int delayIncrement, WheelCallback callback) {

    Random rand = new Random();
    int curNo = rand.nextInt(wheelSize) + 1;
    int finalNo = 0;

    assert (curNo >= 1);

    // while loop handles how long the wheel will spin for
    while (initialDelay <= finalDelay) {
        delay(initialDelay);
        initialDelay += delayIncrement;

        // handles rotating nature of the wheel, ensures that if it reaches wheel size, reverts to 1
        if (curNo > wheelSize) {
            curNo = 1;
            callback.nextNumber(curNo, this);
            curNo++;
        }

        assert (curNo <= wheelSize);
        callback.nextNumber(curNo, this);
        curNo++;

        finalNo = curNo - 1;
    }

    calculateResult(finalNo);

    callback.result(finalNo, this);

}

The method callback.nextNumber(curNo, this):

public void nextNumber(int nextNumber, GameEngine engine) {

    String strNo = Integer.toString(nextNumber);

    assert (nextNumber >= 1);

    System.out.println(nextNumber);

    wcWheel.setCounter(strNo);
}

Where in, wcWheel is my singleton instance of my View, which contains the method setCounter():

 public void setCounter(String value) {
    label.setText(value);
}

Sorry for how convoluted my explanation is, but basically what it boils down to is that setCounter() is definitely being called, but seems to only call the setText() method on the final number. So what I'm left with is an empty label that doesn't present the number until the entire roulette has finished spinning.

I've determined that setCounter() runs on the event dispatch thread, and I suspect this is a concurrency issue but I have no idea how to correct it.

I've tried to include all relevant code, but if I'm missing anything, please mention it and I'll post it up as well.

I'm at my wits end here, so if anyone would be kind of enough to help, that would be so great.

Thank you!

Was it helpful?

Solution

Your while loop along Thread.sleep() will block and repainting or changing of the UI until the loop is finished.

Instead you'll want to implement a javax.swing.Timer for the delay, and keep a counter for the number of ticks, to stop it. You can see more at How to Use Swing Timers

The basic construct is

Timer ( int delayInMillis, ActionListener listener )

where delayInMillis is the millisecond delay between firing of an ActionEvent. This event is listened for by the listener. So every time the event is fired, the actionPerfomed of the listener is called. So you might do something like this:

Timer timer = new Timer(delay, new ActionListener()(
    @Override
    public void actionPerformed(ActionEvent e) {
        if (count == 0) {
            ((Timer)e.getSource()).stop();
        } else {
            //make a change to your label
            count--;
        }
    }
));

You can call timer.start() to start the timer. Every delay milliseconds, the label will change to what you need it to, until some arbitrary count reaches 0, then timer stops. You can then set the count variable to whatever you need to, if you want to to be random, say depending on how hard the wheel is spun :D

OTHER TIPS

I think you didn't post all the relevant code that is required to know exactly the problem.

But most likely the problem is due to you run your loop and JLabel.setText() in the EDT (Event Dispatching Thread).

Note that updating the UI components (e.g. the text of a JLabel) also happens in the EDT, so while your loop runs in the EDT, the text will not be updated, only after your loop ended and you return from your event listener. Then since you modified the text of the JLabel it will be refreshed / repainted and you will see the last value you set to it.

Example to demonstrate this. In the following example a loop in the event listener loops from 0 to 9 and sets the text of the label, but you will only see the final 9 be set:

JPanel p = new JPanel();
final JLabel l = new JLabel("-1");
p.add(l);
JButton b = new JButton("Loop");
p.add(b);

b.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        for ( int i = 0; i < 10; i++ ) {
            l.setText( "" + i );
            try { Thread.sleep( 200 ); } catch ( InterruptedException e1 ) {}
        }
    }
} );

A proposed solution: Use javax.swing.Timer to do the loop's work. Swing's timer calls its listeners in the EDT so it's safe to update swing components in it, and once the listener returns, a component UI update can happen immediately:

JPanel p = new JPanel();
final JLabel l = new JLabel("-1");
p.add(l);
JButton b = new JButton("Loop");
p.add(b);

b.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        new Timer(200, new ActionListener() {
            int i = 0;
            @Override
            public void actionPerformed(ActionEvent e2) {
                l.setText("" + i);
                if ( ++i == 10 )
                    ((Timer)e2.getSource()).stop();
            }
        }).start();
    }
} );

In this solution you will see the label's text counting from 0 up to 9 nicely.

It's appears to me that your entire game must block in the action handler until the while loop has finished? So the text of the label will be getting updated but only the last update will be visible once the AWT thread is running again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top