Advertisement
Vita_Harvey

SudokuController_B

Mar 3rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 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 void main(String[] args) {
  31.        
  32.         // Instantiate (default) class constructor, our controller:
  33.         SudokuController controller = new SudokuController();
  34.                
  35.         // Rows and columns are the desired number of rows/columns for a board.
  36.         int rows = 0;
  37.         int columns = 0;
  38.        
  39.         // Take in the desired dimensions of game from user via command-line.
  40.         Scanner scanner = new Scanner(System.in);
  41.         System.out.println("Please enter the dimensions of the desired board.");
  42.         System.out.println("Number of rows first, followed by the number of columns.");
  43.        
  44.         try {
  45.             // Temp variable to hold each arg as read in.
  46.             int temp;          
  47.             // Parse the string argument into an integer value.
  48.             temp = scanner.nextInt();
  49.            
  50.             if(temp > 0) {
  51.                rows = temp;
  52.                temp = scanner.nextInt();
  53.             }if(temp > 0) {    
  54.                columns = temp;
  55.             }
  56.                        
  57.             SudokuBase sBase = new SudokuModel(rows, columns);
  58.             JFrame win = new JFrame("Sudoku! " + rows + "X" + columns);
  59.             win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  60.             JPanel panel = new JPanel();          
  61.            
  62.             controller.view = new SudokuView(sBase);            
  63.             win.add(controller.view);
  64.            
  65.             for(int i = 0; i < sBase.getSize(); i++) {
  66.                JButton button = new JButton("" + i + "");
  67.                button.setBackground(controller.DEFAULT_COLOR);
  68.                button.setOpaque(true);
  69.                // each button gets a MouseListener
  70.                button.addMouseListener(new MouseAdapter()
  71.                {
  72.                   @Override
  73.                   public void mouseClicked(MouseEvent clicked) {
  74.                      button.setOpaque(!button.isOpaque());
  75.                      //Cell selected = cell.setSelected(row, col);
  76.                      button.repaint();
  77.                   }
  78.                   @Override
  79.                   public void mouseEntered(MouseEvent entered) {                    
  80.                      //button.setOpaque(!button.isOpaque());
  81.                      button.setBackground(controller.HIGHLIGHT_COLOR);
  82.                      button.repaint();
  83.                   }
  84.                   @Override
  85.                   public void mouseExited(MouseEvent exited) {
  86.                      //button.setOpaque(!button.isOpaque());
  87.                      button.setBackground(controller.DEFAULT_COLOR);
  88.                      button.repaint();
  89.                   }                            
  90.                   });            
  91.                panel.add(button);
  92.             }            
  93.             win.add(panel, BorderLayout.SOUTH);
  94.             win.pack();
  95.             win.setVisible(true);                            
  96.         }            
  97.          
  98.         catch (InputMismatchException iME) {
  99.             // One or more arguments isn't a valid integer.  
  100.             // Print an error message, then return.
  101.             System.err.println("The arguments must be integers.");
  102.             return;
  103.         }
  104.         catch (IllegalArgumentException iAE) {
  105.             // One or more arguments isn't a valid input value.                      
  106.             System.err.println("The integers must be non-negative.");
  107.             return;
  108.         }
  109.        
  110.        
  111.    }  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement