Advertisement
Vita_Harvey

SudokuController_C

Mar 3rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. package csc143.sudoku;
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.awt.*;
  6. import javax.swing.*;
  7. import java.awt.event.*;
  8.  
  9. /**
  10.  * @author Vita Wiebe
  11.  * @version PA8: Sudoku Controller & Integration
  12.  */
  13. public class SudokuController extends JComponent {
  14.  
  15.    // Fields
  16.    
  17.    // These shall be the colors that the buttons turn when moused over or off.
  18.    public final Color HIGHLIGHT_COLOR;
  19.    public final Color DEFAULT_COLOR;
  20.    public final Color ACTIVE_COLOR;
  21.    
  22.    SudokuView view;
  23.    
  24.    public SudokuController() {
  25.       DEFAULT_COLOR = Color.GRAY;
  26.       HIGHLIGHT_COLOR = Color.RED;
  27.       ACTIVE_COLOR = Color.BLUE;
  28.    }
  29.    
  30.    public static SudokuBase makeBoard() {
  31.       SudokuBase board = new SudokuModel(2, 3);
  32.       board.setValue(0, 3, 6);
  33.       board.setValue(0, 5, 1);
  34.       board.setValue(1, 2, 4);
  35.       board.setValue(1, 4, 5);
  36.       board.setValue(1, 5, 3);
  37.       board.setValue(2, 3, 3);
  38.       board.setValue(3, 2, 6);
  39.       board.setValue(4, 0, 2);
  40.       board.setValue(4, 1, 3);
  41.       board.setValue(4, 3, 1);
  42.       board.setValue(5, 0, 6);
  43.       board.setValue(5, 2, 1);
  44.       board.fixGivens();
  45.       return board;
  46.       }
  47.    
  48.    /**
  49.     * The main application method and entry to the entire Sudoku game.
  50.     * @param String[] args, the command-line arguments specifying the
  51.     * board dimensions.
  52.     */
  53.    public static void main(String[] args) {
  54.        
  55.         // Instantiate (default) class constructor, our controller:
  56.         SudokuController controller = new SudokuController();
  57.                
  58.         // Rows and columns are the desired number of rows/columns for a board.
  59.         int rows = 0;
  60.         int columns = 0;
  61.        
  62.         // Take in the desired dimensions of game from user via command-line.
  63.         Scanner scanner = new Scanner(System.in);
  64.         System.out.println("Please enter the dimensions of the desired board.");
  65.         System.out.println("Number of rows first, followed by the number of columns.");
  66.        
  67.         try {
  68.             // Temp variable to hold each arg as read in.
  69.             int temp;          
  70.             // Parse the string argument into an integer value.
  71.             temp = scanner.nextInt();
  72.            
  73.             if(temp > 0) {
  74.                rows = temp;
  75.                temp = scanner.nextInt();
  76.             }
  77.             if(temp > 0) {    
  78.                columns = temp;
  79.             }
  80.                        
  81.             SudokuBase sBase = new SudokuModel(rows, columns);
  82.             JFrame win = new JFrame("Sudoku! " + rows + "X" + columns);
  83.             win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  84.             JPanel panel = new JPanel();          
  85.            
  86.             controller.view = new SudokuView(sBase);            
  87.             win.add(controller.view);
  88.            
  89.             for(int i = 0; i < sBase.getSize(); i++) {
  90.                JButton button = new JButton("" + i + "");
  91.                button.setBackground(controller.DEFAULT_COLOR);
  92.                button.setOpaque(true);
  93.                // each button gets a MouseListener
  94.                button.addMouseListener(new MouseAdapter()
  95.                {
  96.                   @Override
  97.                   public void mouseClicked(MouseEvent clicked) {
  98.                      button.setOpaque(!button.isOpaque());
  99.                      //Cell selected = cell.setSelected(row, col);
  100.                      button.repaint();
  101.                   }
  102.                   @Override
  103.                   public void mouseEntered(MouseEvent entered) {                    
  104.                      //button.setOpaque(!button.isOpaque());
  105.                      button.setBackground(controller.HIGHLIGHT_COLOR);
  106.                      button.repaint();
  107.                   }
  108.                   @Override
  109.                   public void mouseExited(MouseEvent exited) {
  110.                      //button.setOpaque(!button.isOpaque());
  111.                      button.setBackground(controller.DEFAULT_COLOR);
  112.                      button.repaint();
  113.                   }                            
  114.                   });            
  115.                panel.add(button);
  116.             }            
  117.             win.add(panel, BorderLayout.SOUTH);
  118.             win.pack();
  119.             win.setVisible(true);                            
  120.         }            
  121.          
  122.         catch (InputMismatchException iME) {
  123.             // One or more arguments isn't a valid integer.  
  124.             // Print an error message, then return.
  125.             System.err.println("The arguments must be integers.");
  126.             return;
  127.         }
  128.         catch (IllegalArgumentException iAE) {
  129.             // One or more arguments isn't a valid input value.                      
  130.             System.err.println("The integers must be non-negative.");
  131.             return;
  132.         }
  133.        
  134.        
  135.    }  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement