Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.35 KB | None | 0 0
  1. //This is the package name
  2. package jsudoku.graphics;
  3.  
  4. //here i am importing external classes and attributes
  5. import java.awt.Color;
  6. import java.awt.Graphics;
  7. import java.awt.GridLayout;
  8. import java.awt.event.KeyAdapter;
  9. import java.awt.event.KeyEvent;
  10. import java.awt.event.KeyListener;
  11. import java.awt.event.MouseAdapter;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseListener;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16. import javax.swing.border.LineBorder;
  17. import jsudoku.Sudoku;
  18.  
  19. //Here I am inheriting from the class JPanel
  20. public class JSudoku extends JPanel {
  21.  
  22.     /**
  23.      * The class KeyAdapter is an abstract (adapter) class for receiving
  24.      * keyboard events. All methods of this class are empty.
  25.      * This class is convenience class for creating listener objects.
  26.      */
  27.     private class Key extends KeyAdapter {
  28.  
  29.         private int x;
  30.         private int y;
  31.  
  32.         public Key(int pX, int pY) {
  33.             this.x = pX;
  34.             this.y = pY;
  35.         }
  36.  
  37.         @Override
  38.         //The method keyPressed is created
  39.         public void keyPressed(KeyEvent e) {
  40.             char pressed = e.getKeyChar();
  41.             //The user can insert numbers 1 to 9 only in the game
  42.             if (pressed == '1' || pressed == '2' || pressed == '3' || pressed == '4' || pressed == '5' || pressed == '6' || pressed == '7' || pressed == '8' || pressed == '9') {
  43.                 //the number is added into the sudoku.addNumber and it is an integer
  44.                 sudoku.addNumber(this.x, this.y, Integer.parseInt(pressed + ""));
  45.                 ground[this.x][this.y].setBorder(new LineBorder(Color.BLACK));
  46.                 ground[this.x][this.y].removeKeyListener(this);
  47.                 repaintSudoku();
  48.             }else if(e.getKeyCode()==KeyEvent.VK_DELETE){
  49.                 sudoku.emptyNumber(this.x, this.y);
  50.                 ground[this.x][this.y].setBorder(new LineBorder(Color.BLACK));
  51.                 ground[this.x][this.y].removeKeyListener(this);
  52.                 repaintSudoku();
  53.             }
  54.         }
  55.     }
  56.  
  57.     //Here I am inheriting from the class MouseAdapter
  58.     private class Mouse extends MouseAdapter {
  59.  
  60.         private int x;
  61.         private int y;
  62.  
  63.         public Mouse(int pX, int pY) {
  64.             this.x = pX;
  65.             this.y = pY;
  66.         }
  67.  
  68.         // the method overrides MouseEvent 'e' into the method
  69.         //This methods helps the user understand if the number inserted by him is correct or not
  70.         //If he enter wrong number then the box will be red bordered
  71.         //Otherwise the selected box will be simply black
  72.         @Override
  73.         public void mouseClicked(MouseEvent e) {
  74.             for (int i = 0; i < 9; i++) {
  75.                 for (int j = 0; j < 9; j++) {
  76.                     ground[i][j].setBorder(new LineBorder(Color.BLACK));
  77.                     KeyListener[] remove = ground[i][j].getKeyListeners();
  78.                     if (remove.length != 0) {
  79.                         ground[i][j].removeKeyListener(remove[0]);
  80.                     }
  81.                 }
  82.             }
  83.             ground[this.x][this.y].requestFocus();
  84.             ground[this.x][this.y].setBorder(new LineBorder(Color.RED));
  85.             ground[this.x][this.y].addKeyListener(new Key(this.x, this.y));
  86.         }
  87.     }
  88.     private Sudoku sudoku;
  89.     private JLabel[][] ground;
  90.  
  91.     public JSudoku() {
  92.         this(new Sudoku());
  93.     }
  94.  
  95.     public Sudoku getSudoku() {
  96.         return this.sudoku;
  97.     }
  98.  
  99.     //This sets the dimention and the color of the grid (the background color included)
  100.     public JSudoku(Sudoku pSudoku) {
  101.         this.sudoku = pSudoku;
  102.         this.setSize(300, 300);
  103.         this.setBackground(Color.WHITE);
  104.         this.setLayout(new GridLayout(9, 9));
  105.         this.initLabel();
  106.     }
  107.  
  108.     //This is the method setSudoku
  109.     public void setSudoku(Sudoku pSudoku) {
  110.         this.sudoku = pSudoku;
  111.         this.repaintSudoku();
  112.         this.initListeners();
  113.     }
  114.  
  115.     //This is the method initListeners
  116.     public void initListeners() {
  117.  
  118.         int[][] sud = this.sudoku.getSudoku();
  119.         for (int i = 0; i < 9; i++) {
  120.             for (int j = 0; j < 9; j++) {
  121.                 MouseListener[] listener=this.ground[i][j].getMouseListeners();
  122.                 for(int x=0; x<listener.length; x++){
  123.                     this.ground[i][j].removeMouseListener(listener[x]);
  124.                 }
  125.                 if (sud[i][j] == 0) {
  126.                     this.ground[i][j].addMouseListener(new Mouse(i, j));
  127.                     this.ground[i][j].setFocusable(true);
  128.                 } else {
  129.                     this.ground[i][j].setFocusable(false);
  130.                 }
  131.             }
  132.         }
  133.     }
  134.  
  135.     //This is the method repaintSudoku
  136.     //It is for the ground backcolor
  137.     public void repaintSudoku() {
  138.         int[][] sud = this.sudoku.getSudoku();
  139.         for (int i = 0; i < 9; i++) {
  140.             for (int j = 0; j < 9; j++) {
  141.                 if (sud[i][j] != 0) {
  142.                     this.ground[i][j].setText(sud[i][j] + "");
  143.                 } else {
  144.                     this.ground[i][j].setText(" ");
  145.                 }
  146.             }
  147.         }
  148.     }
  149.  
  150.     //This is the method initLabel
  151.     //It is for the color of the line borders of the Sudoku game
  152.     private void initLabel() {
  153.         this.ground = new JLabel[9][9];
  154.         int[][] sud = this.sudoku.getSudoku();
  155.         for (int i = 0; i < 9; i++) {
  156.             for (int j = 0; j < 9; j++) {
  157.                 String text = " ";
  158.                 if (sud[i][j] != 0) {
  159.                     text = sud[i][j] + "";
  160.                 }
  161.                 this.ground[i][j] = new JLabel(text);
  162.                 this.ground[i][j].setBorder(new LineBorder(Color.BLACK));
  163.                 this.ground[i][j].setHorizontalAlignment(JLabel.CENTER);
  164.                 this.add(this.ground[i][j]);
  165.             }
  166.         }
  167.         this.initListeners();
  168.     }
  169.  
  170.     @Override
  171.     // the method overrides Graphics g into the paintComponent
  172.     //It contents the color for the external grids and also the intensity of the color
  173.     protected void paintComponent(Graphics g) {
  174.         super.paintComponent(g);
  175.         g.setColor(Color.LIGHT_GRAY);
  176.         g.fillRect(100, 0, 100, 100);
  177.         g.fillRect(0, 100, 100, 100);
  178.         g.fillRect(199, 99, 100, 100);
  179.         g.fillRect(99, 199, 100, 100);
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement