• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone please help me make this OO?  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason I have been staring at this code too long and I can't seem to figure out how I make it OO instead of procedural! This program is a basic Sudoku GUI (with no functionality).

For the input boxes, I have to create 81 blank text fields and I have tried 7 ways to Sunday to wrap this up into two for loops, one for the letter and one for the number nested inside of the letter loop.

Here is the nasty section I am trying to condense:



For completeness, here is my entire program. I know that I could use some more consolidation here and I am hoping once I see how it is done for those text boxes it will click how to for loop through the rest.



Thank you in advance for your help, oh, java gurus!
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you able to put just this section into a loop?


 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What is not object‑oriented about that code? It is repetitive, but you do appear to be using objects.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dayde Murphy wrote:For the input boxes, I have to create 81 blank text fields and I have tried 7 ways to Sunday to wrap this up into two for loops, one for the letter and one for the number nested inside of the letter loop.

First off: welcome to JavaRanch Dayde.

Second: What you're talking about is not really OO; it simple modularization.

Third: I'm no GUI expert, so there may well be better ways of doing it, but here goes:

First up, it looks like you have a 9 x 9 board, so why not make it a 2-dimensional array? viz:
Something[][] board = new Something[9][9];

The question then is: what are those "Somethings"? Well, what about a SudokuSquare? And what's a SudokuSquare? You've already decided that it's a JTextField.

And here's where the OO bit comes in: JTextField is not final - in fact, almost no Swing components are - so you can extend it. About the only other thing I'd add is that it might not be a bad idea for each square to "know" where it is on the board, and there is a class called Point (java.awt.Point) which is very useful for storing two integers at once. So, putting all that together, you might get something likeand now you can add anything else you think you might need for a single square, and your "board" becomes:
SudokuSquare[][] board = new SudokuSquare[9][9];

And since each square IS a JTextField, you can do anything with it that you were going to do with your original fields.

BTW: The reason for having getIndex() return a new Point is that Point is mutable (look it up). If you don't return a copy, then anyone who gets an index would be able to change it, which could lead to nasty problems.

The definition of 'board' above has just defined the array, you now need to fill it with "squares". I'll leave you to work out how you might do that with a loop.

I should add that the above is only ONE way of doing it - and possibly not the best (like I say, I'm no GUI expert). But hopefully it's given you something to think about.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick question for the GUI experts out there - I don't mean to hi-jack the thread; but it is part of this topic, and I was just interested:

Is it necessary to create a new LineBorder for each component, or can you just create one and have it shared by a bunch of them?

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!