• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Turning a file into a grid

 
Christi Swan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody!

I'm not sure how to go about doing this . I am writing a sudoku GUI in Java, for class.  We have to give the user an option to select the file the want to use for the puzzle. I have created the frame and the panels. I use JFileChooser to bring in the file and that works. However, my problem is this, how do I take the file and display it as a sudoku grid? I've scoured the internet and found similar situations but I can't seem to make them work. I've included the text file that is being displayed. ANY help and direction would greatly be appreciated!

This is the working code that I have, but it only displays the txt file as written. I'm assuming I need to put in a GridLayout into the displayPuzzle JTextArea, but am unsure of how to get each, number into an appropriate "square" . The grid needs to be a 9x9 sudoku grid and should have 3 subgrids of 3x3... (not sure if this is relevant to my question or not but I would still need to code it). I need to take the input from the text file and assign each value from the txt file into a grid space.




This is the file that is being used...(I couldn't figure out a valid format in which to attach the file to the post)

x7xx5xxxx
3x428xx1x
x16x9x82x
83xxxx4xx
4xx867xx3
xx1xxxx82
x43x2x97x
x8xx713x5
xxxx3xx4x

 
Carey Brown
Bartender
Posts: 1966
24
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several options that somewhat depend on what you want the program to do after the original board is displayed.

You could create a square JPanel and draw the text on the panel. Roughly, you'd have to divide the height and width by 9 and use those x,y coordinates for drawing the text. This approach has the advantage of being able to easily draw lines surrounding the 3x3 groups of text.

Again, with a JPanel you could populated it with one of: JLabel, JButton, JTextField, or JFormattedTextField. You'd have to use setBounds() to position them at appropriate x,y locations. Adding lines is easy here.

You could create a 9x9 grid layout and fill it with one of: JLabel, JButton, JTextField, or JFormattedTextField. This is quick but leaves the sizing to Swing. Not so easy adding lines.
 
Christi Swan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking and actually started creating a 9x9 grid. Would each cell need to be a text field?
 
Christi Swan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:

You could create a square JPanel and draw the text on the panel. Roughly, you'd have to divide the height and width by 9 and use those x,y coordinates for drawing the text. This approach has the advantage of being able to easily draw lines surrounding the 3x3 groups of text.

.


My J-Frame is a split panel. I created a panel for all my buttons and another panel for my board. My board panel is called boardPane. The  displayPuzzle JTextArea is where I thought I needed the file to scan into. So are you saying that I can take my boardPane divide into a  9x9  grid and scan into that pane?
 
Piet Souris
Rancher
Posts: 1543
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used a main Jpanel with 3x3 gridlayout. Each element is itself a jpanel with 3x3 gridlayout. The panels have a black background.  By setting appropriate hgaps and vgaps in the gridlayouts you don't need to set any lines. The cells are JLabels, preferred size about 40 x 40, with the text (ie 1-9) centered both horizontally and vertically. I found this setup easy to work with.
 
Carey Brown
Bartender
Posts: 1966
24
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christi Swan wrote:
Carey Brown wrote:You could create a square JPanel and draw the text on the panel. Roughly, you'd have to divide the height and width by 9 and use those x,y coordinates for drawing the text. This approach has the advantage of being able to easily draw lines surrounding the 3x3 groups of text..
My J-Frame is a split panel. I created a panel for all my buttons and another panel for my board. My board panel is called boardPane. The  displayPuzzle JTextArea is where I thought I needed the file to scan into. So are you saying that I can take my boardPane divide into a  9x9  grid and scan into that pane?

I would scan the file into an array of char[9][9] first. I would create your own GridPanel that extends JPanel and the override the paintComponent() method to draw the grid based on the contents of the 9x9 char array.
 
Christi Swan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Thanks I'm going to try that!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic