I’ve got this Thread that is supposed to Update the game’s elements, i.e. repaint, wait for a bit of time then do it again, the problem is that it doesn’t repaint. I’ve tested every other component, they all works, but the paint method is only called once ( Because the components are painted ), even if I call repaint() in the loop. Here’s my Code of the Loop:
Thread t = new Thread(){
public void run()
{
mouse.init();
while(true)
{
mouse.Refresh();//Adds Dirty Regions in the RepaintManager
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//What here?
}
});
}
}
};
No need to see the thread or anything, it loops.
Here’s the Paint Method :
@Override
public void paint(Graphics g)
{
EndTime = System.currentTimeMillis();//For the FPS Counter
Time = EndTime - StartTime;
FPS = (byte) (1000/Time);
TotalFPS += FPS;
TotalFrame++;
JPT.AverageFPs.setText( "" + TotalFPS/TotalFrame);
JPT.CurrentFPS.setText( "" + FPS);
StartTime = System.currentTimeMillis();
g.clearRect(0,0,dim.width,dim.width);
for(int x = 0; x < Variables.Width; x++)
{
for(int y = 0; y < Variables.Height; y++)
{
if(Variables.Map[x][y] == 0)
{
g.setColor(new Color(0x613F37));
g.drawLine(x, y, x, y);
}
else if(Variables.Map[x][y] == 1)
{
g.setColor(Color.black);
g.drawLine(x, y, x, y);
}
else if(Variables.Map[x][y] == 2)
{
g.setColor(new Color(0xDEDEDE));
g.drawLine(x, y, x, y);
}
}
}
g.setColor( new Color(0.5f, 0.5f, 0.5f, 0.5f));
g.fillRect(Variables.CurrentX, Variables.CurrentY, Variables.Zoom, Variables.Zoom);
}
Thanks alot in advance.
Also I want to point out that I made this Game as an Applet before and it was working like a charm, but now I need it as Application.
I think your data is not synchronized between threads (there is not enough code to determine if this the case tough).
repaintis called on the Swing EDT andupdate.UpdateGravity()is called in your main thread. I guess yourupdatechangesVariable, but do you ever synchronize this data structure such that the Swing EDT sees the updated value?EDIT:
here is what you should do:
and
yourCustomSwingComponentcontains thepaint()method you have. There is a rule in Swing that the state of any Swing component can only be modified (or read) from the Swing EDT.You should read about Swing and multi-threading. Actually, you must read about multi-threading to use Swing.