Results 1 to 4 of 4
Thread: Connect 4 - from console to GUI
- 03-15-2011, 04:00 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Connect 4 - from console to GUI
I have a fully working version of connect four for the console. Now I would like to get it into a nice gui.
I use a class BoardPanel extends JPanel, in that class I create a 2D array of JLabels and fill the panel with them.
Now I'm looking for a way to "sync" my console board (a 2D array) with my array of JLabels.Java Code:grid= new JLabel[rows][columns]; for (int x = 0; x < rows; x++){ for (int y = 0; y < columns; y++){ grid[x][y] = new JLabel(empty); //empty is an ImageIcon grid[x][y].setBorder(new LineBorder(Color.BLACK)); super.add(grid[x][y]); } }
Console board:
Java Code:public void setBoard () { for (int indexRow = 0; indexRow <rows; indexRow ++) { for (int indexColumn = 0; indexColumn < columns; indexColumn ++) { board [indexRow] [indexColumn] = empty; //here 'empty' is a ' '-character } } public String show () { String s = ""; for (int indexRow= 0; indexRow<rows; indexRow++) {/ / print the numbers for the first row s + = String.format ("% d", rows - indexRow); for (int indexColumn = 0; indexColumn <columns; indexColumn++) { s + = String.format ("% c", board [indexRow] [indexColumn]); } s + = String.format ("\ n "); } s + = ""; for (int index = 1; index <= columns; index + +) s + = "" + index + ""; return s; }
-
So what part of the code are you having trouble with?
To sync the board with the array of JLabels is pretty straight-forward:
every time a move is made, your 2D board array stores the data of the current board, and you should have an "updateBoard()" method which translates the data to the GUI by either setting the JLabel text or icons depending on the data in the array.
- 03-16-2011, 06:04 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
My biggest problem is the updatemethod, I don't really know how to do that.
-
okay so you have to make a GUI first, make all the buttons on the board and load them into a 2D array to match your 2D data array.
Java Code:JButton[][] BoardArray = new JButton[8][8]; //allocate for 8x8 board of buttons
then every time a move is made, loop through your data array & translate the info e.g.
Java Code:for (int i=0; i<BoardArray[0].length; i++) { for (int j=0; j<BoardArray[0].length; j++) { BoardArray[i][j].setText("boardData[i][j]"); } }
Now, i'm sure you don't want to just setText, but you get the picture...
Similar Threads
-
Java Console
By stiwi in forum Java SoftwareReplies: 4Last Post: 05-19-2010, 02:53 PM -
implementing a console
By bilbodeb in forum Threads and SynchronizationReplies: 2Last Post: 03-26-2009, 07:09 AM -
Starting the console on Mac
By ScottVal in forum New To JavaReplies: 4Last Post: 03-19-2009, 07:24 AM -
Console doesn't appear!
By PeteMarsh in forum New To JavaReplies: 2Last Post: 12-17-2007, 05:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks