13 Replies - 554 Views - Last Post: 14 October 2012 - 03:36 PM Rate Topic: -----

#1 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Sudoku board

Posted 07 October 2012 - 12:06 PM

Hello,

I just started working on GUI components and I'm trying to create a Sudoku board. It doesn't need to be functional yet. I am just trying to get the layout. Anyone know why this code isn't producing a blank 81 square board? I am just getting a blank JFrame. I feel like my nested for loop should work, but I'm getting nothing. Any suggestions would be appreciated.


Thanks,
Adam


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



public class SudokuTest extends JFrame {

	public SudokuTest() {

		JTextField smallSquare = new JTextField(1);

		JPanel smallPanel = new JPanel();
		smallPanel.setLayout(new GridLayout(3, 3));

		JPanel largePanel = new JPanel();
		largePanel.setLayout(new GridLayout(3, 3));


		for (int i = 1; i <= 9; i++) {
			largePanel.add(smallPanel);

			for (int j = 1; j <= 9; j++) {
				smallPanel.add(smallSquare);
			}
		}
		smallPanel.add(smallSquare, BorderLayout.CENTER);
		largePanel.add(smallPanel, BorderLayout.CENTER);
	}


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

}



Is This A Good Question/Topic? 0
  • +

Replies To: Sudoku board

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Sudoku board

Posted 07 October 2012 - 12:12 PM

You never add your panels to your frame. Remember that your frame also has a container of components :)
Was This Post Helpful? 1
  • +
  • -

#3 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: Sudoku board

Posted 07 October 2012 - 12:20 PM

Yes, of course. Thank you for that quick reply. Very helpful! I have altered the code to add the panels. But now I only get one.

public SudokuTest() {

		JTextField smallSquare = new JTextField(1);

		JPanel smallPanel = new JPanel();
		smallPanel.setLayout(new GridLayout(3, 3));

		JPanel largePanel = new JPanel(new BorderLayout());
		largePanel.setLayout(new GridLayout(3, 3));


		for (int i = 1; i <= 9; i++) {
			largePanel.add(smallPanel, BorderLayout.WEST);

			for (int j = 1; j <= 9; j++) {
				smallPanel.add(smallSquare);
			}
		}
		add(smallPanel, BorderLayout.WEST);
		add(largePanel, BorderLayout.CENTER);
	}



This post has been edited by RenaldoTheNeck: 07 October 2012 - 12:21 PM

Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Sudoku board

Posted 07 October 2012 - 01:12 PM

Im confused about the whole smallpanel largepanel thing

You can not reuse the same JTextField over and over since you want different text in each of your cells

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

import java.awt.*;

public class SudokuTest extends JFrame {

	public SudokuTest() {
		setLayout(new GridLayout(3, 3));
		
		Border border = BorderFactory.createLineBorder(Color.BLACK);
		
		for (int i=0; i<9; i++) { //Add boxes with 9 cells
			JPanel box = new JPanel(new GridLayout(3, 3));
			box.setBorder(border);
			add(box);
			
			for (int j=0; j<9; j++) { //Add 9 cells
				JTextField field = new JTextField();
				box.add(field);
			}
		}
	}

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

}



Was This Post Helpful? 1
  • +
  • -

#5 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: Sudoku board

Posted 07 October 2012 - 07:51 PM

Excellent! Thank you, this really helps!! I will now work on getting the text bigger and select the font. I also need to add a menu on the right side to add four buttons that allow you to solve, get a new puzzle, get a hint, or reset puzzle.
Was This Post Helpful? 0
  • +
  • -

#6 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: Sudoku board

Posted 14 October 2012 - 01:03 PM

So I'm now told by my instructor that we need to use a 2d array to add the panels and textfields....I am pretty lost. Any ideas?? Here's the skeleton for my new approach:

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

public class ArrayTest extends JFrame {

	public ArrayTest() {

		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) {
		// TODO Auto-generated method stub

		/** Create a frame and set its properties*/
		JFrame frame = new ArrayTest();
		frame.setTitle("Sudoku");
		frame.setSize(600, 600);
		frame.setLocationRelativeTo(null); //Center the frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}

}



Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Sudoku board

Posted 14 October 2012 - 01:20 PM

by having a [9][9] 2D array you are no longer having the 3x3 boxes as you were having before

You need to look at each JPanel as being one cell where you need to put a number. Add a JTextField to each of your panels, then it would be just like before just without the 3x3 boxes
Was This Post Helpful? 1
  • +
  • -

#8 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: Sudoku board

Posted 14 October 2012 - 01:33 PM

I appreciate the quick response! Do you mean something like this? I feel so dumb.

public ArrayTest() {

		JPanel board = new JPanel(new GridLayout(9, 9));
		add(board);
		
		JPanel[][] squares = new JPanel[9][9];
		
		JTextField cellText = new JTextField();
		
		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();
				squares.add(cellText(1));
				board.add(squares[row][col]);
				
				
			}
			
		}
		


Was This Post Helpful? 0
  • +
  • -

#9 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Sudoku board

Posted 14 October 2012 - 01:37 PM

Almost. You should create a new JTextField instead of adding the same JTextField to all your panels
I think I already told you this before?
Try and add the same JTextField in every cell and then input a number when run, you will see why it doesn't work :)

Did you try and run your code?
Was This Post Helpful? 1
  • +
  • -

#10 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: Sudoku board

Posted 14 October 2012 - 02:39 PM

Here's another approach I am trying. If I get rid of the nested for loops, I think I can see it better. But this isn't working either.


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

public class ArrayTest extends JFrame {

	public ArrayTest() {

		JPanel board = new JPanel(new GridLayout(3, 3));
		Border border = BorderFactory.createLineBorder(Color.BLACK);
		
		JPanel squareOne = new JPanel(new GridLayout(3, 3));
			squareOne.setBorder(border);
				for (int i = 1; i <= 9; i++) {
					JTextField threeByThree = new JTextField(1);
					squareOne.add(threeByThree);
				}
			
		JPanel squareTwo = new JPanel(new GridLayout(3, 3));
			squareTwo.setBorder(border);
				for (int i = 1; i <= 9; i++) {
					JTextField threeByThree = new JTextField(1);
					squareTwo.add(threeByThree);
				}
			
		JPanel squareThree = new JPanel(new GridLayout(3, 3));
			squareThree.setBorder(border);
				for (int i = 1; i <= 9; i++) {
					JTextField threeByThree = new JTextField(1);
					squareThree.add(threeByThree);
				}
		JPanel squareFour = new JPanel(new GridLayout(3, 3));
			squareFour.setBorder(border);
				for (int i = 1; i <= 9; i++) {
					JTextField threeByThree = new JTextField(1);
					squareFour.add(threeByThree);
				}
		JPanel squareFive = new JPanel(new GridLayout(3, 3));
			squareFive.setBorder(border);
				for (int i = 1; i <= 9; i++) {
					JTextField threeByThree = new JTextField(1);
					squareFive.add(threeByThree);
				}
		JPanel squareSix = new JPanel(new GridLayout(3, 3));
			squareSix.setBorder(border);
				for (int i = 1; i <= 9; i++) {
					JTextField threeByThree = new JTextField(1);
					squareSix.add(threeByThree);
				}
		JPanel squareSeven= new JPanel(new GridLayout(3, 3));
			squareSeven.setBorder(border);
				for (int i = 1; i <= 9; i++) {
					JTextField threeByThree = new JTextField(1);
					squareSeven.add(threeByThree);
				}
		JPanel squareEight = new JPanel(new GridLayout(3, 3));
			squareEight.setBorder(border);
			for (int i = 1; i <= 9; i++) {
				JTextField threeByThree = new JTextField(1);
				squareEight.add(threeByThree);
			}
		JPanel squareNine = new JPanel(new GridLayout(3, 3));
			squareNine.setBorder(border);
				for (int i = 1; i <= 9; i++) {
					JTextField threeByThree = new JTextField(1);
					squareNine.add(threeByThree);
				}
		
			board.add(squareOne);
			board.add(squareTwo);
			board.add(squareThree);
			board.add(squareFour);
			board.add(squareFive);
			board.add(squareSix);
			board.add(squareSeven);
			board.add(squareEight);
			board.add(squareNine);

		
		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(squareOne);
		add(board);
		add(menu);
		

	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/** Create a frame and set its properties*/
		JFrame frame = new ArrayTest();
		frame.setTitle("Sudoku");
		frame.setSize(600, 600);
		frame.setLocationRelativeTo(null); //Center the frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}

}


Was This Post Helpful? 0
  • +
  • -

#11 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Sudoku board

Posted 14 October 2012 - 02:42 PM

You removed the 2D array that your teacher told you to use, why would you do that?
Was This Post Helpful? 0
  • +
  • -

#12 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: Sudoku board

Posted 14 October 2012 - 02:49 PM

Because I am an idiot. My brain is fried. Sorry. I'll keep working on getting that 2d array implemented.
Was This Post Helpful? 0
  • +
  • -

#13 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Sudoku board

Posted 14 October 2012 - 02:56 PM

Well you are having two problems with your code

Here you are trying to call a named named add on your array, but that won't work. Instead you should call add on the JPanel you just put in the array
squares.add(cellText(1));



It is as simple as
squares[row][col].add(new JTextField(5)); //the 5 just tells how big the JTextField should be



Remember that arrays are 0 indexed, so you should start your loops at 0 and not 1
Was This Post Helpful? 0
  • +
  • -

#14 RenaldoTheNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 29-February 12

Re: Sudoku board

Posted 14 October 2012 - 03:36 PM

Okay, thanks for your patience and knowledge. I've simplified it very much and this is what I have. Where do I go from here?

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


public class SudokuBoard extends JFrame {
		
		public SudokuBoard() {
		JPanel board = new JPanel(new GridLayout(9, 9));
		
		JPanel [][] squares = new JPanel[9][9];
		
		for (int row = 0; row < 9; row++) {
			
			for (int col = 0; col < 9; col++) {
				squares[row][col].add(new JTextField(1));
				board.add(squares[row][col]);
			}
		
		}

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

	}

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1