Write a program using Grid layout that draw a Sudoku board with buttons on the s
ID: 3651705 • Letter: W
Question
Write a program using Grid layout that draw a Sudoku board with buttons on the side. The board itself is a 9 x 9 square(Use arrays here), divided into 9 3x3 (so 9 sections, each has a 3 x 3 part of the 9 x 9 array) squares. Note that each smaller square is set off by a heavier border than the regular intersquare borders. Each square is a text field. Write the program so that nothing is in a text field (You can read that as the "textfields" make up a 9 x 9 board). Users can type in the text field if they want, and if they do, numbers will show up. On the side there are four buttons that allow you to solve, get a new puzzle, get a hint, or reset puzzle (No action required just use jcombobox and buttons) . Here's a picture:
Explanation / Answer
Please rate...
program SudokuGUI.java
================================================
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.awt.*;
class SudokuGUI extends JFrame
{
private JTextField square[][][];
private JFrame frame;
private JPanel p1,p2,panel[];
JButton reset,hint,solve,puzzle;
JLabel difficulty,b1,b2;
JComboBox difficultyBox;
Border exteriorBorder = new LineBorder(Color.BLACK, 2);
Border dividerBorder = new LineBorder(Color.BLACK, 1);
SudokuGUI()
{
frame=new JFrame();
frame.setTitle("Sudoku");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int i,j,k;
square=new JTextField[9][3][3];
Font font = new Font("Verdana", Font.BOLD, 30);
for(i=0;i<9;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
square[i][j][k]=new JTextField("",1);
square[i][j][k].setFont(font);
square[i][j][k].setPreferredSize(new Dimension(30,30));
square[i][j][k].setHorizontalAlignment(JTextField.CENTER);
square[i][j][k].setBorder(dividerBorder);
}
}
}
p1=new JPanel();
panel=new JPanel[9];
p1.setLayout(new GridLayout(3,3));
for(i=0;i<9;i++)
{
panel[i]=new JPanel();
panel[i].setLayout(new GridLayout(3,3));
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
panel[i].add(square[i][j][k]);
}
}
panel[i].setBorder(exteriorBorder);
p1.add(panel[i]);
}
p1.setBorder(exteriorBorder);
class RoundedBorder implements Border
{
private int radius;
RoundedBorder(int radius) {
this.radius = radius;
}
public Insets getBorderInsets(Component c) {
return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
}
public boolean isBorderOpaque() {
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
g.drawRoundRect(x,y,width-1,height-1,radius,radius);
}
}
reset=new JButton("Reset");
reset.setSize(new Dimension(10, 40));
reset.setBorder(new RoundedBorder(10));
hint=new JButton("Hint");
hint.setSize(new Dimension(10, 40));
hint.setBorder(new RoundedBorder(10));
solve=new JButton("Solve");
solve.setSize(new Dimension(10, 40));
solve.setBorder(new RoundedBorder(10));
puzzle=new JButton("New Puzzle");
puzzle.setSize(new Dimension(10, 40));
puzzle.setBorder(new RoundedBorder(10));
difficulty=new JLabel("Difficulty: ");
difficulty.setHorizontalAlignment(JLabel.CENTER);
String[] difficulties = { "Easy", "Medium", "Hard" };
difficultyBox = new JComboBox(difficulties);
difficultyBox.setSelectedIndex(1);
p2=new JPanel();
p2.setLayout(new GridLayout(6,1));
p2.add(reset);
p2.add(hint);
p2.add(solve);
p2.add(puzzle);
p2.add(difficulty);
p2.add(difficultyBox);
b1=new JLabel(" ");
b2=new JLabel(" ");
frame.setLayout(new BorderLayout());
frame.add(b1,BorderLayout.NORTH);
frame.add(p1,BorderLayout.LINE_START);
frame.add(p2,BorderLayout.LINE_END);
frame.add(b2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[])
{
new SudokuGUI();
}
}
======================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.