Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  java event repainting  (Read 2253 times)
0 Members and 1 Guest are viewing this topic.
Offline DigitalGhost

Senior Newbie




Java games rock!


« Posted 2004-12-29 17:06:15 »

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?
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2004-12-29 17:45:53 »

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.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline DigitalGhost

Senior Newbie




Java games rock!


« Reply #2 - Posted 2004-12-29 19:27:22 »

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.


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 =/.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #3 - Posted 2004-12-29 20:05:38 »

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.:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
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. :-)

Java Game Console Project
Last Journal Entry: 12/17/04
Offline DigitalGhost

Senior Newbie




Java games rock!


« Reply #4 - Posted 2004-12-30 03:51:14 »

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.:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
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. :-)


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.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (150 views)
2017-09-30 02:57:34

theagentd (225 views)
2017-09-26 18:23:31

cybrmynd (304 views)
2017-08-02 12:28:51

cybrmynd (291 views)
2017-08-02 12:19:43

cybrmynd (300 views)
2017-08-02 12:18:09

Sralse (292 views)
2017-07-25 17:13:48

Archive (980 views)
2017-04-27 17:45:51

buddyBro (1108 views)
2017-04-05 03:38:00

CopyableCougar4 (1685 views)
2017-03-24 15:39:42

theagentd (1434 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!