Browse Source
refactoring of DTSUD-47 into class - added test framework in maven and worte test for setupField and implemented gameField.
remotes/origin/develop
refactoring of DTSUD-47 into class - added test framework in maven and worte test for setupField and implemented gameField.
remotes/origin/develop
fdai6499
2 years ago
3 changed files with 138 additions and 73 deletions
@ -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; |
||||
|
} |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue