Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is the package name
- package jsudoku.graphics;
- //here i am importing external classes and attributes
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.GridLayout;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.border.LineBorder;
- import jsudoku.Sudoku;
- //Here I am inheriting from the class JPanel
- public class JSudoku extends JPanel {
- /**
- * The class KeyAdapter is an abstract (adapter) class for receiving
- * keyboard events. All methods of this class are empty.
- * This class is convenience class for creating listener objects.
- */
- private class Key extends KeyAdapter {
- private int x;
- private int y;
- public Key(int pX, int pY) {
- this.x = pX;
- this.y = pY;
- }
- @Override
- //The method keyPressed is created
- public void keyPressed(KeyEvent e) {
- char pressed = e.getKeyChar();
- //The user can insert numbers 1 to 9 only in the game
- if (pressed == '1' || pressed == '2' || pressed == '3' || pressed == '4' || pressed == '5' || pressed == '6' || pressed == '7' || pressed == '8' || pressed == '9') {
- //the number is added into the sudoku.addNumber and it is an integer
- sudoku.addNumber(this.x, this.y, Integer.parseInt(pressed + ""));
- ground[this.x][this.y].setBorder(new LineBorder(Color.BLACK));
- ground[this.x][this.y].removeKeyListener(this);
- repaintSudoku();
- }else if(e.getKeyCode()==KeyEvent.VK_DELETE){
- sudoku.emptyNumber(this.x, this.y);
- ground[this.x][this.y].setBorder(new LineBorder(Color.BLACK));
- ground[this.x][this.y].removeKeyListener(this);
- repaintSudoku();
- }
- }
- }
- //Here I am inheriting from the class MouseAdapter
- private class Mouse extends MouseAdapter {
- private int x;
- private int y;
- public Mouse(int pX, int pY) {
- this.x = pX;
- this.y = pY;
- }
- // the method overrides MouseEvent 'e' into the method
- //This methods helps the user understand if the number inserted by him is correct or not
- //If he enter wrong number then the box will be red bordered
- //Otherwise the selected box will be simply black
- @Override
- public void mouseClicked(MouseEvent e) {
- for (int i = 0; i < 9; i++) {
- for (int j = 0; j < 9; j++) {
- ground[i][j].setBorder(new LineBorder(Color.BLACK));
- KeyListener[] remove = ground[i][j].getKeyListeners();
- if (remove.length != 0) {
- ground[i][j].removeKeyListener(remove[0]);
- }
- }
- }
- ground[this.x][this.y].requestFocus();
- ground[this.x][this.y].setBorder(new LineBorder(Color.RED));
- ground[this.x][this.y].addKeyListener(new Key(this.x, this.y));
- }
- }
- private Sudoku sudoku;
- private JLabel[][] ground;
- public JSudoku() {
- this(new Sudoku());
- }
- public Sudoku getSudoku() {
- return this.sudoku;
- }
- //This sets the dimention and the color of the grid (the background color included)
- public JSudoku(Sudoku pSudoku) {
- this.sudoku = pSudoku;
- this.setSize(300, 300);
- this.setBackground(Color.WHITE);
- this.setLayout(new GridLayout(9, 9));
- this.initLabel();
- }
- //This is the method setSudoku
- public void setSudoku(Sudoku pSudoku) {
- this.sudoku = pSudoku;
- this.repaintSudoku();
- this.initListeners();
- }
- //This is the method initListeners
- public void initListeners() {
- int[][] sud = this.sudoku.getSudoku();
- for (int i = 0; i < 9; i++) {
- for (int j = 0; j < 9; j++) {
- MouseListener[] listener=this.ground[i][j].getMouseListeners();
- for(int x=0; x<listener.length; x++){
- this.ground[i][j].removeMouseListener(listener[x]);
- }
- if (sud[i][j] == 0) {
- this.ground[i][j].addMouseListener(new Mouse(i, j));
- this.ground[i][j].setFocusable(true);
- } else {
- this.ground[i][j].setFocusable(false);
- }
- }
- }
- }
- //This is the method repaintSudoku
- //It is for the ground backcolor
- public void repaintSudoku() {
- int[][] sud = this.sudoku.getSudoku();
- for (int i = 0; i < 9; i++) {
- for (int j = 0; j < 9; j++) {
- if (sud[i][j] != 0) {
- this.ground[i][j].setText(sud[i][j] + "");
- } else {
- this.ground[i][j].setText(" ");
- }
- }
- }
- }
- //This is the method initLabel
- //It is for the color of the line borders of the Sudoku game
- private void initLabel() {
- this.ground = new JLabel[9][9];
- int[][] sud = this.sudoku.getSudoku();
- for (int i = 0; i < 9; i++) {
- for (int j = 0; j < 9; j++) {
- String text = " ";
- if (sud[i][j] != 0) {
- text = sud[i][j] + "";
- }
- this.ground[i][j] = new JLabel(text);
- this.ground[i][j].setBorder(new LineBorder(Color.BLACK));
- this.ground[i][j].setHorizontalAlignment(JLabel.CENTER);
- this.add(this.ground[i][j]);
- }
- }
- this.initListeners();
- }
- @Override
- // the method overrides Graphics g into the paintComponent
- //It contents the color for the external grids and also the intensity of the color
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.LIGHT_GRAY);
- g.fillRect(100, 0, 100, 100);
- g.fillRect(0, 100, 100, 100);
- g.fillRect(199, 99, 100, 100);
- g.fillRect(99, 199, 100, 100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement