From d23bda7a00b26c38e4a7b7872b7371736d9863da Mon Sep 17 00:00:00 2001 From: fdai6499 Date: Tue, 10 Jan 2023 05:42:41 +0100 Subject: [PATCH] refactoring of DTSUD-47 into class - added test framework in maven and worte test for setupField and implemented gameField. --- src/test/java/MainTest.java | 19 ++--- sudokuExample/src/GameField.java | 74 +++++++++++++++++++ sudokuExample/src/Main.java | 118 +++++++++++++++---------------- 3 files changed, 138 insertions(+), 73 deletions(-) create mode 100644 sudokuExample/src/GameField.java diff --git a/src/test/java/MainTest.java b/src/test/java/MainTest.java index ca38d57..6f2536e 100644 --- a/src/test/java/MainTest.java +++ b/src/test/java/MainTest.java @@ -9,13 +9,11 @@ class MainTest extends Main { @org.junit.jupiter.api.Test void testField() { - JPanel setupGameField = setupGameField(360); - - JLabel[][] labelField = getLabelField(); + GameField gameField = new GameField(360); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { - labelField[i][j].setText(String.valueOf(j)); + gameField.setValue(i, j, j); } } @@ -23,22 +21,15 @@ class MainTest extends Main { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { - String s = labelField[i][j].getText(); - try { - Integer integer = Integer.valueOf(s); - if (j != integer) { - a = false; - } - } catch (NumberFormatException e) { - assertTrue(false); + int value = gameField.getValue(i, j); + if (j != value) { + a = false; } } } assertTrue(a); - - } @org.junit.jupiter.api.Test diff --git a/sudokuExample/src/GameField.java b/sudokuExample/src/GameField.java new file mode 100644 index 0000000..e5d8162 --- /dev/null +++ b/sudokuExample/src/GameField.java @@ -0,0 +1,74 @@ +import javax.swing.*; +import java.awt.*; + +public class GameField extends JPanel { + + private static JPanel[][] gameField; + private static JLabel[][] labelField; + + /** + * represents the game filed of sudoku + * @param widthHeight describes the field size in pixels which will be seperated into single sudoku fields + */ + public GameField(int widthHeight) { + + int nrOfFields = 9; + int border = 1; + int size = widthHeight/nrOfFields - (nrOfFields*border)/nrOfFields; + + this.setVisible(true); + this.setLayout(null); + + gameField = new JPanel[nrOfFields][]; + labelField = new JLabel[nrOfFields][]; + + int gapi = 0, gapj = 0; + + for (int i = 0; i < nrOfFields; i++) { + + gameField[i] = new JPanel[nrOfFields]; + labelField[i] = new JLabel[nrOfFields]; + + for (int j = 0; j < nrOfFields; j++) { + + gameField[i][j] = new JPanel(); + gameField[i][j].setBounds( + j * size + border*(j+1), + i * size + border*(i+1), + size, size); + gameField[i][j].setBackground(Color.BLUE); + gameField[i][j].setVisible(true); + this.add(gameField[i][j]); + + labelField[i][j] = new JLabel("", SwingConstants.CENTER); + labelField[i][j].setBackground(Color.GREEN); + labelField[i][j].setForeground(Color.MAGENTA); + labelField[i][j].setFont(new Font("Times new Roman", Font.BOLD, 25)); + + labelField[i][j].setBackground(Color.CYAN); + labelField[i][j].setText("0"); + + gameField[i][j].add(labelField[i][j]); + + } + } + } + + public void setValue(int x, int y, int value) { + + if (value > 0 && value < 10) { + if (x > -1 && x < 9 && y > -1 && y < 9) { + labelField[x][y].setText(String.valueOf(value)); + } + } + } + + public int getValue(int x, int y) { + if (x > -1 && x < 9 && y > -1 && y < 9) { + Integer value = Integer.valueOf(labelField[x][y].getText()); + return value; + } + return -1; + } + +} diff --git a/sudokuExample/src/Main.java b/sudokuExample/src/Main.java index e02ffa2..e515677 100644 --- a/sudokuExample/src/Main.java +++ b/sudokuExample/src/Main.java @@ -6,16 +6,16 @@ import java.awt.event.ActionListener; public class Main { - private static JPanel[][] gameField; - private static JLabel[][] labelField; - - public static JPanel[][] getGameField() { - return gameField; - } - - public static JLabel[][] getLabelField() { - return labelField; - } +// private static JPanel[][] gameField; +// private static JLabel[][] labelField; +// +// public static JPanel[][] getGameField() { +// return gameField; +// } +// +// public static JLabel[][] getLabelField() { +// return labelField; +// } public static void main(String[] args) { @@ -35,9 +35,9 @@ public class Main { rootPanel.setBackground(Color.LIGHT_GRAY); rootPanel.setLayout(null); - JPanel gamePanel = setupGameField(360); - gamePanel.setBounds(70, 50, 360, 360); - rootPanel.add(gamePanel); + GameField gameField = new GameField(360); + gameField.setBounds(70, 50, 360, 360); + rootPanel.add(gameField); main.setContentPane(rootPanel); @@ -57,52 +57,52 @@ public class Main { }); } - protected static JPanel setupGameField(int widthHeight) { - - int nrOfFields = 9; - int border = 1; - int size = widthHeight/nrOfFields - (nrOfFields*border)/nrOfFields; - - JPanel fieldRoot = new JPanel(); - fieldRoot.setVisible(true); - fieldRoot.setLayout(null); - - gameField = new JPanel[nrOfFields][]; - labelField = new JLabel[nrOfFields][]; - - int gapi = 0, gapj = 0; - - for (int i = 0; i < nrOfFields; i++) { - - gameField[i] = new JPanel[nrOfFields]; - labelField[i] = new JLabel[nrOfFields]; - - for (int j = 0; j < nrOfFields; j++) { - - gameField[i][j] = new JPanel(); - gameField[i][j].setBounds( - j * size + border*(j+1), - i * size + border*(i+1), - size, size); - gameField[i][j].setBackground(Color.BLUE); - gameField[i][j].setVisible(true); - fieldRoot.add(gameField[i][j]); - - labelField[i][j] = new JLabel("", SwingConstants.CENTER); - labelField[i][j].setBackground(Color.GREEN); - labelField[i][j].setForeground(Color.MAGENTA); - labelField[i][j].setFont(new Font("Times new Roman", Font.BOLD, 25)); - - labelField[i][j].setBackground(Color.CYAN); - labelField[i][j].setText("0"); - - gameField[i][j].add(labelField[i][j]); - - } - } - - return fieldRoot; - } +// protected static JPanel setupGameField(int widthHeight) { +// +// int nrOfFields = 9; +// int border = 1; +// int size = widthHeight/nrOfFields - (nrOfFields*border)/nrOfFields; +// +// JPanel fieldRoot = new JPanel(); +// fieldRoot.setVisible(true); +// fieldRoot.setLayout(null); +// +// gameField = new JPanel[nrOfFields][]; +// labelField = new JLabel[nrOfFields][]; +// +// int gapi = 0, gapj = 0; +// +// for (int i = 0; i < nrOfFields; i++) { +// +// gameField[i] = new JPanel[nrOfFields]; +// labelField[i] = new JLabel[nrOfFields]; +// +// for (int j = 0; j < nrOfFields; j++) { +// +// gameField[i][j] = new JPanel(); +// gameField[i][j].setBounds( +// j * size + border*(j+1), +// i * size + border*(i+1), +// size, size); +// gameField[i][j].setBackground(Color.BLUE); +// gameField[i][j].setVisible(true); +// fieldRoot.add(gameField[i][j]); +// +// labelField[i][j] = new JLabel("", SwingConstants.CENTER); +// labelField[i][j].setBackground(Color.GREEN); +// labelField[i][j].setForeground(Color.MAGENTA); +// labelField[i][j].setFont(new Font("Times new Roman", Font.BOLD, 25)); +// +// labelField[i][j].setBackground(Color.CYAN); +// labelField[i][j].setText("0"); +// +// gameField[i][j].add(labelField[i][j]); +// +// } +// } +// +// return fieldRoot; +// } } // JFrame jFrame = new JFrame();