• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Carey Brown
  • Frits Walraven

newbie needs your help with a grid!

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.
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...
Posts: 3036
Mac IntelliJ IDE Python VI Editor Java
  • 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.
Ranch Hand
Posts: 15304
Mac OS X IntelliJ IDE Chrome
  • 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.
Take a look at that. You will probably need jdk1.4.1.
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
Two software engineers solve most of the world's problems in one K&R sized book
    Bookmark Topic Watch Topic
  • New Topic