java event repainting

Sorry for such a newb question, but…

I have a button event that starts my game. The problem is that there are a LOT of graphcis and stuff to download. I made it so I use the setText() of a textarea to show the graphic file names so taht the user doesn’t think the game has hung. The problem is that the text isn’t getting set because I guess it needs to be in a separate thread or something. Even when I call repaint after setText(…), it still doesn’t update. What should I do?

This is a well known issue. The problem is that the paint thread and event thread are one and the same. If you lock the event thread (e.g. doing network loading on ActionEvent), then nothing can get painted.

The solution is to spawn a new Thread from your actionPerformed() clause. This will unblock the event thread and allow the screen to repaint.

[quote]This is a well known issue. The problem is that the paint thread and event thread are one and the same. If you lock the event thread (e.g. doing network loading on ActionEvent), then nothing can get painted.

The solution is to spawn a new Thread from your actionPerformed() clause. This will unblock the event thread and allow the screen to repaint.
[/quote]
I tried that, and when I did, my fps in my game dropped from 200 to 8-10. I think it might have something to do with a thread being inside a thread which has finished, but I’m not sure. I was hoping there was another solution =/.

Just to be clear, I am not saying that you should launch your entire game in a new thread. That would most likely cause a few issues at the OS level. Rather, I am suggesting that just the loading part happens in the new thread. e.g.:


public class Loader implements Runnable
{
    private Game game;
    private String[] images = {"Image1.png", "Image2.png"}
    private boolean done = false;

    public Loader(Game game)
    {
        this.game = game;
    }

    public void run()
    {
        for(int i=0; i<images.length; i++)
        {
            //load image
            game.loaded(images[i]);
        }

         done = true;
    }

    public boolean isDone() {return done;}
}

public class Game implements ActionListener
{
    private Loader loader = new Loader(this);

    public void actionPerformed(ActionEvent evt)
    {
        Thread thread = new Thread(loader);
        thread.start();
    }

    public static void main(String[] args)
    {
        Game game = new Game();
        //set up some GUI stuff
        while(!game.loader().isDone())
        {
            try{Thread.sleep(100);}catch(Exception e) {}
        }

        //Do Game Loop
    }
}

The important thing is to not let go of the initial thread used to construct your game. That way you can keep things simple, and make sure that you don’t run into any nasty surprises. At the same time, you also make sure that you don’t block the event thread. :slight_smile:

[quote]Just to be clear, I am not saying that you should launch your entire game in a new thread. That would most likely cause a few issues at the OS level. Rather, I am suggesting that just the loading part happens in the new thread. e.g.:


public class Loader implements Runnable
{
    private Game game;
    private String[] images = {"Image1.png", "Image2.png"}
    private boolean done = false;

    public Loader(Game game)
    {
        this.game = game;
    }

    public void run()
    {
        for(int i=0; i<images.length; i++)
        {
            //load image
            game.loaded(images[i]);
        }

         done = true;
    }

    public boolean isDone() {return done;}
}

public class Game implements ActionListener
{
    private Loader loader = new Loader(this);

    public void actionPerformed(ActionEvent evt)
    {
        Thread thread = new Thread(loader);
        thread.start();
    }

    public static void main(String[] args)
    {
        Game game = new Game();
        //set up some GUI stuff
        while(!game.loader().isDone())
        {
            try{Thread.sleep(100);}catch(Exception e) {}
        }

        //Do Game Loop
    }
}

The important thing is to not let go of the initial thread used to construct your game. That way you can keep things simple, and make sure that you don’t run into any nasty surprises. At the same time, you also make sure that you don’t block the event thread. :slight_smile:
[/quote]
hmm

That poses a problem because right now, the event actually creates the game object. the thing is, the applet is a login screen, and based on the username/password, you can potenitally get a different game (wheter you log in as a level builder or a player), so I’m making a new game in the event thread. I’ll have to somehow rework it so the event thread doesn’t make the game, which is going to be difficult.