java - GUI thread updating issue -
i have gui class gui:
public class gui extends jframe implements runnable { private jpanel outer, inner; private jlabel[][] labels = new jlabel[22][12]; private color[][] defaultmap, map; thread t; private int row, col; private color color; public gui() { container content = getcontentpane(); content.setlayout(new borderlayout()); setbackground(color.black); setsize(1000, 1000); setlocation(300, 0); setdefaultcloseoperation(jframe.exit_on_close); setresizable(false); defaultmap = createmap(); draw(defaultmap); } public color[][] createmap() { color[][] map = new color[22][12]; (int = 0; < 22; i++) { (int j = 0; j < 12; j++) { map[i][j] = color.white; } } (int = 0; < 22; i++) { map[i][0] = color.gray; map[i][11] = color.gray; } (int = 0; < 12; i++) { map[0][i] = color.gray; map[21][i] = color.gray; } return map; } public void draw(color[][] map) { outer = new jpanel(); outer.setlayout(new borderlayout()); outer.setbackground(color.white); outer.setpreferredsize(new dimension()); inner = new jpanel(); inner.setlayout(new gridlayout(22, 12, 2, 2)); inner.setbackground(color.black); (int = 0; < 22; i++) { (int j = 0; j < 12; j++) { labels[i][j] = new jlabel(); jlabel label = labels[i][j]; label.setpreferredsize(new dimension(20, 20)); label.setbackground(map[i][j]); label.setopaque(true); inner.add(label); } } add(outer); add(inner); pack(); } public void move(int row, int col, color color) { system.out.println(row+","+col); map = defaultmap; map[row][col] = color; t = new thread(this); t.start(); } @override public void run() { draw(map); } } which called main class so:
public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { try { gui = new gui(); gui.setvisible(true); gui.move(2,5,color.green); thread.sleep(1000); gui.move(3,5,color.green); thread.sleep(1000); gui.move(4,5,color.green); } catch (interruptedexception ex) { logger.getlogger(tetris.class.getname()).log(level.severe, null, ex); } } }); } so weird stuff happening when gui.move() function called. can ignore rest or use if helps. each time, green block should "added" gui @ 2,5; 3,5; , 4,5; 1 second after other.
the issue:
the gui remains black time repaints/updates proper grid , colors first 2 blocks correctly colored green it's missing last block @ 4,5. again, initial "defaultmap" should painted immediately, not, jframe black until painted @ once minus last green block. each green block should painted on 1 second after other.
the interesting part system.out.println() bit in move method in gui prints out row , col i'd expect to... come out 1 second after other in terminal. tells me going right. i'm not sure what's going on gui...
edit: slight difference in story. upon closer examination, noticed last green block appear second entire map painted "disappears" repainted white.
you sleeping on edt (event dispatching thread). should never block edt.
- never call
sleeporwaiton edt - move long running tasks other threads (using
executorsorswingworker) - for repeated or delayed update of ui, can use
javax.swing.timer - all ui-operations should performed on edt (using
swingutilities.invokelaterorjavax.swing.timerorswingworker
read more swing concurrency in swing tag wiki.
Comments
Post a Comment