• 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

newbie needs your help with a grid!

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Pete!
I'm moving this to The Swing / JFC / AWT Forum...
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic