java - JFrame update when data change -
how update jframe
display when data in program changes?
a bit of context. i'm working on sort of ant colony simulation (it's not school project. well... not school ;p (i'm not helping either, i'm doing fun))
in simulation, try display map of current state of simulation, displaying position of food, ant, colony, rocks...
what i'm doing currently: have "two maps". each 2d array. first 1 contain id of each element, other 1 contain icon (at right position of array/map) shown user, , display important object of case (for example, if ant on same square colony, colony show up).
the data behind each map work fine , contain want.
the problem that, once simulation start running, frame still stuck on cycle 0.
so (for 1 didn't read above), how can update frame display when map data changed?
some bits of current code: edit: compilable version cheap way make value change periodically
my "main"
public class finit { public static string[][] textgrid= {{"+", "-", "-", "-", "-", "-", "+"}, {"|", " ", " ", " ", " ", "#", "|"}, {"|", " ", " ", " ", " ", " ", "|"}, {"|", " ", " ", " ", "o", " ", "|"}, {"|", " ", " ", " ", " ", " ", "|"}, {"|", " ", "#", "o", "©", "o", "|"}, {"+", "-", "-", "-", "-", "-", "+"}}; public static void main(string[] args) throws interruptedexception{ ffenetre fenetre = new ffenetre(); fenetre.fmapwindows(textgrid.length, textgrid[0].length); while(true){ fenetre.repaint(); thread.sleep(500); if(textgrid[5][4].equals("©")) textgrid[5][4]=" "; else textgrid[5][4]="©"; } } }
the other relevant bit:
public class ffenetre extends jpanel { public ffenetre test; public void fmapwindows(int x, int y){ jframe frame = new jframe("carte"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize((x+1)*20, (y+2)*20+5); frame.setlocationrelativeto(null); ffenetre test = new ffenetre(); frame.add(test); frame.setvisible(true); //frame.setcontentpane(new fgraphiquemapicones()); } @override public void paintcomponent(graphics g){ super.paintcomponent(g); font font = new font("monospaced", font.plain, 20); g.setfont(font); for(int a=0; a<finit.textgrid.length; a++){ for(int b=0; b<finit.textgrid[0].length; b++){ g.drawstring(finit.textgrid[b][a], (a)*20+5, (b+1)*20); } } } }
i don't know doing after @override
part, tried implement solutions of different people, none made handle wanted (not button nor mouse movement)
Comments
Post a Comment