Calling repaint() in JApplet doesn't get around to calling paint()

As the topic says, whenever I call repaint() in my applet, the paint() method never gets invoked.

The code for the applet:

import javax.swing.*;

import java.awt.*;

publicclass FallDownextends JAppletimplements Runnable

{

private GameManager game;

private Thread thread;

privateboolean isRunning;

publicvoid start()

{

if(thread ==null)

{

game =new GameManager(this);

isRunning =true;

thread =new Thread(this);

thread.start();

}

}

publicvoid run()

{

while(isRunning)

{

game.updateFloors();

repaint();

try{Thread.sleep(100);}

catch(InterruptedException e)

{}

}

}

publicvoid stop()

{

isRunning =false;

}

publicvoid paint(Graphics g)

{

g.setColor(Color.white);

g.fillRect(0, 0, 600, 600);

game.draw(g);

}

publicvoid update(Graphics g)

{

paint(g);

}

}

The run() function (seemingly) executes as expected, but the call to repaint() never gets around to calling paint() for some reason unbeknownst to me.

I'm pretty sure that the problem lies within this class here, though if you wish to see any of the other code for other classes, I'll post them.

[2885 byte] By [Oooskaa] at [2007-9-30 0:24:50]
# 1
Try changing from paint(Graphics g)to:paintComponent(Graphics g)This has worked for me.
drslinkya at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...
# 2
Thanks for the suggestion, but it doesn't seem to work. When I do that, the screen doesn't get repainted, nor does it get painted when the applet first starts. Any other suggestions?
Oooskaa at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...
# 3
add 2 debug lines, 1 in the paint(Graphics g) method, and 1 before the repaint() call.Tell me what u find.
Abusea at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...
# 4
The debug statement prior to the call to repaint() is executed, but the debug statement in the paint() method itself is not executed.
Oooskaa at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...
# 5
instead of just calling repaint.. try a little active rendering:Graphics g = getGraphics();if (g != null) paint(g);else repaint();
Woogleya at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...
# 6
Active rendering doesn't seem to be working either; g always seems to be null. Thanks for the suggestions so far. Rendering graphics in applets has always given me a headache for some reason.
Oooskaa at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...
# 7

2 possible causes I can think of....

1) You have added a Component to the JApplet.

2) you have a loop in a Listener (Mouse/Key etc etc) that is tieing up the EventDispatch thread (and hence preventing repaint events from being serviced)

if you remove the GameManager stuff, does it work?

Abusea at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...
# 8
Removing the references to the game objects does allow the screen to refresh, but it stops after doing so about 6 times.
Oooskaa at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...
# 9
Yes, that sounds about right, try putting the statementSystem.out.println("Thread is Running"); //or similar in your run meathodand see if anything prints to console.
drslinkya at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...
# 10

Everything seems to be working correctly.

Here's the code with a few debug statements sent to the console:

import javax.swing.*;

import java.awt.*;

public class FallDown extends JApplet implements Runnable

{

private GameManager game;

private Thread thread;

private boolean isRunning;

private int loc = 50;

public void start()

{

System.out.println("in start()");

if(thread == null)

{

game = new GameManager(this);

isRunning = true;

thread = new Thread(this);

thread.start();

}

}

public void run()

{

System.out.println("In run()");

while(isRunning)

{

System.out.println("About to call updareFloors()");

//game.updateFloors();

//System.out.println("About to call repaint()");

//repaint();

Graphics g = getGraphics();

if(g != null)

{

System.out.println("g is NOT equal to null");

paint(g);

g.dispose();

}

else

repaint();

System.out.println("About to sleep for 100");

try{Thread.sleep(1000);}

catch(InterruptedException e)

{}

}

}

public void stop()

{

System.out.println("in Stop()");

isRunning = false;

}

public void paint(Graphics g)

{

super.paint(g);

System.out.println("In paint");

g.setColor(Color.black);

g.drawString("Test..." + loc, loc, loc);

//game.draw(g);

loc += 5;

}

public void update(Graphics g)

{

paint(g);

}

}

It prints "testing... #" about 5 times before quitting, yet the run() loop is still executing continuously. THanks for the help so far. This is really quite frustrating to me.

Oooskaa at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...
# 11

The only reason the call to repaint() could possibly not cause a redraw, is if some other piece of code executing on the Event dispatch Thread was in an infinite loop.(or was deadlocked due to 2 competing synchronization blocks)

Does GameManager do any stuff with Listeners at all?

also, getGraphics() should always return a valid Graphics context (once the JApplet has been fully realized), so the repaint(); line should never be executing.

The code below works absolutely fine.

Either you have a very strange phuked up machine :D or your GameManager was doing something it should have been doing on the event dispatch Thread (in a Listener)

import javax.swing.*;

import java.awt.*;

public class FallDown extends JApplet implements Runnable

{

private Thread thread;

private boolean isRunning;

private int loc = 50;

public void start()

{

System.out.println("in start()");

if(thread == null)

{

isRunning = true;

thread = new Thread(this);

thread.start();

}

}

public void run()

{

System.out.println("In run()");

while(isRunning)

{

Graphics g = getGraphics();

paint(g);

g.dispose();

try{Thread.sleep(100);}

catch(InterruptedException e){}

}

}

public void stop()

{

System.out.println("in Stop()");

isRunning = false;

}

public void paint(Graphics g)

{

System.out.println("In paint");

g.setColor(Color.black);

g.drawString("Test..." + loc, loc, loc);

loc += 5;

}

public void update(Graphics g)

{

paint(g);

}

}

Abusea at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...
# 12

GameManager has a KeyListener, but that's just a single if/else statement, so that's not it.

I ran the code you posted, and it did happen to run for a longer period of time than my code, but it also stops after a few iterations (around 20 or so). I wouldn't doubt this machine is screwed up, I'm currently on a WinME box, which I'm sure explains quite a bit, heh.

The only thing I can think of that may cause some problems is I do pass around the Graphics object to quite a few different methods, which may be posing a slowdown problem. Still don't quite understand why it would stop completely though, especially with active rendering.

In any, thanks to everyone who tried to help. It's really appreciated.

Oooskaa at 2007-7-16 4:54:21 > top of Java-index,Other Topics,Java Game Development...