aspose file tools*
The moose likes Game Development and the fly likes Tic Tac Toe - Drawing panels on grid? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Tic Tac Toe - Drawing panels on grid?" Watch "Tic Tac Toe - Drawing panels on grid?" New topic
Author

Tic Tac Toe - Drawing panels on grid?

Bear Kim
Greenhorn

Joined: Feb 25, 2012
Posts: 9
Hi, I'm making an interactive tic tac toe game using java. I wrote the code for the actual inner game workings (testing for wins, etc.), and now I need to work on the graphics portion.
I used JPanel/JFrame swing library to create a three by three board. What I need help with is creating panels that I can draw on for each square of the board (for the Xs and Os).
I'd really appreciate any input and help, thanks in advance!

Bear
Bill Krieg
Greenhorn

Joined: Jul 31, 2011
Posts: 9
Make a new cell class that extends JPanel. Provide a setter and getter for the state (X, O, or blank). Override paintComponent to check the current state and paint an X, O, or just the background. This will be a very short and simple class. If you synchronize access to the setter, getter and paintComponent, you won't even need to worry about updating on the Event Dispatch Thread. Every time you call the setter, also call repaint().

Create nine objects of this class to represent the nine tic tac toe cells. The containing panel can use a 3x3 GridLayout to hold these nine cells.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

You could create a custom JPanel subclass which is constructed with a reference to your game model, and the index of the field it's supposed to represent. You should override its paintComponent(Graphics g) method in order to let it properly repaint the field when it needs to. Mess around with it for a bit and tell us what problems you're running into.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Bill Krieg wrote:Provide a setter and getter for the state (X, O, or blank).


Personally I think it's a better idea to separate the GUI and the game logic. Even if this is a fairly simple program, it's still good to get into the habit of making strong cohesive classes with clear responsibilities.
Bill Krieg
Greenhorn

Joined: Jul 31, 2011
Posts: 9
Stephan van Hulst wrote:
Bill Krieg wrote:Provide a setter and getter for the state (X, O, or blank).


Personally I think it's a better idea to separate the GUI and the game logic. Even if this is a fairly simple program, it's still good to get into the habit of making strong cohesive classes with clear responsibilities.


The paintComponent method still needs to know whether it's drawing an X, O or blank. How else would you suggest it makes that determination? I think having a reference to the game model is more closely tied to the game logic than simply having a flag indicating what shape to draw.
 
 
subject: Tic Tac Toe - Drawing panels on grid?