java - How do I get my JFrame that displays an array of JPanels to update itself? -
i'm building board game cs course, i've started engine - backbone - , i've perfected it, i'm still stuck on gui part though. in engine, have double array initialises board pieces, , has getwinner , move methods. in gui part, created jframe , set layout gridlayout, , i've double array there checks engine's array , prints board board pieces accordingly.. however, whenever move, engine's array registers move doesn't displayed on jframe.. i've no idea on how it.. here's main class in gui package:
package eg.edu.guc.loa.gui; import java.awt.*; import java.util.arraylist; import javax.swing.*; import eg.edu.guc.loa.engine.*; import eg.edu.guc.loa.engine.point; @suppresswarnings("serial") public class loa extends jframe{ static tiles[][] jboard; static board b = new board(); static color temp; static color col1 = color.dark_gray; static color col2 = color.light_gray; static jframe loa = new jframe(); jbutton b1, b2; public loa(){ jboard = new tiles[8][8]; loa.setlayout(new gridlayout(8, 8)); initboard(); loa.setsize(600, 600); loa.setvisible(true); loa.setlocationrelativeto(null); loa.setdefaultcloseoperation(jframe.exit_on_close); b.printboard(); } public static void initboard(){ remove(); b.printboard(); for(int = 0; i<8; i++){ if (i%2 == 0){ temp = col1; } else{ temp = col2; } for(int j = 0; j<8; j++){ jboard[i][j] = new tiles(temp, i, j); loa.getcontentpane().add(jboard[i][j]); if (temp.equals(col1)){ temp = col2; } else{ temp = col1; } if(b.getpiece(new point(j, i)).getplayer() == boardcell.player_1_piece){ jboard[i][j].haschecker(true); jboard[i][j].setwhite(true); jboard[i][j] = new tiles(temp, i, j); } if(b.getpiece(new point(j, i)).getplayer() == boardcell.player_2_piece){ jboard[i][j].haschecker(true); jboard[i][j].setwhite(false); jboard[i][j] = new tiles(temp, i, j); } } } } public static void remove(){ for(int = 0; i<8;i++){ for(int j = 0; j<8; j++){ if(jboard[i][j] != null) loa.remove(jboard[i][j]); } } } public static void main (string [] args){ new loa(); } }
any appreciated.. in advance.
edit: b.print() prints engine's array on console, , remove() attempt on removing old frame , building new 1 based on new updated array (with moved piece), failed.
as shown in simpler mvcgame
, can arrange view register listener model using observer pattern. user gestures should update model; when notified model, view should render current state of model. there should no drawing in model , no game logic in view.
Comments
Post a Comment