Advertisement
CaptainSpaceCat

Sudoku (Java Edition) - RunUI

Aug 4th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.85 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5.  
  6. public class RunUI extends JFrame implements ActionListener, MouseListener, KeyListener, ChangeListener {
  7.  
  8.   private int w, h;
  9.  
  10.   private Board board;
  11.   private Board solvedBoard;
  12.  
  13.   private Color permColor = Color.blue;
  14.   private Color backColor = new Color(238, 238, 238);
  15.  
  16.   private Font baseFont = new Font("Serif", Font.BOLD, 50);
  17.   private Font solveFont = new Font("Serif", Font.PLAIN, 50);
  18.  
  19.   private JLabel[][] boardLabels;
  20.   private JButton startButton, solveButton, resetButton, checkButton;
  21.   private JPanel panel;
  22.   private JFrame frame;
  23.  
  24.   private JSlider difficultySlider;
  25.   private JTextField difficultyBox;
  26.  
  27.   public RunUI() {
  28.     super("Sudoku");
  29.     initializeGUI();
  30.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31.     repaint();
  32.   }
  33.  
  34.   public void repaint(Board b) {
  35.     for (int y = 0; y < 9; y++) {
  36.       for (int x = 0; x < 9; x++) {
  37.         //System.out.println(perms[x][y]);
  38.         if (!b.getPerm(x, y)) {
  39.           boardLabels[x][y].setFont(baseFont);
  40.           boardLabels[x][y].setForeground(Color.black);
  41.           boardLabels[x][y].setBackground(backColor);
  42.         } else {
  43.           boardLabels[x][y].setFont(solveFont);
  44.           boardLabels[x][y].setForeground(permColor);
  45.           boardLabels[x][y].setBackground(Color.yellow);
  46.         }
  47.         if (b.getValue(x, y) > 0) {
  48.           boardLabels[x][y].setText("" + b.getValue(x, y));
  49.         } else {
  50.           boardLabels[x][y].setText(" ");
  51.         }
  52.       }
  53.     }
  54.    
  55.     pack();
  56.     panel.requestFocus();
  57.     panel.repaint();
  58.   }
  59.  
  60.   public void repaint() {
  61.     pack();
  62.     panel.requestFocus();
  63.     panel.repaint();
  64.   }
  65.  
  66.   private void setBoard(Board b) {
  67.     board = b;
  68.     board.solve();
  69.     solvedBoard = new Board(b);
  70.     board.reset();
  71.   }
  72.  
  73.   public void checkSolved() {
  74.     boolean complete = true;
  75.     for (int y = 0; y < 9; y++) {
  76.       for (int x = 0; x < 9; x++) {
  77.         if (board.getValue(x, y) == 0) {
  78.           complete = false;
  79.         }
  80.       }
  81.     }
  82.     if (complete) {
  83.       if (board.validate()) {
  84.         permColor = Color.green;
  85.       } else {
  86.         permColor = Color.red;
  87.       }
  88.     } else {
  89.       permColor = Color.blue;
  90.     }
  91.   }
  92.  
  93.   public void actionPerformed(ActionEvent e) {
  94.     if (e.getSource() == startButton) {
  95.       setBoard(new Board());
  96.       resetButton.setEnabled(true);
  97.       //if (difficultySlider.getValue() <= 3) {
  98.         solveButton.setEnabled(true);
  99.       //}
  100.       board.puzzle(difficultySlider.getValue());
  101.       repaint(board);
  102.     } else if (e.getSource() == solveButton) {
  103.       board = new Board(solvedBoard, board.getPerms());
  104.       checkSolved();
  105.       repaint(board);
  106.     } else if (e.getSource() == resetButton) {
  107.       board.reset();
  108.       repaint(board);
  109.     } else if (e.getSource() == difficultyBox) {
  110.       difficultySlider.setValue(Integer.parseInt(difficultyBox.getText()));
  111.     }
  112.   }
  113.  
  114.   public void stateChanged(ChangeEvent e) {
  115.     if (e.getSource() == difficultySlider) {
  116.       difficultyBox.setText("" + difficultySlider.getValue());
  117.       /*if (difficultySlider.getValue() > 3) {
  118.         solveButton.setEnabled(false);
  119.       } else if (board != null) {
  120.         solveButton.setEnabled(true);
  121.       }*/
  122.       repaint();
  123.     }
  124.   }
  125.  
  126.   public void mousePressed(MouseEvent e) {
  127.   }
  128.  
  129.   public void mouseReleased(MouseEvent e) {
  130.   }
  131.  
  132.   public void mouseEntered(MouseEvent e) {
  133.     JLabel source = (JLabel)e.getSource();
  134.     source.setOpaque(true);
  135.     //System.out.println("entered");
  136.     repaint();
  137.   }
  138.  
  139.   public void mouseExited(MouseEvent e) {
  140.     JLabel source = (JLabel)e.getSource();
  141.     //System.out.println("exited");
  142.     source.setOpaque(false);
  143.     repaint();
  144.   }
  145.  
  146.   public void mouseClicked(MouseEvent e) {
  147.   }
  148.  
  149.   public void keyTyped(KeyEvent e) {
  150.     //System.out.println("Key pressed!");
  151.     if (e.getID() == KeyEvent.KEY_TYPED && board != null) {
  152.       char c = e.getKeyChar();
  153.       int i = Character.getNumericValue(c);
  154.       //System.out.println(i);
  155.       if (c == '\b') {
  156.         for (int y = 0; y < 9; y++) {
  157.           for (int x = 0; x < 9; x++) {
  158.             if (boardLabels[x][y].isOpaque() && board.getPerm(x, y)) {
  159.               board.setValue(x, y, 0);
  160.               checkSolved();
  161.               repaint(board);
  162.             }
  163.           }
  164.         }
  165.       } else if (i >= 1 && i <= 9) {
  166.         for (int y = 0; y < 9; y++) {
  167.           for (int x = 0; x < 9; x++) {
  168.             if (boardLabels[x][y].isOpaque() && board.getPerm(x, y)) {
  169.               board.setValue(x, y, i);
  170.               checkSolved();
  171.               repaint(board);
  172.             }
  173.           }
  174.         }
  175.       }
  176.     }
  177.   }
  178.  
  179.   public void keyPressed(KeyEvent e) {
  180.   }
  181.  
  182.   public void keyReleased(KeyEvent e) {
  183.   }
  184.  
  185.  
  186.   public void initializeGUI() {
  187.     int offsetX = 62;
  188.     int offsetY = 62;
  189.    
  190.     panel = new JPanel() {
  191.       public void paintComponent(Graphics g) {
  192.         super.paintComponent(g);
  193.         Graphics2D g2 = (Graphics2D) g;
  194.         g2.setStroke(new BasicStroke(4));
  195.         int big = 192;
  196.         int small = 63;
  197.         for (int i = 0; i < 9; i++) {
  198.           g2.drawRect(offsetX-2+big*(i%3), offsetY-2+big*(i/3), big, big);
  199.         }
  200.         g2.setStroke(new BasicStroke(1));
  201.         for (int y = 0; y < 9; y++) {
  202.           for (int x = 0; x < 9; x++) {
  203.             g2.drawRect(offsetX-1+small*x+3*(x/3), offsetY-1+small*y+3*(y/3), small, small);
  204.           }
  205.         }
  206.       }
  207.     };
  208.    
  209.     panel.addKeyListener(this);
  210.    
  211.     this.setSize(new Dimension(700, 700));
  212.     panel.setLayout(null);
  213.     panel.setPreferredSize(new Dimension(700, 700));
  214.    
  215.     boardLabels = new JLabel[9][9];
  216.     Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  217.     if (w == 0 && h == 0) {
  218.       w = (int)screen.getWidth();
  219.       h = (int)screen.getHeight();
  220.     }
  221.     //System.out.println(w + ":" + h);
  222.    
  223.     startButton = new JButton("Generate");
  224.     startButton.addActionListener(this);
  225.     panel.add(startButton);
  226.     startButton.setBounds(10, 10, 100, 30);
  227.    
  228.     solveButton = new JButton("Solve");
  229.     solveButton.addActionListener(this);
  230.     panel.add(solveButton);
  231.     solveButton.setBounds(110, 10, 100, 30);
  232.     solveButton.setEnabled(false);
  233.    
  234.     resetButton = new JButton("Reset");
  235.     resetButton.addActionListener(this);
  236.     panel.add(resetButton);
  237.     resetButton.setBounds(210, 10, 100, 30);
  238.     resetButton.setEnabled(false);
  239.    
  240.     difficultySlider = new JSlider(JSlider.VERTICAL, 1, 5, 3);
  241.     panel.add(difficultySlider);
  242.     difficultySlider.addChangeListener(this);
  243.     difficultySlider.setBounds(10, 75, 30, 150);
  244.     difficultySlider.setMajorTickSpacing(2);
  245.     difficultySlider.setMinorTickSpacing(1);
  246.     difficultySlider.setPaintTicks(true);
  247.     //difficultySlider.setPaintLabels(true);
  248.    
  249.     difficultyBox = new JTextField("3");
  250.     panel.add(difficultyBox);
  251.     difficultyBox.addActionListener(this);
  252.     difficultyBox.setBounds(16, 50, 16, 20);
  253.    
  254.     for (int y = 0; y < 9; y++) {
  255.       for (int x = 0; x < 9; x++) {
  256.         boardLabels[x][y] = new JLabel("", SwingConstants.CENTER);
  257.         panel.add(boardLabels[x][y]);
  258.         //boardLabels[x][y].setFocusable(true);
  259.         boardLabels[x][y].addMouseListener(this);
  260.         //boardLabels[x][y].addKeyListener(this);
  261.         int cellSize = 62;
  262.         boardLabels[x][y].setBounds(offsetX+cellSize*x+3*(x/3)+x, offsetY+cellSize*y+3*(y/3)+y, cellSize, cellSize);
  263.       }
  264.     }
  265.     pack();
  266.     getContentPane().add(panel);
  267.     getRootPane().setDefaultButton(startButton);
  268.     panel.setVisible(true);
  269.    
  270.   }
  271.  
  272.   public void init() {
  273.     SwingUtilities.invokeLater(new Runnable() {
  274.       public void run() {
  275.         setVisible(true);
  276.       }
  277.     });
  278.   }
  279.  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement