Advertisement
Vita_Harvey

SudokuController_B

Mar 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.51 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.PINK;
  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.                      // Buttons turns blue when "activated".
  99.                      if(button.getForeground == DEFAULT_COLOR) {                    
  100.                         button.setForeground(controller.ACTIVE_COLOR);                                            
  101.                      } else {
  102.                         button.setForeground(controller.DEFAULT_COLOR);
  103.                      }
  104.                      button.repaint();                      
  105.                   }
  106.                   @Override
  107.                   public void mouseReleased(MouseEvent released){
  108.                      button.setForeground(controller.ACTIVE_COLOR);
  109.                      // The following comments are for my own dev use.
  110.                      // get x, y position of chosenOne (the selected cell, yellow).
  111.                      // or get row, col position of chosenOne (getSelectedRow, etc)
  112.                      // button.drawSymbol(super.paint(Graphics g), x, y, i);
  113.                      button.repaint();
  114.                   }
  115.                   @Override
  116.                   public void mouseEntered(MouseEvent entered) {                    
  117.                      button.setBackground(controller.HIGHLIGHT_COLOR);
  118.                      button.repaint();
  119.                   }
  120.                   @Override
  121.                   public void mouseExited(MouseEvent exited) {
  122.                      button.setBackground(controller.DEFAULT_COLOR);
  123.                      button.repaint();
  124.                   }                            
  125.                   });            
  126.                panel.add(button);
  127.             }            
  128.             win.add(panel, BorderLayout.SOUTH);
  129.             win.pack();
  130.             win.setVisible(true);                            
  131.         }            
  132.          
  133.         catch (InputMismatchException iME) {
  134.             // One or more arguments isn't a valid integer.  
  135.             // Print an error message, then return.
  136.             System.err.println("The arguments must be integers.");
  137.             return;
  138.         }
  139.         catch (IllegalArgumentException iAE) {
  140.             // One or more arguments isn't a valid input value.                      
  141.             System.err.println("The integers must be non-negative.");
  142.             return;
  143.         }      
  144.    }  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement