22
33import javax .swing .*;
44import java .awt .*;
5- import java .awt .event .ActionEvent ;
6- import java .awt .event .ActionListener ;
7- import java .io .IOException ;
85
9- class guiController extends JFrame {
6+ class guiController extends JFrame implements Runnable {
107/*
118This class handle GUI related functions and also allows GUI refresh whenever any move is taken by AI
129 */
1310 private JPanel grid = new JPanel (new GridLayout (9 , 9 )); //Grid layout
14- private JPanel buttons = new JPanel (new GridLayout (2 , 1 ));
11+ private JPanel buttons = new JPanel (new GridLayout (1 , 2 ));
1512 private JTextField [][] fieldArray = new JTextField [9 ][9 ]; //array of fields for easy extraction
1613
17- private sudokuInitializer sud = new sudokuInitializer (); //initializes array from file
14+ sudokuInitializer sud = new sudokuInitializer (); //initializes array from file
1815 private int flag ;
1916
20- guiController () {
17+ public void run () {
2118 flag = chooseInputStream (); //choose input method
2219 guiInit ();//Initializes GUI
2320 //sud.printArray();
21+ setVisible (true );
2422 }
2523/*
2624Function to display GUI for the user to choose input method method
@@ -31,7 +29,6 @@ private int chooseInputStream() {
3129 int response = JOptionPane .showOptionDialog (null , "Choose one way" , "" ,
3230 JOptionPane .DEFAULT_OPTION , JOptionPane .PLAIN_MESSAGE ,
3331 null , options , options [0 ]);
34- System .out .println (response );
3532 if (response == 0 ) {
3633 try {
3734 sud .fileInput ();
@@ -73,29 +70,59 @@ private void guiInit() {
7370 }
7471 add (grid );
7572 JButton solve = new JButton ("Solve" );//Button to compute
73+ JButton Clear = new JButton ("Clear" );//Button to compute
7674 solve .setBorder (null );
7775 solve .setBackground (Color .white );
7876 buttons .add (solve );
79- buttons .add (new JLabel ());
8077 buttons .setBackground (Color .white );
8178 add (buttons , BorderLayout .SOUTH );
8279 solve .addActionListener (e -> { //If the button is pressed
8380 if (flag != 0 ) {
8481 try {
8582 getText (); //takes in text from the GUI
86- sud .printArray (); //debugging purpose
87- solve .setEnabled (false );//cell value becomes fixed
8883 } catch (Exception ex ) {
8984 JOptionPane .showMessageDialog (null ,"Invalid Input" );//Throws error if the input is invalid
85+ clearGUI ();
86+ flag =-1 ;
9087 }
9188 }
89+ if (flag !=-1 ) {
90+ sud .printArray (); //debugging purpose
91+ solve .setEnabled (false );//cell value becomes fixed
92+ solver solver = new solver ();
93+ Thread t2 = new Thread (solver );
94+ t2 .start ();
95+ System .out .println ("Solving..." );
96+ Clear .setEnabled (false );
97+ }
98+ });
99+
100+
101+ Clear .setBorder (null );
102+ Clear .setBackground (Color .white );
103+ buttons .add (Clear );
104+ Clear .addActionListener (e -> { //If the button is pressed
105+ clearGUI ();
106+
92107 });
108+
109+ buttons .setPreferredSize (new Dimension (400 ,50 ));
93110 //Basic Frame parameters are set below this
94111 setSize (400 , 500 );
95112 //setVisible(true);//done in Main function
96113 setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
97114 setLocationRelativeTo (null );
98115 }
116+ private void clearGUI (){
117+ for (int i = 0 ; i < 9 ; i ++)
118+ for (int j = 0 ; j < 9 ; j ++) {
119+ Main .sudoku_array [i ][j ] = 0 ;
120+ fieldArray [i ][j ].setEditable (true );
121+ fieldArray [i ][j ].setText ("" );
122+ }
123+ Main .points .clear ();
124+ refreshGUI ();
125+ }
99126/*
100127takes in valid input and sets the GUI cells as non editable
101128 */
@@ -105,23 +132,24 @@ private void getText() throws Exception{
105132 for (int j = 0 ; j < 9 ; j ++) {
106133 temp = fieldArray [i ][j ].getText ();
107134 if (!temp .equals ("" )) {
108- Main .sudoku_array [i ][j ] = sud .checkValidInput (temp ); //throws exception
109- fieldArray [i ][j ].setEditable (false );
110- }
135+ if (sud .checkValidInput (temp +"" ))
136+ Main .sudoku_array [i ][j ] = Integer .parseInt (temp + "" );//checks valid input, catches exception
137+ fieldArray [i ][j ].setEditable (false );
138+ }else
139+ Main .points .add (new Point (i , j ));
111140 }
112141 }
142+ sud .printPoints ();
113143 }
114144/*
115145Refreshes the GUI whenever AI makes some changes
116146 */
117- void refreshGUI (){
118- for (int i = 0 ; i < 9 ; i ++) {
119- for (int j = 0 ; j < 9 ; j ++) {
120- if (Main .sudoku_array [i ][j ] != 0 ) {
121- fieldArray [i ][j ].setText (Main .sudoku_array [i ][j ]+"" );
122- }
123- }
124- }
147+ protected void refreshGUI (){
148+ for (int i =0 ; i <Main .points .size ();i ++)
149+ if (Main .sudoku_array [(int )Main .points .get (i ).getX ()][(int )Main .points .get (i ).getY ()]!=0 )
150+ fieldArray [(int )Main .points .get (i ).getX ()][(int )Main .points .get (i ).getY ()].setText (Main .sudoku_array [(int )Main .points .get (i ).getX ()][(int )Main .points .get (i ).getY ()]+"" );
151+ else
152+ fieldArray [(int )Main .points .get (i ).getX ()][(int )Main .points .get (i ).getY ()].setText ("" );
125153 }
126154
127155}
0 commit comments