Created
June 24, 2020 10:22
-
-
Save alrafiabdullah/a4793d392dcbf70349e29e1dc30d6341 to your computer and use it in GitHub Desktop.
Sudoku Graphical User Interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.elridge.sudoku.view; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Font; | |
import javax.swing.BorderFactory; | |
import javax.swing.JLabel; | |
/** | |
* This class represents a field on the SudokuPanel. | |
* | |
* @author Eric Beijer | |
*/ | |
public class Field extends JLabel { | |
private int x; // X position in game. | |
private int y; // Y position in game. | |
/** | |
* Constructs the label and sets x and y positions in game. | |
* | |
* @param x X position in game. | |
* @param y Y position in game. | |
*/ | |
public Field(int x, int y) { | |
super("", CENTER); | |
this.x = x; | |
this.y = y; | |
setPreferredSize(new Dimension(40, 40)); | |
setBorder(BorderFactory.createLineBorder(Color.GRAY)); | |
setFont(new Font(Font.DIALOG, Font.PLAIN, 20)); | |
setOpaque(true); | |
} | |
/** | |
* Sets number and foreground color according to userInput. | |
* | |
* @param number Number to be set. | |
* @param userInput Boolean indicating number is user input or not. | |
*/ | |
public void setNumber(int number, boolean userInput) { | |
setForeground(userInput ? Color.BLUE : Color.BLACK); | |
setText(number > 0 ? number + "" : ""); | |
} | |
/** | |
* Returns x position in game. | |
* | |
* @return X position in game. | |
*/ | |
public int getFieldX() { | |
return x; | |
} | |
/** | |
* Return y position in game. | |
* | |
* @return Y position in game. | |
*/ | |
public int getFieldY() { | |
return y; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.elridge.sudoku.view; | |
import java.awt.BorderLayout; | |
import javax.swing.JFrame; | |
import javax.swing.UIManager; | |
import nl.elridge.sudoku.controller.ButtonController; | |
import nl.elridge.sudoku.controller.SudokuController; | |
import nl.elridge.sudoku.model.Game; | |
/** | |
* Main class of program. | |
* | |
* @author Eric Beijer | |
*/ | |
public class Sudoku extends JFrame { | |
public Sudoku() { | |
super("Sudoku"); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
getContentPane().setLayout(new BorderLayout()); | |
Game game = new Game(); | |
ButtonController buttonController = new ButtonController(game); | |
ButtonPanel buttonPanel = new ButtonPanel(); | |
buttonPanel.setController(buttonController); | |
add(buttonPanel, BorderLayout.EAST); | |
SudokuPanel sudokuPanel = new SudokuPanel(); | |
SudokuController sudokuController = new SudokuController(sudokuPanel, game); | |
sudokuPanel.setGame(game); | |
sudokuPanel.setController(sudokuController); | |
add(sudokuPanel, BorderLayout.CENTER); | |
game.addObserver(buttonPanel); | |
game.addObserver(sudokuPanel); | |
pack(); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
} | |
/** | |
* Main entry point of program. | |
* | |
* @param args Command line arguments. | |
*/ | |
public static void main(String[] args) { | |
// Use System Look and Feel | |
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } | |
catch (Exception ex) { ex.printStackTrace(); } | |
new Sudoku(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.elridge.sudoku.view; | |
import java.awt.Color; | |
import java.awt.GridLayout; | |
import java.util.Observable; | |
import java.util.Observer; | |
import javax.swing.BorderFactory; | |
import javax.swing.JPanel; | |
import nl.elridge.sudoku.controller.SudokuController; | |
import nl.elridge.sudoku.model.Game; | |
import nl.elridge.sudoku.model.UpdateAction; | |
/** | |
* This class draws the sudoku panel and reacts to updates from the model. | |
* | |
* @author Eric Beijer | |
*/ | |
public class SudokuPanel extends JPanel implements Observer { | |
// Color constant for candidates. | |
private static final Color COLOR_CANDIDATE = new Color(102, 153, 255); | |
private Field[][] fields; // Array of fields. | |
private JPanel[][] panels; // Panels holding the fields. | |
/** | |
* Constructs the panel, adds sub panels and adds fields to these sub panels. | |
*/ | |
public SudokuPanel() { | |
super(new GridLayout(3, 3)); | |
panels = new JPanel[3][3]; | |
for (int y = 0; y < 3; y++) { | |
for (int x = 0; x < 3; x++) { | |
panels[y][x] = new JPanel(new GridLayout(3, 3)); | |
panels[y][x].setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY)); | |
add(panels[y][x]); | |
} | |
} | |
fields = new Field[9][9]; | |
for (int y = 0; y < 9; y++) { | |
for (int x = 0; x < 9; x++) { | |
fields[y][x] = new Field(x, y); | |
panels[y / 3][x / 3].add(fields[y][x]); | |
} | |
} | |
} | |
/** | |
* Method called when model sends update notification. | |
* | |
* @param o The model. | |
* @param arg The UpdateAction. | |
*/ | |
public void update(Observable o, Object arg) { | |
switch ((UpdateAction)arg) { | |
case NEW_GAME: | |
setGame((Game)o); | |
break; | |
case CHECK: | |
setGameCheck((Game)o); | |
break; | |
case SELECTED_NUMBER: | |
case CANDIDATES: | |
case HELP: | |
setCandidates((Game)o); | |
break; | |
} | |
} | |
/** | |
* Sets the fields corresponding to given game. | |
* | |
* @param game Game to be set. | |
*/ | |
public void setGame(Game game) { | |
for (int y = 0; y < 9; y++) { | |
for (int x = 0; x < 9; x++) { | |
fields[y][x].setBackground(Color.WHITE); | |
fields[y][x].setNumber(game.getNumber(x, y), false); | |
} | |
} | |
} | |
/** | |
* Sets fields validity according to given game. | |
* | |
* @param game Current game. | |
*/ | |
private void setGameCheck(Game game) { | |
for (int y = 0; y < 9; y++) { | |
for (int x = 0; x < 9; x++) { | |
fields[y][x].setBackground(Color.WHITE); | |
if (fields[y][x].getForeground().equals(Color.BLUE)) | |
fields[y][x].setBackground(game.isCheckValid(x, y) ? Color.GREEN : Color.RED); | |
} | |
} | |
} | |
/** | |
* Shows the candidates according to given game. | |
* | |
* @param game Current game. | |
*/ | |
private void setCandidates(Game game) { | |
for (int y = 0; y < 9; y++) { | |
for (int x = 0; x < 9; x++) { | |
fields[y][x].setBackground(Color.WHITE); | |
if (game.isHelp() && game.isSelectedNumberCandidate(x, y)) | |
fields[y][x].setBackground(COLOR_CANDIDATE); | |
} | |
} | |
} | |
/** | |
* Adds controller to all sub panels. | |
* | |
* @param sudokuController Controller which controls all user actions. | |
*/ | |
public void setController(SudokuController sudokuController) { | |
for (int y = 0; y < 3; y++) { | |
for (int x = 0; x < 3; x++) | |
panels[y][x].addMouseListener(sudokuController); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment