Advertisement
Vita_Harvey

SudokuController_C

Mar 15th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.64 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.                JLabel label = new JLabel("" + i + "");
  91.                label.setForeground(Color.GREEN);
  92.                JPanel button = new JPanel();
  93.                button.add(label);
  94.                button.setBackground(controller.DEFAULT_COLOR);
  95.                button.setOpaque(true);
  96.                // each button gets a MouseListener
  97.                button.addMouseListener(new MouseAdapter()
  98.                {
  99.                   @Override
  100.                   public void mousePressed(MouseEvent pressed) {
  101.                      // This will set the value "i" of this button
  102.                      // into the currently selected Cell, unless
  103.                      // it is a "given".  Need code for this.
  104.                      
  105.                      // Buttons turns blue when "activated",
  106.                      // while button is held down.                  
  107.                      label.setForeground(Color.RED);
  108.                      label.repaint();                      
  109.                   }
  110.                   @Override
  111.                   public void mouseReleased(MouseEvent released){
  112.                      label.setForeground(Color.GREEN);
  113.                      // The following comments are for my own dev use.
  114.                      // get x, y position of chosenOne (the selected cell, yellow).
  115.                      // or get row, col position of chosenOne (getSelectedRow, etc)
  116.                      // button.drawSymbol(super.paint(Graphics g), x, y, i);
  117.                      label.repaint();
  118.                   }
  119.                   @Override
  120.                   public void mouseEntered(MouseEvent entered) {                    
  121.                      button.setBackground(controller.HIGHLIGHT_COLOR);
  122.                      button.repaint();
  123.                   }
  124.                   @Override
  125.                   public void mouseExited(MouseEvent exited) {
  126.                      button.setBackground(controller.DEFAULT_COLOR);
  127.                      button.repaint();
  128.                   }                            
  129.                   });            
  130.                panel.add(button);
  131.             }            
  132.             win.add(panel, BorderLayout.SOUTH);
  133.             win.pack();
  134.             win.setVisible(true);                            
  135.         }            
  136.          
  137.         catch (InputMismatchException iME) {
  138.             // One or more arguments isn't a valid integer.  
  139.             // Print an error message, then return.
  140.             System.err.println("The arguments must be integers.");
  141.             return;
  142.         }
  143.         catch (IllegalArgumentException iAE) {
  144.             // One or more arguments isn't a valid input value.                      
  145.             System.err.println("The integers must be non-negative.");
  146.             return;
  147.         }      
  148.    }  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement