Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JSudoku - JSudoku.java

JSudoku.java

Caricato da:
Scarica il programma completo

  1. package jsudoku.graphics;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.GridLayout;
  6. import java.awt.event.KeyAdapter;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import java.awt.event.MouseAdapter;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseListener;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.border.LineBorder;
  15. import jsudoku.Sudoku;
  16.  
  17. public class JSudoku extends JPanel {
  18.  
  19.     private class Key extends KeyAdapter {
  20.  
  21.         private int x;
  22.         private int y;
  23.  
  24.         public Key(int pX, int pY) {
  25.             this.x = pX;
  26.             this.y = pY;
  27.         }
  28.  
  29.         @Override
  30.         public void keyPressed(KeyEvent e) {
  31.             char pressed = e.getKeyChar();
  32.             if (pressed == '1' || pressed == '2' || pressed == '3' || pressed == '4' || pressed == '5' || pressed == '6' || pressed == '7' || pressed == '8' || pressed == '9') {
  33.                 sudoku.addNumber(this.x, this.y, Integer.parseInt(pressed + ""));
  34.                 ground[this.x][this.y].setBorder(new LineBorder(Color.BLACK));
  35.                 ground[this.x][this.y].removeKeyListener(this);
  36.                 repaintSudoku();
  37.             }else if(e.getKeyCode()==KeyEvent.VK_DELETE){
  38.                 sudoku.emptyNumber(this.x, this.y);
  39.                 ground[this.x][this.y].setBorder(new LineBorder(Color.BLACK));
  40.                 ground[this.x][this.y].removeKeyListener(this);
  41.                 repaintSudoku();
  42.             }
  43.         }
  44.     }
  45.  
  46.     private class Mouse extends MouseAdapter {
  47.  
  48.         private int x;
  49.         private int y;
  50.  
  51.         public Mouse(int pX, int pY) {
  52.             this.x = pX;
  53.             this.y = pY;
  54.         }
  55.  
  56.         @Override
  57.         public void mouseClicked(MouseEvent e) {
  58.             for (int i = 0; i < 9; i++) {
  59.                 for (int j = 0; j < 9; j++) {
  60.                     ground[i][j].setBorder(new LineBorder(Color.BLACK));
  61.                     KeyListener[] remove = ground[i][j].getKeyListeners();
  62.                     if (remove.length != 0) {
  63.                         ground[i][j].removeKeyListener(remove[0]);
  64.                     }
  65.                 }
  66.             }
  67.             ground[this.x][this.y].requestFocus();
  68.             ground[this.x][this.y].setBorder(new LineBorder(Color.RED));
  69.             ground[this.x][this.y].addKeyListener(new Key(this.x, this.y));
  70.         }
  71.     }
  72.     private Sudoku sudoku;
  73.     private JLabel[][] ground;
  74.  
  75.     public JSudoku() {
  76.         this(new Sudoku());
  77.     }
  78.  
  79.     public Sudoku getSudoku() {
  80.         return this.sudoku;
  81.     }
  82.  
  83.     public JSudoku(Sudoku pSudoku) {
  84.         this.sudoku = pSudoku;
  85.         this.setSize(300, 300);
  86.         this.setBackground(Color.WHITE);
  87.         this.setLayout(new GridLayout(9, 9));
  88.         this.initLabel();
  89.     }
  90.  
  91.     public void setSudoku(Sudoku pSudoku) {
  92.         this.sudoku = pSudoku;
  93.         this.repaintSudoku();
  94.         this.initListeners();
  95.     }
  96.  
  97.     public void initListeners() {
  98.  
  99.         int[][] sud = this.sudoku.getSudoku();
  100.         for (int i = 0; i < 9; i++) {
  101.             for (int j = 0; j < 9; j++) {
  102.                 MouseListener[] listener=this.ground[i][j].getMouseListeners();
  103.                 for(int x=0; x<listener.length; x++){
  104.                     this.ground[i][j].removeMouseListener(listener[x]);
  105.                 }
  106.                 if (sud[i][j] == 0) {
  107.                     this.ground[i][j].addMouseListener(new Mouse(i, j));
  108.                     this.ground[i][j].setFocusable(true);
  109.                 } else {
  110.                     this.ground[i][j].setFocusable(false);
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.     public void repaintSudoku() {
  117.         int[][] sud = this.sudoku.getSudoku();
  118.         for (int i = 0; i < 9; i++) {
  119.             for (int j = 0; j < 9; j++) {
  120.                 if (sud[i][j] != 0) {
  121.                     this.ground[i][j].setText(sud[i][j] + "");
  122.                 } else {
  123.                     this.ground[i][j].setText(" ");
  124.                 }
  125.             }
  126.         }
  127.     }
  128.  
  129.     private void initLabel() {
  130.         this.ground = new JLabel[9][9];
  131.         int[][] sud = this.sudoku.getSudoku();
  132.         for (int i = 0; i < 9; i++) {
  133.             for (int j = 0; j < 9; j++) {
  134.                 String text = " ";
  135.                 if (sud[i][j] != 0) {
  136.                     text = sud[i][j] + "";
  137.                 }
  138.                 this.ground[i][j] = new JLabel(text);
  139.                 this.ground[i][j].setBorder(new LineBorder(Color.BLACK));
  140.                 this.ground[i][j].setHorizontalAlignment(JLabel.CENTER);
  141.                 this.add(this.ground[i][j]);
  142.             }
  143.         }
  144.         this.initListeners();
  145.     }
  146.  
  147.     @Override
  148.     protected void paintComponent(Graphics g) {
  149.         super.paintComponent(g);
  150.         g.setColor(Color.LIGHT_GRAY);
  151.         g.fillRect(100, 0, 100, 100);
  152.         g.fillRect(0, 100, 100, 100);
  153.         g.fillRect(199, 99, 100, 100);
  154.         g.fillRect(99, 199, 100, 100);
  155.     }
  156. }