Results 1 to 3 of 3
  1. #1
    GAZ082 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Question 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?

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    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.

  3. #3
    GAZ082 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

Similar Threads

  1. JXL Empty cell problem
    By ironicending in forum New To Java
    Replies: 1
    Last Post: 01-26-2009, 04:32 PM
  2. set different font for each cell in JTable
    By success21061985 in forum AWT / Swing
    Replies: 3
    Last Post: 09-10-2008, 03:06 PM
  3. jTable Cell editing
    By mahaling_m in forum AWT / Swing
    Replies: 0
    Last Post: 06-23-2008, 09:17 AM
  4. Jframe In Jtable cell
    By Clarion in forum AWT / Swing
    Replies: 4
    Last Post: 06-23-2008, 05:42 AM
  5. Select specific cell
    By Echilon in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 08:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •