Hi
I’ve just started using processing. I’ll put the code below, but first the problem.
I have to setup a 14x14 chessboard in a 672x672 board (plus a 120 pixel-wide sidebar, where each tile is 48 pixels square. I set up a 2D array, and create a Tile object, which knows its array reference (eg 0,1 for 2nd tile in 1st array) which I refer to as board_x and board_y. The pixel_x is equal to board_x x 48, and pixel_y is board_y x 48. Each Tile has a draw method. From the main app, I iterate over the array containing the tiles, to call the draw method and print the board. The board is the right width, but the bottom row is only partially visible in the canvas, as you can see in the attached image.
Code fragments below:
// dimensions variables
public static final int CELLSIZE = 48;
public static final int SIDEBAR = 120;
public static int HEIGHT = 14 * CELLSIZE; // = 672
public static int WIDTH = HEIGHT + SIDEBAR; // = 792
// settings
size(WIDTH,HEIGHT)
// in setup() instantiate the board of tiles (one per Board array cell)
for (int x = 0; x < BOARD_WIDTH; x++) {
for (int y = 0; y < BOARD_WIDTH; y++) {
this.B[x][y] = new Tile(x, y, this);
}
}
// in the draw() method of the main app
for (int x = 0; x < BOARD_WIDTH; x++) {
for (int y = 0; y < BOARD_WIDTH; y++) {
this.B[x][y].draw();
}
}
// Tile class
public class Tile {
App app; // access to the canvas
int boardX, boardY, pixelX, pixelY, cellSize;
String base_colour; // dark or light for tile
public Tile(int x, int y, App app) {
this.app = app;
this.cellSize = app.CELLSIZE;
this.boardX = x;
this.boardY = y;
this.pixelX = this.boardX * this.cellSize;
this.pixelY = this.boardY * this.cellSize;
// set base colour
if ((this.boardX + this.boardY) % 2 == 0) {
this.base_colour = "light";
} else {
this.base_colour = "dark";
}
}
public void tick() {
}
public void draw() {
this.app.setFillColour(this.base_colour);
System.out.println("Draw tile (" + this.boardX + "," +
this.boardY + "); pixel ref = ("
+ this.pixelX + "-" + (this.pixelX + this.cellSize) + ","
+ this.pixelY + "-" + (this.pixelY + this.cellSize) + ")");
this.app.rect(this.pixelX, this.pixelY, this.cellSize, this.cellSize);
}
}