Question: JPanel panel1 = new JPanel(); panel1.setLayout(new GridLayout(9,9)); for(int i=0;i <9;i++) { gridButton[i] = new JButton[9]; for(int j=0;j <9;j++) { gridButton[i][j] = new JButton(); //gridButton[i][j].setSize(51,42); gridButton[i][j].setFont(new
JPanel panel1 = new JPanel(); panel1.setLayout(new GridLayout(9,9));
for(int i=0;i<9;i++) { gridButton[i] = new JButton[9]; for(int j=0;j<9;j++) { gridButton[i][j] = new JButton(); //gridButton[i][j].setSize(51,42); gridButton[i][j].setFont(new Font("Courier New",Font.BOLD,16)); gridButton[i][j].setForeground(Color.blue); gridButton[i][j].setText(""); gridButton[i][j].setFocusable(false); gridButton[i][j].addActionListener(this); gridButton[i][j].setBackground(GAINSBORO); switch(i) { case 0: case 1: case 2: case 6: case 7: case 8:if(j<3 || j>5) gridButton[i][j].setBackground(BEIGE); break; case 3: case 4: case 5:if(j>2 && j<6) gridButton[i][j].setBackground(BEIGE); } // add buttons to the panel panel1.add(gridButton[i][j]); } } // create another panel with a grid layout for the numbers JPanel panel2 = new JPanel(); panel2.setLayout(new GridLayout(1,11)); for(int i=0;i<11;i++) { if(i < 9) { numberButton[i] = new JButton(""+(i+1)); numberButton[i].setBackground(Color.blue); } else { numberButton[i] = new JButton(new ImageIcon("images/"+i+".jpg")); numberButton[i].setBackground(Color.white); } numberButton[i].addActionListener(this); numberButton[i].setForeground(Color.white); numberButton[i].setFocusable(false); panel2.add(numberButton[i]); } // set default number to 1 by giving that button the focus numberButton[0].requestFocus(); // set the frame's layout to a Border Layout and add the panels setLayout(new BorderLayout()); add(panel1,BorderLayout.CENTER); add(panel2,BorderLayout.SOUTH); // Add a window listener to the frame this.addWindowListener ( new WindowAdapter() { public void windowClosing(WindowEvent e) { Sudoku.this.windowClosed(); } } ); } /***********************************************************************/ public void resetGrid() { for(int i=0;i<9;i++) for(int j=0;j<9;j++) { gridButton[i][j].removeActionListener(this); gridButton[i][j].setForeground(Color.red); gridButton[i][j].setText(""); gridButton[i][j].setFocusable(false); gridButton[i][j].addActionListener(this); } } /************************************************************************ * Fill the grid with data read from a file * You will have to complete the code for this method */ public void fillGrid() { Scanner scan; URL url; try { url = new URL("http://venus.cs.qc.cuny.edu/~aabreu/cs212/project1/Sudoku1.txt"); scan = new Scanner(url.openStream());
// write the code to fill the grid JOptionPane.showMessageDialog(this, "Write the necessary code to fill the grid.");
} catch (MalformedURLException mue) { mue.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } /***********************************************************************/ public void actionPerformed(ActionEvent e) { boolean commandButtonClicked = false; // check to see if user pressed 1 of the buttons - 1 thru 9 for(int i=0; i < 11 && !commandButtonClicked; i++) { if(((JButton)e.getSource()) == numberButton[i]) { commandButtonClicked = true; // if a number was pressed, convert the text to a number // and store it in the variable 'number' if(i < 9) { number = Integer.parseInt(((JButton)e.getSource()).getText()); } else if(i == 9) { if(evaluate()) JOptionPane.showMessageDialog(this, "Sudoku"); else JOptionPane.showMessageDialog(this,"Not Sudoku"); } else if(i == 10) JOptionPane.showMessageDialog(this, "CS212 - Program #1 Written by"); } } // check to see if user pressed 1 of the buttons in the grid // otherwise erase it if(!commandButtonClicked ) { JButton temp = (JButton)e.getSource(); // replace the text of the grid button with the number clicked if(!String.valueOf(number).equals(temp.getText())) { temp.setText("" + number); } else temp.setText(""); } } /*********************************************************************** * Check to see if the grid represents a Sudoku * You will have to write the code for this method ***********************************************************************/ public boolean evaluate() { JOptionPane.showMessageDialog(this, "Write the necessary code to see if this is a Sudoku."); return false; } /*********************************************************************** * Close window and exit application ***********************************************************************/ protected void windowClosed() { System.exit(0); } /*********************************************************************** main menu and start of program ***********************************************************************/ public static void main(String[] args) { Sudoku frame = new Sudoku(); // Create application frame. frame.setTitle("Sudoku v0.99"); // add title to the frame frame.setSize(new Dimension(470, 480)); // set the size of the frame frame.setResizable(false); // frame's size is fixed frame.setVisible(true); // Show frame } }
first part question is at :
https://www.chegg.com/homework-help/questions-and-answers/code-import-javaawt-import-javaawtevent-import-javaioioexception-import-javanetmalformedur-q35473284
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
