• Post Reply Bookmark Topic Watch Topic
  • New Topic

newbie needs your help with a grid!

 
Pete Davies
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I would like to know how to create a playing grid. I must be able to have access to the individual cells of the grid for e.g. being able to fill-in each cell with a certain colour, storing their states etc...
Having done a bit of research, I think 2D arrays would be the best thing to use, but..how?
I am using swing to build the interface, so can I use swing it with a 2D array to build a grid?? Someone suggested Jtables on another board, but I'm not convinced.
Help me PLEASE!!! Cheers.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Pete!
I'm moving this to The Swing / JFC / AWT Forum...
 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a two-dimensional array would do the job for storing the data. When you get more advanced, you will start calling that the model. However, there is no magic way to turn the array into the grid you describe. One day, you'll call that the view. I think the best bet is to extend JPanel, or even Canvas, and override the paint method to draw your grid, based on information from the array. There's also the idea of a controller, which allows a user to change the data in the model, and see then see the changes reflected in the view. You didn't ask about that though, so I'll stop confusing you.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another similar option is to have a JPanel with a GridLayout and then place JButtons' on each cell of the GridLayout. The reason I suggest buttons is because you talked like you needed something to happen when you click. And it is a bit simpler to create an ActionListener on a JButton than to create a MouseListener for different regions of a Grid.
An example is an Applet that my friend wrote to simulate a Robot Walking around on a Grid. He used JButtons on a Grid to track the movements by changing the backgroud color of each Button the robot "Stepped" on.
http://derek.embeddedthought.com/robot.html
Take a look at that. You will probably need jdk1.4.1.
 
Paper has not yet been outlawed – so let your Java Swing application print with wild abandon: JPDF
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!