Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Applet Failing After First Load  (Read 2776 times)
0 Members and 1 Guest are viewing this topic.
Offline fletchergames

Senior Member





« Posted 2006-08-03 01:29:41 »

With every applet I've ever written (so far as I can recall), I've had the following problem:

It runs fine the first time.  Then, if I open it again in the same Internet browser when any window of the Internet browser is open, the applet is just empty space.  This didn't happen with an applet I checked that was written by some else, so I assume it's a problem with my code.

Shutting all the windows in the Internet Browser and then opening up the page with my applet again works fine.  I assume that Internet Browsers cache the applet somehow, but this doesn't cause a problem with other people's code.

I'm using Java 5, but I've always had this same problem.  This occurs with both Mozilla Firefox and Microsoft Internet Explorer.

Here's the parts of my code that I believe are relevant:

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  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
/**   <p>This class runs the Nanotron applet.
   @author Steven Fletcher
   @since 2006/07/10
   @version 2006/08/02*/

public class Nanotron extends JApplet implements Runnable {
//APPLET/////////////////////////////////////////////////////////////////////////////////
/**   Destroys this applet.*/
public void destroy() {
   //stop the main loop
  synchronized(this) {
      bDestroyApplet = bPauseApplet = true;
   }
} //end destroy

/**   Initializes this applet.*/
public void init() {
//do some initialization
//...
} //end init

/**   Starts this applet.*/
public void start() {
   //focus on this applet so that keyboard input is accepted
  requestFocus();

   //unpause the main loop
  synchronized(this) {
      bPauseApplet = false;
   }

   //start the main loop
  //NOTE: The main loop can't be started during init or start will never be called.
  new Thread(this).start();
} //end start

/**   Stops this applet.*/
public void stop() {
   //stop the main loop
  synchronized(this) {
      bPauseApplet = true;
   }
} //end stop

//RUNNABLE///////////////////////////////////////////////////////////////////////////////
/**   Executes the game's main loop.*/
public void run() {
   while(true) {
      //check for the end of the loop
     synchronized(this) {
         //do nothing while the applet is paused
        while(bPauseApplet) {
            //if the applet should be destroyed, stop
           if(bDestroyApplet)
               return;

            //sleep
           try {
               Thread.sleep(100);
            } catch(InterruptedException exception) {}
         }
      };

      //do the actual loop
//...
  } //end while forever
} //end run

//VARIABLES//////////////////////////////////////////////////////////////////////////////
//multithreading
private boolean bPauseApplet = false, bDestroyApplet = false;
} //end class Nanotron
Offline fletchergames

Senior Member





« Reply #1 - Posted 2006-08-03 01:46:54 »

The following "minimal" code does, in fact, not have this problem.  All it does is display an annoying flashing blue applet though.

I still don't know what part of my actual code causes the problem though.

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  
/**   <p>This class runs the Nanotron applet.
   @author Steven Fletcher
   @since 2006/07/10
   @version 2006/08/02*/

public class Nanotron extends JApplet implements Runnable {
//APPLET/////////////////////////////////////////////////////////////////////////////////
/**   Destroys this applet.*/
public void destroy() {
} //end destroy

/**   Initializes this applet.*/
public void init() {
} //end init

/**   Paints this Applet.
   @param g the Graphics object to paint to*/

public void paint(final Graphics g) {
   super.paint(g);
   g.setColor(Color.blue);
   g.fillRect(0, 0, getWidth(), getHeight());
} //end paint

/**   Starts this applet.*/
public void start() {
   //start the main loop
  //NOTE: The main loop can't be started during init or start will never be called.
  new Thread(this).start();
} //end start

/**   Stops this applet.*/
public void stop() {
} //end stop

//RUNNABLE///////////////////////////////////////////////////////////////////////////////
/**   Executes the game's main loop.*/
public void run() {
   while(true) {
      repaint();
   } //end while forever
} //end run
} //end class Nanotron
Offline fletchergames

Senior Member





« Reply #2 - Posted 2006-08-03 02:05:45 »

I've done some testing (by commenting out various sections of the code).  In the "unimportant" parts of the code that I didn't post, I created some other singleton classes that use static variables.  Could it be that the combination of the static variables and the rather trivial multithreading is causing the problem?  If you run two copies of an applet in the same browser, do they share static variables somehow?  If they do, that kind of sucks.

If that's the problem, the only potential solution I see is to make all the non-final variables static.  And that seems a little silly - those variables are static for a reason.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2006-08-03 12:32:39 »

if your applet isn't huge, you could slowly upgrade that minimal case and see from what code problems appears. Seems worth the time if all your applets suffer from this problem.
Offline fletchergames

Senior Member





« Reply #4 - Posted 2006-08-03 18:51:15 »

That's probably the only solution.  It's more of a hassle than I would like, but it's probably the only way.
Offline fletchergames

Senior Member





« Reply #5 - Posted 2006-08-03 19:17:57 »

I've since discovered that it's only the Swing components within the applet that don't get drawn.  If I fill the applet with blue in the paint method and draw the Swing components, the blue appears but the Swing components don't.

I know there were some problems with CardLayout in previous releases of Java, so I tried using FillLayout instead.  It didn't seem to help.

Does anyone else have this problem with Swing components in applets?
Offline fletchergames

Senior Member





« Reply #6 - Posted 2006-08-05 19:29:47 »

I figured out what the problem was and fixed it.

All of my Swing components (except the applet itself) were created in a singleton class.  The static variables apparently get shared between applets.  This seemed to be what was screwing things up.  All I had to do was make the singleton class a non-singleton.

Also, I made a singleton class representing the state of the game into a non-singleton so that multiple copies of the game can be run simultaneously.
Offline CommanderKeith
« Reply #7 - Posted 2006-08-09 06:08:13 »

That is really wierd, I thought applets ran in their own VM & so static variables are not shared?  I just read the API docs about java.awt.EventQueue http://java.sun.com/j2se/1.5.0/docs/api/java/awt/EventQueue.html and it talks about applet contexts, what are they? Is it onlyu Swing static vars that are shared?

Offline fletchergames

Senior Member





« Reply #8 - Posted 2006-08-10 03:29:15 »

It's all static variables.  I had to change the static variables for the state of the game as well.  I would have the first version of the applet in the game, and then open a second version.  All the enemies would disappear, and the player's sprite would appear in the top-left corner.  The only way I can explain that behavior is that the static variables were being shared.

I read somewhere that applets running at the same time share the same ClassLoader, so that might be the problem.  If they're sharing the same ClassLoader, they must be running in the same VM.

It only seems to be for applets being run from the same url in the same brower though.  I could run 2 copies of the applet from 2 different pages with no problems.

Now that I've taken out all the static variables that change during the game, there's no problem anymore.  It was kind of weird though.
Offline Matlu

Junior Member




Hasta La Victoria Siempre!


« Reply #9 - Posted 2006-08-20 17:26:10 »

That is really wierd, I thought applets ran in their own VM & so static variables are not shared?

A web browser creates only one VM (at least IE and Firefox do it this way). Thus you need to be very careful when same applet runs twice in same web browser. Both instances will share static fields.

Multiplayer Online Games
http://www.duelboard.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #10 - Posted 2006-08-20 18:45:31 »

I guess this is another case of "don't use static variables" wisdom. It can be convenient to use a static variable once in a while for ease of reference, but really it's bad practice. Static variables should generally be final and fully immutable (like String). That aside, I still use bad static references sometimes  Roll Eyes
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (12 views)
2014-07-29 18:09:19

Riven (8 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (25 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!