Java - Display a 2d array of data

Status
Not open for further replies.

snotnose

Ars Praefectus
3,454
Subscriptor
I have a 2d array of ascii chars that I want to display. For example, think of a chessboard with ' ' as an mt white square, '.' an mt black square, 'k' is a white king, 'K' a black king, etc. I have a 'char [][]' board = new char [8][8];'. I want to see:

Code:
 . . . . .
P . . . . 
c. .PK . .
. . . CP. 
c. . . . .
. . . pk. 
 . . . . .

How would you print this thing out? I'm trying to use JTextArea, but unfortunately it seems to treat the requested size as a suggestion and does it's own thing.

Note my app isn't chess related, and the size is determined at runtime based on the data.

My code is:

Code:
    // (elsewhere...  xsize and ysize are globals specifying the size of the data grid)
    JTextArea display = new JTextArea(ysize, xsize);


    public static void displayBoard(JTextArea display, char [][] data)
    {
        int offset = 0;          // avoids lots of multiplications
        String[] line = new String[ysize];

        for(int y = 0; y < ysize; y++)
        {
            line[y] = new String(data[y]);
            display.replaceRange(line[y], offset, offset + xsize);
            offset += xsize;
        }
    }

The problem is, instead of an 8x8 display, I'm getting a 2x50 display. Which really doesn't look like what I want.
 
Status
Not open for further replies.