swing - Java Application : repaint isn't executed in While Loop? -
i've got thread
supposed update game's elements, i.e. repaint, wait bit of time again, problem doesn't repaint. i've tested every other component, works, paint method called once ( because components painted ), if call repaint()
in loop. here's code of loop:
thread t = new thread(){ public void run() { mouse.init(); while(true) { mouse.refresh();//adds dirty regions in repaintmanager swingutilities.invokelater(new runnable() { @override public void run() { //what here? } }); } } };
no need see thread or anything, loops.
here's paint method :
@override public void paint(graphics g) { endtime = system.currenttimemillis();//for 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 want point out made game applet before , working charm, need application.
i think data not synchronized between threads (there not enough code determine if case tough).
repaint
called on swing edt , update.updategravity()
called in main thread. guess update
changes variable
, ever synchronize data structure such swing edt sees updated value?
edit:
here should do:
variables = update.updategravity(); // runs in main thread swingutilities.invokelater(new runnable() { @override public void run() { yourcustomswingcomponent.update(variables); // run on swing edt } }
and yourcustomswingcomponent
contains paint()
method have. there rule in swing state of swing component can modified (or read) swing edt.
you should read swing , multi-threading. actually, must read multi-threading use swing.
Comments
Post a Comment