Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csc143.sudoku;
- import java.util.*;
- import java.lang.*;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- /**
- * @author Vita Wiebe
- * @version PA8: Sudoku Controller & Integration
- */
- public class SudokuController extends JComponent {
- // Fields
- // These shall be the colors that the buttons turn when moused over or off.
- public final Color HIGHLIGHT_COLOR;
- public final Color DEFAULT_COLOR;
- public final Color ACTIVE_COLOR;
- SudokuView view;
- public SudokuController() {
- DEFAULT_COLOR = Color.GRAY;
- HIGHLIGHT_COLOR = Color.PINK;
- ACTIVE_COLOR = Color.BLUE;
- }
- public static SudokuBase makeBoard() {
- SudokuBase board = new SudokuModel(2, 3);
- board.setValue(0, 3, 6);
- board.setValue(0, 5, 1);
- board.setValue(1, 2, 4);
- board.setValue(1, 4, 5);
- board.setValue(1, 5, 3);
- board.setValue(2, 3, 3);
- board.setValue(3, 2, 6);
- board.setValue(4, 0, 2);
- board.setValue(4, 1, 3);
- board.setValue(4, 3, 1);
- board.setValue(5, 0, 6);
- board.setValue(5, 2, 1);
- board.fixGivens();
- return board;
- }
- /**
- * The main application method and entry to the entire Sudoku game.
- * @param String[] args, the command-line arguments specifying the
- * board dimensions.
- */
- public static void main(String[] args) {
- // Instantiate (default) class constructor, our controller:
- SudokuController controller = new SudokuController();
- // Rows and columns are the desired number of rows/columns for a board.
- int rows = 0;
- int columns = 0;
- // Take in the desired dimensions of game from user via command-line.
- Scanner scanner = new Scanner(System.in);
- System.out.println("Please enter the dimensions of the desired board.");
- System.out.println("Number of rows first, followed by the number of columns.");
- try {
- // Temp variable to hold each arg as read in.
- int temp;
- // Parse the string argument into an integer value.
- temp = scanner.nextInt();
- if(temp > 0) {
- rows = temp;
- temp = scanner.nextInt();
- }
- if(temp > 0) {
- columns = temp;
- }
- SudokuBase sBase = new SudokuModel(rows, columns);
- JFrame win = new JFrame("Sudoku! " + rows + "X" + columns);
- win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- JPanel panel = new JPanel();
- controller.view = new SudokuView(sBase);
- win.add(controller.view);
- for(int i = 0; i < sBase.getSize(); i++) {
- JButton button = new JButton("" + i + "");
- button.setBackground(controller.DEFAULT_COLOR);
- button.setOpaque(true);
- // each button gets a MouseListener
- button.addMouseListener(new MouseAdapter()
- {
- @Override
- public void mouseClicked(MouseEvent clicked) {
- // Buttons turns blue when "activated".
- if(button.getForeground == DEFAULT_COLOR) {
- button.setForeground(controller.ACTIVE_COLOR);
- } else {
- button.setForeground(controller.DEFAULT_COLOR);
- }
- button.repaint();
- }
- @Override
- public void mouseReleased(MouseEvent released){
- button.setForeground(controller.ACTIVE_COLOR);
- // The following comments are for my own dev use.
- // get x, y position of chosenOne (the selected cell, yellow).
- // or get row, col position of chosenOne (getSelectedRow, etc)
- // button.drawSymbol(super.paint(Graphics g), x, y, i);
- button.repaint();
- }
- @Override
- public void mouseEntered(MouseEvent entered) {
- button.setBackground(controller.HIGHLIGHT_COLOR);
- button.repaint();
- }
- @Override
- public void mouseExited(MouseEvent exited) {
- button.setBackground(controller.DEFAULT_COLOR);
- button.repaint();
- }
- });
- panel.add(button);
- }
- win.add(panel, BorderLayout.SOUTH);
- win.pack();
- win.setVisible(true);
- }
- catch (InputMismatchException iME) {
- // One or more arguments isn't a valid integer.
- // Print an error message, then return.
- System.err.println("The arguments must be integers.");
- return;
- }
- catch (IllegalArgumentException iAE) {
- // One or more arguments isn't a valid input value.
- System.err.println("The integers must be non-negative.");
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement