Compare commits
3 commits
Author | SHA1 | Date | |
---|---|---|---|
|
072da88135 | ||
|
b02e846ec6 | ||
|
970aabd532 |
2 changed files with 24 additions and 14 deletions
|
@ -1,6 +1,8 @@
|
||||||
package gui;
|
package gui;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import javax.swing.border.Border;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
|
||||||
|
@ -27,40 +29,48 @@ public class SudokuView extends JFrame {
|
||||||
setTitle("Sudoku Solver");
|
setTitle("Sudoku Solver");
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
|
setIconImage(new ImageIcon("src/main/resources/sudoku.png").getImage());
|
||||||
|
|
||||||
initializeGrid();
|
initializeGrid();
|
||||||
initializeButtons();
|
initializeButtons();
|
||||||
|
|
||||||
pack();
|
setMinimumSize(new Dimension(500, 500));
|
||||||
setLocationRelativeTo(null);
|
pack(); // Resize the window to fit the components, if necessary
|
||||||
|
setLocationRelativeTo(null); // Center the window
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Initialize the grid, called by the constructor */
|
/** Initialize the grid, called by the constructor */
|
||||||
private void initializeGrid() {
|
private void initializeGrid() {
|
||||||
grid = new JTextField[9][9];
|
grid = new JTextField[9][9];
|
||||||
JPanel gridPanel = new JPanel(new GridLayout(9, 9));
|
JPanel gridPanel = new JPanel(new GridLayout(9, 9));
|
||||||
|
gridPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
||||||
|
|
||||||
|
int fontSize = 24;
|
||||||
|
String fontName = gridPanel.getFont().getName();
|
||||||
|
Border border = BorderFactory.createLineBorder(Color.BLACK);
|
||||||
|
Font font = new Font(fontName, Font.BOLD, fontSize);
|
||||||
|
|
||||||
for (int row = 0; row < 9; row++) {
|
for (int row = 0; row < 9; row++) {
|
||||||
for (int col = 0; col < 9; col++) {
|
for (int col = 0; col < 9; col++) {
|
||||||
grid[row][col] = new JTextField(2);
|
JTextField cell = new JTextField(2);
|
||||||
grid[row][col].setHorizontalAlignment(JTextField.CENTER);
|
cell.setHorizontalAlignment(JTextField.CENTER);
|
||||||
|
cell.setFont(font);
|
||||||
|
cell.setBorder(border);
|
||||||
|
cell.setForeground(Color.BLACK);
|
||||||
|
|
||||||
// Set background color to gray for every third JTextField
|
// Set background color to gray for every third JTextField
|
||||||
if ((row / 3 + col / 3) % 2 == 1) {
|
if ((row / 3 + col / 3) % 2 == 1) {
|
||||||
grid[row][col].setBackground(Color.LIGHT_GRAY);
|
cell.setBackground(Color.LIGHT_GRAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
Font boldFont = new Font(grid[row][col].getFont().getName(), Font.BOLD, grid[row][col].getFont().getSize());
|
grid[row][col] = cell;
|
||||||
grid[row][col].setFont(boldFont);
|
gridPanel.add(cell);
|
||||||
grid[row][col].setForeground(Color.BLACK);
|
|
||||||
gridPanel.add(grid[row][col]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
add(gridPanel, BorderLayout.CENTER);
|
add(gridPanel, BorderLayout.CENTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Initialize the buttons, called by the constructor */
|
/** Initialize the buttons, called by the constructor */
|
||||||
private void initializeButtons() {
|
private void initializeButtons() {
|
||||||
solveButton = new JButton("Solve");
|
solveButton = new JButton("Solve");
|
||||||
|
|
BIN
app/src/main/resources/sudoku.png
Normal file
BIN
app/src/main/resources/sudoku.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.5 KiB |
Loading…
Reference in a new issue