Advertisement
Vita_Harvey

SudokuController_A

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