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.RED;
- ACTIVE_COLOR = Color.BLUE;
- }
- 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) {
- button.setOpaque(!button.isOpaque());
- //Cell selected = cell.setSelected(row, col);
- button.repaint();
- }
- @Override
- public void mouseEntered(MouseEvent entered) {
- //button.setOpaque(!button.isOpaque());
- button.setBackground(controller.HIGHLIGHT_COLOR);
- button.repaint();
- }
- @Override
- public void mouseExited(MouseEvent exited) {
- //button.setOpaque(!button.isOpaque());
- 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