Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- public class RunUI extends JFrame implements ActionListener, MouseListener, KeyListener, ChangeListener {
- private int w, h;
- private Board board;
- private Board solvedBoard;
- private Color permColor = Color.blue;
- private Color backColor = new Color(238, 238, 238);
- private Font baseFont = new Font("Serif", Font.BOLD, 50);
- private Font solveFont = new Font("Serif", Font.PLAIN, 50);
- private JLabel[][] boardLabels;
- private JButton startButton, solveButton, resetButton, checkButton;
- private JPanel panel;
- private JFrame frame;
- private JSlider difficultySlider;
- private JTextField difficultyBox;
- public RunUI() {
- super("Sudoku");
- initializeGUI();
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- repaint();
- }
- public void repaint(Board b) {
- for (int y = 0; y < 9; y++) {
- for (int x = 0; x < 9; x++) {
- //System.out.println(perms[x][y]);
- if (!b.getPerm(x, y)) {
- boardLabels[x][y].setFont(baseFont);
- boardLabels[x][y].setForeground(Color.black);
- boardLabels[x][y].setBackground(backColor);
- } else {
- boardLabels[x][y].setFont(solveFont);
- boardLabels[x][y].setForeground(permColor);
- boardLabels[x][y].setBackground(Color.yellow);
- }
- if (b.getValue(x, y) > 0) {
- boardLabels[x][y].setText("" + b.getValue(x, y));
- } else {
- boardLabels[x][y].setText(" ");
- }
- }
- }
- pack();
- panel.requestFocus();
- panel.repaint();
- }
- public void repaint() {
- pack();
- panel.requestFocus();
- panel.repaint();
- }
- private void setBoard(Board b) {
- board = b;
- board.solve();
- solvedBoard = new Board(b);
- board.reset();
- }
- public void checkSolved() {
- boolean complete = true;
- for (int y = 0; y < 9; y++) {
- for (int x = 0; x < 9; x++) {
- if (board.getValue(x, y) == 0) {
- complete = false;
- }
- }
- }
- if (complete) {
- if (board.validate()) {
- permColor = Color.green;
- } else {
- permColor = Color.red;
- }
- } else {
- permColor = Color.blue;
- }
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == startButton) {
- setBoard(new Board());
- resetButton.setEnabled(true);
- //if (difficultySlider.getValue() <= 3) {
- solveButton.setEnabled(true);
- //}
- board.puzzle(difficultySlider.getValue());
- repaint(board);
- } else if (e.getSource() == solveButton) {
- board = new Board(solvedBoard, board.getPerms());
- checkSolved();
- repaint(board);
- } else if (e.getSource() == resetButton) {
- board.reset();
- repaint(board);
- } else if (e.getSource() == difficultyBox) {
- difficultySlider.setValue(Integer.parseInt(difficultyBox.getText()));
- }
- }
- public void stateChanged(ChangeEvent e) {
- if (e.getSource() == difficultySlider) {
- difficultyBox.setText("" + difficultySlider.getValue());
- /*if (difficultySlider.getValue() > 3) {
- solveButton.setEnabled(false);
- } else if (board != null) {
- solveButton.setEnabled(true);
- }*/
- repaint();
- }
- }
- public void mousePressed(MouseEvent e) {
- }
- public void mouseReleased(MouseEvent e) {
- }
- public void mouseEntered(MouseEvent e) {
- JLabel source = (JLabel)e.getSource();
- source.setOpaque(true);
- //System.out.println("entered");
- repaint();
- }
- public void mouseExited(MouseEvent e) {
- JLabel source = (JLabel)e.getSource();
- //System.out.println("exited");
- source.setOpaque(false);
- repaint();
- }
- public void mouseClicked(MouseEvent e) {
- }
- public void keyTyped(KeyEvent e) {
- //System.out.println("Key pressed!");
- if (e.getID() == KeyEvent.KEY_TYPED && board != null) {
- char c = e.getKeyChar();
- int i = Character.getNumericValue(c);
- //System.out.println(i);
- if (c == '\b') {
- for (int y = 0; y < 9; y++) {
- for (int x = 0; x < 9; x++) {
- if (boardLabels[x][y].isOpaque() && board.getPerm(x, y)) {
- board.setValue(x, y, 0);
- checkSolved();
- repaint(board);
- }
- }
- }
- } else if (i >= 1 && i <= 9) {
- for (int y = 0; y < 9; y++) {
- for (int x = 0; x < 9; x++) {
- if (boardLabels[x][y].isOpaque() && board.getPerm(x, y)) {
- board.setValue(x, y, i);
- checkSolved();
- repaint(board);
- }
- }
- }
- }
- }
- }
- public void keyPressed(KeyEvent e) {
- }
- public void keyReleased(KeyEvent e) {
- }
- public void initializeGUI() {
- int offsetX = 62;
- int offsetY = 62;
- panel = new JPanel() {
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D) g;
- g2.setStroke(new BasicStroke(4));
- int big = 192;
- int small = 63;
- for (int i = 0; i < 9; i++) {
- g2.drawRect(offsetX-2+big*(i%3), offsetY-2+big*(i/3), big, big);
- }
- g2.setStroke(new BasicStroke(1));
- for (int y = 0; y < 9; y++) {
- for (int x = 0; x < 9; x++) {
- g2.drawRect(offsetX-1+small*x+3*(x/3), offsetY-1+small*y+3*(y/3), small, small);
- }
- }
- }
- };
- panel.addKeyListener(this);
- this.setSize(new Dimension(700, 700));
- panel.setLayout(null);
- panel.setPreferredSize(new Dimension(700, 700));
- boardLabels = new JLabel[9][9];
- Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
- if (w == 0 && h == 0) {
- w = (int)screen.getWidth();
- h = (int)screen.getHeight();
- }
- //System.out.println(w + ":" + h);
- startButton = new JButton("Generate");
- startButton.addActionListener(this);
- panel.add(startButton);
- startButton.setBounds(10, 10, 100, 30);
- solveButton = new JButton("Solve");
- solveButton.addActionListener(this);
- panel.add(solveButton);
- solveButton.setBounds(110, 10, 100, 30);
- solveButton.setEnabled(false);
- resetButton = new JButton("Reset");
- resetButton.addActionListener(this);
- panel.add(resetButton);
- resetButton.setBounds(210, 10, 100, 30);
- resetButton.setEnabled(false);
- difficultySlider = new JSlider(JSlider.VERTICAL, 1, 5, 3);
- panel.add(difficultySlider);
- difficultySlider.addChangeListener(this);
- difficultySlider.setBounds(10, 75, 30, 150);
- difficultySlider.setMajorTickSpacing(2);
- difficultySlider.setMinorTickSpacing(1);
- difficultySlider.setPaintTicks(true);
- //difficultySlider.setPaintLabels(true);
- difficultyBox = new JTextField("3");
- panel.add(difficultyBox);
- difficultyBox.addActionListener(this);
- difficultyBox.setBounds(16, 50, 16, 20);
- for (int y = 0; y < 9; y++) {
- for (int x = 0; x < 9; x++) {
- boardLabels[x][y] = new JLabel("", SwingConstants.CENTER);
- panel.add(boardLabels[x][y]);
- //boardLabels[x][y].setFocusable(true);
- boardLabels[x][y].addMouseListener(this);
- //boardLabels[x][y].addKeyListener(this);
- int cellSize = 62;
- boardLabels[x][y].setBounds(offsetX+cellSize*x+3*(x/3)+x, offsetY+cellSize*y+3*(y/3)+y, cellSize, cellSize);
- }
- }
- pack();
- getContentPane().add(panel);
- getRootPane().setDefaultButton(startButton);
- panel.setVisible(true);
- }
- public void init() {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- setVisible(true);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement