'Translating coordinates from 9 layouts within a larger one, to two-dimensional array?
So I have a Sudoku grid which consists of one large 3x3 GridLayout and each of its elements consists of a smaller 3x3 GridLayout of JTextFields. My issue here is that my Sudoku will be in the form of a 2D array but the layout structure does not look the same. The image below shows how the coordinates look like in the layout form. My question is if and then how I can "translate" the coordinates from array form to "layout"-form?
(I guess a problem could be that the outer GridLayout loops 9 times linearly while the inner ones loop 3x3 in 2D)
int[][] matrix = s.getMatrix();
JPanel p1 = new JPanel();
gl.setHgap(4);
gl.setVgap(4);
p1.setBackground(Color.black);
p1.setSize(WIDTH, (HEIGHT - PANEL_HEIGHT));
p1.setLayout(gl);
for(int i = 0; i < 9; i++) {
JPanel p = new JPanel();
GridLayout local_gl = new GridLayout(3,3);
local_gl.setHgap(2);
local_gl.setVgap(2);
p.setBackground(Color.black);
p.setSize(WIDTH / 3, (HEIGHT - PANEL_HEIGHT) / 3);
p.setLayout(local_gl);
p1.add(p);
for(int row = 0; row < 3; row++) {
for(int col = 0; col < 3; col++) {
JTextField jtf = new JTextField(" " + Integer.toString(matrix[row][col]));
if(jtf.getText().trim().equals("0")) {
jtf.setText(" ");
}
jtf.setFont(new Font("Times New Roman", Font.BOLD, 30));
p.add(jtf);
}
}
}
This solution works.
JTextField jtf = new JTextField(" " + Integer.toString(matrix[translateRowToY(i, row)][translateColToX(i, col)]));
private int translateColToX(int i, int col) {
if(i == 2 || i == 5 || i == 8) {
return col + 6;
}else if(i == 1 || i == 4 || i == 7) {
return col + 3;
}else if(i == 0 || i == 3 || i == 6) {
return col;
}else {
return -1;
}
}
private int translateRowToY(int i, int row) {
if(i >= 0 && i < 3) {
return row;
}else if(i >= 3 && i < 6) {
return row + 3;
}else if(i >= 6 && i < 9) {
return row + 6;
}else {
return -1;
}
}
Solution 1:[1]
I would not use 9 panels with 9 inputs each; instead, I would use single panel with 81 inputs, arranged in a 9x9 grid. Instead of JTextFields, I would define a public static class SudokuCell extends JTextField, so that, each time you create a new cell, you can keep all its tracking information inside:
public static class SudokuCell extends JTextField {
public final int block; // 0 to 9, you call it `i`
public final int row; // 0 to 9 again
public final int col; // again 0-9
public SudokuCell(int block, int row, int col) {
// ... assign to attributes
}
}
The advantage is that interpreting clicks is now trivial: if you add a MouseListener to each of those cells, c.row will tell you the row it is on, c.block will reply with the block, and so on and so forth.
Initializing those cells is also very easy:
for (int row=0; row<DIM; row++) {
for (int col=0; col<DIM; col++) {
SudokuCell c = new SudokuCell((row/3)*3 + col/3, row, col);
// add to panel for display, and to internal DIMxDIM array too.
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tucuxi |

