Ok well here’s the code that reads the bricks file (i.e. tile) and stores the information line by line in an array:
private void storeBricks(String line, int lineNo, int numImages)
/* Read a single bricks line, and create Brick objects.
A line contains digits and spaces (which are ignored). Each
digit becomes a Brick object.
The collection of Brick objects are stored in the bricksList
ArrayList.
*/
{
int imageID;
for(int x=0; x < line.length(); x++) {
char ch = line.charAt(x);
if (ch == ' ') // ignore a space
continue;
if (Character.isDigit(ch)) {
imageID = ch - '0'; // we assume a digit is 0-9
if (imageID >= numImages)
System.out.println("Image ID " + imageID + " out of range");
else // make a Brick object
bricksList.add( new Brick(imageID, x, lineNo) );
}
else
System.out.println("Brick char " + ch + " is not a digit");
}
} // end of storeBricks()
And this is the code that reads the brick arrays (they get transfered into columns rather than rows earlier on in the code) and displays them column by column:
private void drawBricks(Graphics g, int xStart, int xEnd, int xBrick)
/* Draw bricks into the JPanel starting at xStart, ending at xEnd.
The bricks are drawn a column at a time, separated by imWidth pixels.
The first column of bricks drawn is the one at the xBrick location
in the bricks map.
*/
{ int xMap = xBrick/imWidth; // get the column position of the brick
// in the bricks map
// System.out.println("xStart: " + xStart + "; xEnd: " + xEnd);
// System.out.println("xBrick: " + xBrick + "; xMap: " + xMap);
ArrayList column;
Brick b;
for (int x = xStart; x < xEnd; x += imWidth) {
column = columnBricks[ xMap ]; // get the current column
for (int i=0; i < column.size(); i++) { // draw all bricks
b = (Brick) column.get(i);
b.display(g, x); // draw brick b at JPanel posn x
}
xMap++; // examine the next column of bricks
}
} // end of drawBricks()
If you can see any obvious mistakes in those lines of code, please tell me, I can’t see wood for trees at the moment!