• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tic Tac Toe - Drawing panels on grid?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic