Drawing a board using GUI

Hello, below is a code I wrote to draw a board using GUI. I want to locate the board in the center of my screen but I don`t know-how. Can someone help, please?
And, can you tell me how can I get access to each of the cells in the grid?

import processing.core.*;

import javax.swing.*;

public class Problem05 extends PApplet {

float scale = height / 2f;
float size;
float x = width / 7f;
float y = height / 6f;
float r;
float c;


public void settings() {

    fullScreen();
}

public void setup() {
    try {

        String boardSize = JOptionPane.showInputDialog("Enter the board`s size[4,12]: ");
        size = Float.parseFloat(boardSize);
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "incorrect input");
        System.exit(1);

    }
    if (size < 4 || size > 12) {
        JOptionPane.showMessageDialog(null, "incorrect input");
        System.exit(1);

    }

}

public void draw() {
    background(0, 0, 0);


    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            x = i * scale;
            y = j * scale;

            if ((i % 2 == 0) == (j % 2 == 0)) {
                fill(24, 77, 51);
            } else {
                fill(255, 255, 255);
            }
            stroke(0, 70, 0);
            rect(x + width / 3f, y + height / 6f, scale, scale);
  }
    }
}

public static void main(String[] args) {
    PApplet.main("Problem05");
}

}

1 Like

Hello,

Your focus should be x and y offset here:

rect(x + width / 3f, y + height / 6f, scale, scale);

The offset will change with the variable size.

Give it some thought and experiment a little.

:)

1 Like

done thank you very much

2 Likes