5

I'm currently working on my GUI for this Sudoku solver I'm making. I've managed to print out the board with no problems. However I'd like to know how I would go about to differentiate the 3x3 regions with some kind of thicker or coloured line.

Basically something resembling the picture below.

sudoku

Below is the code I've already implemented. Thanks!

    Board = new JPanel(new GridLayout(9, 9));
    for(int i= 0; i < 9; i++) {

        for(int j = 0; j < 9; j++) {

            board[i][j] = new JLabel();

            board[i][j].setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));


            Font font = new Font("Arial", Font.PLAIN, 20);

            board[i][j].setFont(font);

            board[i][j].setForeground(Color.WHITE);

            board[i][j].setBackground(Color.WHITE);


            board[i][j].setOpaque(true);

            board[i][j].setHorizontalAlignment(JTextField.CENTER);

            Board.add(board[i][j]);

        }
    }
Community
  • 1
  • 1
zak.oud
  • 152
  • 1
  • 2
  • 6

3 Answers3

6

By far the easiest way would be to use nine 3x3 JPanels of JLabels nested into one large 3x3 JPanel of JPanels. Then you could just apply special borders to the small 3x3s.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 3
    Still don't understand how I can do this. I've looked online and all I can find is command prompt boards. They seem to be using modulo 3 statements to make them stand out more. Isn't there a similar way for JLabels that would be easier and shorter to do? – zak.oud Mar 17 '12 at 00:04
4

What if you created your own custom JPanel to hold a digit and draw a black border - and then a custom JPanel to hold a grid of those?

Example custom JPanel:

    class SudokuPanel extends JPanel {

        int digit; //the number it would display
        int x, y; //the x,y position on the grid

        SudokuPanel(int x, int y) {
            super();

            this.x = x;
            this.y = y;

            /** create a black border */
            setBorder(BorderFactory.createLineBorder(Color.black));

            /** set size to 50x50 pixel for one square */
            setPreferredSize(50,50);
        }

        public int getDigit() { return digit; }

        //getters for x and y

        public void setDigit(int num) { digit = num }

    }

Example custom grid JPanel:

    class SudokuGrid extends JPanel {

        SudokuGrid(int w, int h) {
            super(new GridBagLayout());

            GridBagConstraints c = new GridBagConstraints();
            /** construct the grid */
            for (int i=0; i<w; i++) {
                for (int j=0; j<h; j++) {
                    c.weightx = 1.0;
                    c.weighty = 1.0;
                    c.fill = GridBagConstraints.BOTH;
                    c.gridx = i;
                    c.gridy = j;
                    add(new SudokuPanel(i, j), c);
                }
            }

            /** create a black border */ 
            setBorder(BorderFactory.createLineBorder(Color.black)); 

        }

    }

Example code:

...
SudokuGrid sg = new SudokuGrid(3,3);
myFrame.add(sg);
...
Ozzy
  • 8,244
  • 7
  • 55
  • 95
1

Here is some sample code from Yahoo Answers! on how one would add a JPanel to a JPanel

    import javax.swing.*;

public class RecursiveJPanelTest
{
public static void main(String[] arg)
{
JFrame window = new JFrame();
JPanel top = new JPanel();
JPanel within = new JPanel();

window. setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);

window.setSize(400, 200);
top.setSize(400, 200);
within.setSize(400, 200);

window.add(top);
top.add(within);
within.add(new JButton("Button"));

window.validate();
window.setVisible(true);
top.setVisible(true);
within.setVisible(true);
}
}

Couldn't post this in a comment without having it all messed up.

Griffin
  • 710
  • 2
  • 15
  • 29