Browse Source

DTSUD-47 - added test framework in maven and worte test for setupField and implemented gameField.

remotes/origin/develop
fdai6499 2 years ago
parent
commit
843dd7663d
  1. 8
      pom.xml
  2. 48
      src/test/java/MainTest.java
  3. 64
      sudokuExample/src/Main.java

8
pom.xml

@ -13,5 +13,13 @@
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

48
src/test/java/MainTest.java

@ -0,0 +1,48 @@
import org.junit.Test;
import javax.swing.*;
import static org.junit.jupiter.api.Assertions.*;
class MainTest extends Main {
@org.junit.jupiter.api.Test
void testField() {
JPanel setupGameField = setupGameField(360);
JLabel[][] labelField = getLabelField();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
labelField[i][j].setText(String.valueOf(j));
}
}
boolean a = true;
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);
}
}
}
assertTrue(a);
}
@org.junit.jupiter.api.Test
void testBla() {
assertTrue(true);
}
}

64
sudokuExample/src/Main.java

@ -6,8 +6,18 @@ import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {
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) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
@ -25,6 +35,10 @@ public class Main {
rootPanel.setBackground(Color.LIGHT_GRAY);
rootPanel.setLayout(null);
JPanel gamePanel = setupGameField(360);
gamePanel.setBounds(70, 50, 360, 360);
rootPanel.add(gamePanel);
main.setContentPane(rootPanel);
JButton back = new JButton();
@ -38,9 +52,57 @@ public class Main {
newButton.setBounds(395, 5, 100, 25);
newButton.setText("New");
rootPanel.add(newButton);
}
});
}
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();

Loading…
Cancel
Save