Hello, my task is to create a new Sudoku object and then create a new SudokuWindow object, passing the Sudoku object as a constructor parameter.
I am given
SudokuWindow
import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class SudokuWindow extends JFrame { private static final Color lockedColor = new Color(233, 233, 233); private static final Color bgColor = Color.white; private static final Color hoverColor = new Color(211, 255, 211); private static final Border regionBorder = BorderFactory.createLineBorder(new Color(102, 102, 153)); private static final Border cellBorder = BorderFactory.createLineBorder(new Color(221, 221, 238)); private static final Border hoverBorder = BorderFactory.createLineBorder(Color.black); private static final Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR); private Sudoku sudoku; private State state; private int lockCount; private BoardPanel boardPanel; private StatusPanel statusPanel; public SudokuWindow(Sudoku sudoku) { this.sudoku = sudoku; state = State.configuring; lockCount = 0; boardPanel = new BoardPanel(); statusPanel = new StatusPanel(); JPanel boardContainer = new JPanel(); boardContainer.setBackground(bgColor); boardContainer.add(boardPanel); add(boardContainer, BorderLayout.CENTER); add(statusPanel, BorderLayout.SOUTH); pack(); setVisible(true); } class BoardPanel extends JPanel { public BoardPanel() { int size = sudoku.getSize(); setLayout(new GridLayout(size, size)); for (int row = 0; row < size; row++) for (int col = 0; col < size; col++) add(new RegionPanel(row, col)); } } class RegionPanel extends JPanel { private JTextField[][] fields; private boolean[][] locked; public RegionPanel(int regRow, int regCol) { final int size = sudoku.getSize(); final int bigSize = size*size; setLayout(new GridLayout(size, size)); setBorder(regionBorder); fields = new JTextField[size][size]; locked = new boolean[size][size]; for (int row = 0; row < size; row++) for (int col = 0; col < size; col++) { final int frow = row, fcol = col; final JTextField field = new JTextField(); field.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent event) { if (!locked[frow][fcol]) { field.requestFocus(); field.setBackground(hoverColor); field.setBorder(hoverBorder); } } public void mouseExited(MouseEvent event) { if (!locked[frow][fcol]) { field.setBackground(bgColor); field.setBorder(cellBorder); } } } ); field.setCursor(defaultCursor); field.setBorder(cellBorder); field.setPreferredSize(new Dimension(35, 35)); field.setHorizontalAlignment(JTextField.CENTER); field.setFont(new Font("Serif", Font.BOLD, 16)); final int region = regRow*size + regCol + 1; final int cell = row*size + col + 1; field.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent event) { if (locked[frow][fcol]) return; int number; try { number = Integer.parseInt(field.getText()); if (number > bigSize) number /= 10; if (number < 1) number = 1; } catch (Exception e) { number = Cell.BLANK; } boolean lock = state == State.configuring && number != Cell.BLANK; sudoku.enterNumber(region, cell, number, lock); number = sudoku.getNumber(region, cell); field.setText(number == Cell.BLANK ? "" : (""+number)); if (lock) { field.setEditable(false); field.setBackground(lockedColor); field.setBorder(cellBorder); lockCount++; if (lockCount >= size*size*size*size/3) { state = State.playing; statusPanel.update(); } } if (sudoku.isSolved()) { state = State.solved; statusPanel.update(); } locked[frow][fcol] |= lock; } } ); add(field); fields[row][col] = field; } } } class StatusPanel extends JPanel { private JLabel label; public StatusPanel() { setBackground(bgColor); add(label = new JLabel()); label.setFont(new Font("Serif", Font.BOLD, 18)); update(); } public void update() { label.setText(message()); } private String message() { switch (state) { case configuring: return "Please initialise the puzzle..."; case playing: return "Puzzle ready to solve"; case solved: return "It's SOLVED!!!"; default: return "Invalid state?"; } } } enum State { configuring, playing, solved } }
Sudoku
public class Sudoku { private Group reg1, reg2, reg3, reg4; private Group row1, row2, row3, row4; private Group col1, col2, col3, col4; public Sudoku() { Cell c1 = new Cell(), c2 = new Cell(), c3 = new Cell(), c4 = new Cell(); Cell c5 = new Cell(), c6 = new Cell(), c7 = new Cell(), c8 = new Cell(); Cell c9 = new Cell(), c10 = new Cell(), c11 = new Cell(), c12 = new Cell(); Cell c13 = new Cell(), c14 = new Cell(), c15 = new Cell(), c16 = new Cell(); reg1 = new Group(c1, c2, c5, c6); reg2 = new Group(c3, c4, c7, c8); reg3 = new Group(c9, c10, c13, c14); reg4 = new Group(c11, c12, c15, c16); row1 = new Group(c1, c2, c3, c4); row2 = new Group(c5, c6, c7, c8); row3 = new Group(c9, c10, c11, c12); row4 = new Group(c13, c14, c15, c16); col1 = new Group(c1, c5, c9, c13); col2 = new Group(c2, c6, c10, c14); col3 = new Group(c3, c7, c11, c15); col4 = new Group(c4, c8, c12, c16); } public void enterNumber(int region, int cell, int number, boolean lock) { getRegion(region).enterNumber(cell, number, lock); } public int getNumber(int region, int cell) { return getRegion(region).getCell(cell).getNumber(); } private Group getRegion(int regionNumber) { switch (regionNumber) { case 1: return reg1; case 2: return reg2; case 3: return reg3; case 4: return reg4; } return null; } public boolean isSolved() { return reg1.isSolved() && reg2.isSolved() && reg3.isSolved() && reg4.isSolved() && row1.isSolved() && row2.isSolved() && row3.isSolved() && row4.isSolved() && col1.isSolved() && col2.isSolved() && col3.isSolved() && col4.isSolved(); } public void print() { System.out.println(); row1.print(); row2.print(); row3.print(); row4.print(); System.out.println(); } public int getSize() { return 2; } }
My attempt to link the Window to the main.
import java.util.*; public class GraphicalMain { private static Scanner keyboard = new Scanner(System.in); private static SudokuWindow sudokuWindow; private static Sudoku sudoku; public static void main(String[] args) { sudoku = new Sudoku(); sudoku = new SudokuWindow(Sudoku, sudoku); } }
Please any advice or help would be greatly appreicated.
I'm getting the error at
saying [ or ( expected but I think that isn't the problem.sudoku = new SudokuWindow(Sudoku, sudoku);
Thanks