-3

I am required to create a Suduku Game Board that looks like this:

enter image description here

Here are the requirements I need for this assignment, but am having some issues.

  1. Use two for loops to draw the text fields instead of brute-force of listing 81 text fields. You should do something like:

        for (int k = 1; k <= 9; k++)
        {
            JPanel level2 = new JPanel();
     ….
            for (int i = 1; i <= 9; i++)
            {
                JTextField text = new JTextField();
                …
            }
            gridPanel.add(level2);
        }
    
  2. I need 2 classes in an application class named TestSudoku and a work class named SudokuLayout.

  3. Implement the following visual gadgets and write listeners for them. These gadgets have the following behaviors:

    • Button “Reset”---when the button is clicked, the program will clear the text area, then output the string “Reset button clicked!” to the text area.
    • Button “Hint”---when the button is clicked, the program will clear the text area, then output the string “Hint button clicked!” to the text area.
    • Combobox “Difficulty”---when an item is selected, the program will clear the text area, then output the selected item name to the text area.
  4. implement the listeners using loosely coupled methods (private listener class or private adapter class).

This is what I currently have..

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

public class SudokuLayout extends JFrame {

    public SudokuLayout() {

        JPanel board = new JPanel(new GridLayout(9, 9));
        add(board);

        JPanel[][] squares = new JPanel[9][9];

        Border border = BorderFactory.createLineBorder(Color.BLACK);

        for (int row = 1; row < 9; row++) {
            for (int col = 1; col < 9; col++) {
                squares[row][col] = new JPanel();
                board.add(squares[row][col]);
            }
        }

        JPanel menu = new JPanel();
        menu.add(new JButton("Reset"));
        menu.add(new JButton("Hint"));
        menu.add(new JButton("Solve"));
        menu.add(new JButton("New Puzzle"));

        add(menu);
    }

    public static void main(String[] args) {
        /** Create a frame and set its properties*/
        JFrame frame = new SudokuLayout();
        frame.setTitle("Sudoku");
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null); //Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

The problem is that, with my current version, the right menu shows horizontally and I cannot see the grid. Moreover, I don't know how to add the output area.

Cristian Ramon-Cortes
  • 1,838
  • 1
  • 19
  • 32

1 Answers1

2

When facing this kind of frames with Swing you need to divide the frame in sections and treat them separately. I mean, by looking at the image you can easily identify the Sudoku, the menu, and the output. Thus, your answer should try to first create each of them separately and then join them.

Considering this, you must notice:

  • There are 3 main components: sudoku, menu, and output
  • The 3 main components may use a BorderLayout since they are probably at WEST, EAST and SOUTH
  • The sudoku component is a 3x3 grid of 3x3 smaller grids (so you can change the black-border).
  • The menu component is a 5x1 grid with buttons in each position
  • The output component is a single text frame

So, you may need to change something to get the exact behaviour (button sizes and margins, options on the difficulty ComboBox) but your solution should look like this:

    public class SudokuLayout extends JFrame {

    public SudokuLayout() {
        // Create panel for Sudoku
        JPanel board = new JPanel();
        board.setLayout(new GridLayout(3, 3));
        board.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        for (int row = 0; row < 3; ++row) {
            for (int col = 0; col < 3; ++col) {
                JPanel box = new JPanel(new GridLayout(3, 3));
                box.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                for (int cell = 0; cell < 9; ++cell) {
                    box.add(new JTextField(2));
                }
                board.add(box);
            }
        }

        // Create difficulty combo box
        JComboBox<String> difficultyChoices = new JComboBox<>(new String[] { "Hard", "Easy" });
        difficultyChoices.setSelectedIndex(0);

        // Create menu panel
        JPanel menu = new JPanel();
        menu.setLayout(new GridBagLayout());
        GridBagConstraints menuConstraints = new GridBagConstraints();

        menuConstraints.anchor = GridBagConstraints.WEST;
        menuConstraints.weightx = 0.5;
        menuConstraints.weighty = 0.5;
        menuConstraints.gridwidth = 2;

        menuConstraints.gridx = 2;
        menuConstraints.gridy = 0;
        menu.add(new JButton("Reset"), menuConstraints);

        menuConstraints.gridx = 2;
        menuConstraints.gridy = 1;
        menu.add(new JButton("Hint"), menuConstraints);

        menuConstraints.gridx = 2;
        menuConstraints.gridy = 2;
        menu.add(new JButton("Solve"), menuConstraints);

        menuConstraints.gridx = 2;
        menuConstraints.gridy = 3;
        menu.add(new JButton("New Puzzle"), menuConstraints);

        menuConstraints.weighty = 1.0;
        menuConstraints.gridx = 2;
        menuConstraints.gridy = 4;
        menu.add(new JLabel("Difficulty:"), menuConstraints);

        menuConstraints.fill = GridBagConstraints.HORIZONTAL;
        menuConstraints.weightx = 0.5;
        menuConstraints.weighty = 0.5;
        menuConstraints.gridwidth = 2;
        menuConstraints.gridx = 0;
        menuConstraints.gridy = 5;
        menu.add(difficultyChoices, menuConstraints);

        // Create output panel
        JTextArea output = new JTextArea(5, 20);
        output.setEditable(false);
        output.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLUE), "Output Area"));

        // Join the 3 panels on the frame
        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());

        cp.add(board, BorderLayout.WEST);
        cp.add(menu, BorderLayout.EAST);
        cp.add(output, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        // Create a frame and set its properties
        JFrame frame = new SudokuLayout();
        frame.setTitle("TestSudoku");
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null); // Center the frame

        // Setup the window
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

}

UPDATE: I update my answer with a GridBagLayout for the menu and a TextArea with border for the output.

Cristian Ramon-Cortes
  • 1,838
  • 1
  • 19
  • 32
  • Thank you so much for your help. May i ask you another question? Would you know how i may be able to create an output once the button was pressed? id assume an if else statemnt may allow for that, but im unsure how to implement it – Daniel Henrichs Aug 07 '17 at 19:16
  • @DanielHenrichs You can implement `Listeners` on the button creation that can also modify other GUI elements. However, as @Andrew Thompson said, consider accepting this question and ask another one so I can develop it more ;) – Cristian Ramon-Cortes Aug 09 '17 at 08:49