Results 1 to 3 of 3
- 02-09-2009, 04:06 PM #1Member
- Join Date
- Feb 2009
- Rep Power
Getting a grid with cell properties to work.
Hi guys. I'm 26 and starting to learn OOP and Java. The best way to motivate myself is writing a simple game with the concepts i'm learning. Of course, there is a lot to rewrite every time i learn something radically better than i used, but that does not fade out the fun factor.
Now i'm struggling with a hexgrid. I made a Class "Cell" and a subclass of JPanel Grid which renders an array of cells.
Cell has certain properties such as axis coordinates, (X;Y), terrain type and according to the terrain type, gets a different Color.
Grid is basically the graphical instructions to render it and include it into a JFrame. Has the method paintComponents.
Now, what i want to do in some days is to click over a cell and get it's property, such as X,Y and terrain type. Now, i should try to make an Array of JPanels (making every Cell a JPanel extension) or rendering all the cells inside a JPanel (Grid) will be just fine and if i add to the class Cell a getMouseClick (or something) will detect that the cell was actually clicked?
- 02-11-2009, 06:08 PM #2
Since your intent is to reinvent the wheel, I'll just mention a few avenues you can venture down.
First, JTable is the component that does what you are talking about with minimal programming. It allows you to define your own data model (ignore the double array stuff; that is a workable approach, but you can store the information any way you want. I prefer an ArrayList for each column, if the columns are not all identical.) and cell renderer and editor. It's intimidating at first, but it turns out to be fairly easy and quite powerful. Used with JScrollPane, it provides a scrollable list with very little effort.
If you want to do it yourself, start out with a JPanel that holds all the cells, and then look at layout managers, either GridLayout or GridBagLayout. I use GridBagLayout a lot, but I also use a form designer to avoid a ton of hand coding. Grid bag provides tremendous flexibility, once you understand it. These layouts will provide you with a grid, so you don't have to manually position every component.
Look at JLabel for your cell contents. You can set the shape, the background color, and add an ImageIcon (which is really just an image of any kind). In addition, you can add listeners to each JLabel for mouse, keyboard, etc. events.
As you play around, spend the time to learn the "right" way. Normally, implementing paint() methods should be reserved for graphics type applications.
Last, learn about the event dispatcher thread, if you haven't yet. That where all the GUI code runs. As you get into more advanced applications and worker threads, understanding how to keep all GUI-related code running on the EDT is important.
- 02-11-2009, 09:17 PM #3Member
- Join Date
- Feb 2009
- Rep Power
- By ironicending in forum New To JavaReplies: 1Last Post: 01-26-2009, 04:32 PM
- By success21061985 in forum AWT / SwingReplies: 3Last Post: 09-10-2008, 02:06 PM
- By mahaling_m in forum AWT / SwingReplies: 0Last Post: 06-23-2008, 08:17 AM
- By Clarion in forum AWT / SwingReplies: 4Last Post: 06-23-2008, 04:42 AM
- By Echilon in forum New To JavaReplies: 1Last Post: 01-01-2008, 08:47 AM