This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Game Development and the fly likes Boardgame to interactive GUI -- need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Boardgame to interactive GUI -- need help " Watch "Boardgame to interactive GUI -- need help " New topic
Author

Boardgame to interactive GUI -- need help

Will Blanc
Greenhorn

Joined: Jul 11, 2008
Posts: 8
We want to make a computer game using this hexagonal game-board/map as the GUI:

http://dwarfstar.brainiac.com/barbarianprince/barbarianprince_map1.jpg

When you stop moving on a hex, an event happens. We are stuck because we do not know how to make each individual hex have its own characteristics/properties. The only thing we can think of is to make each hex an object, but we don't know how to do anything that advanced (is it even possible to make different sections of an .jpg image an individual object?).

Thanks!
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Draw out your hex grid on a JPanel.
Create a class that represents a cell object to hold the data you will need for each cell.
As you lay out your grid on the panel you can assign each cell. A cell can know how to draw itself with the data provided when you layout the grid. Cell selection with a mouse can take place with a mouse listener added to the panel that draws them. In the mouse listener you can message the selected cell object to elicit the behavior you want.
Jim Clarke
author
Ranch Hand

Joined: Jun 15, 2009
Posts: 49
I am sure you can do this with Java Swing, but let me answer how I would approach this with JavaFX.

First I would create a CustomNode for the hex position, maybe call it HEXButton. This node would have a clipping region
using a Hex shape. Next the background would be from the main map1 image with an ImageView
that is translated to the correct position on the map.
The clip would only show the part within the clip shape. Next, I would add mouseEvent functions to handle
the mouse clicks, etc.

The whole board would be a collection of so many HEX "Buttons" arranged in their proper position.

If you want to stay in the Java space, my I recommend the book "Filthy Rich Clients". It has many examples
of this sort of thing and actually was the design inspiration behind the JavaFX UI model.

jim
Will Blanc
Greenhorn

Joined: Jul 11, 2008
Posts: 8
Craig Wood wrote:As you lay out your grid on the panel you can assign each cell. A cell can know how to draw itself with the data provided when you layout the grid.


Thank you for responding

So each Hex will have to be a separate image file?

Thank you,
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
You could do it either way.
1 — draw the image in the background of the panel and then draw the hex objects next (to appear between the background image and the viewer).
2 — clip the image for each hex object and let each cell draw its portion of the background image before it draws itself (between the image and viewer).
One consideration in your choice might be the possibility of fluid resizing of the component that does the drawing and/or the parent frame. Or maybe you would have a fixed size for your graphic component and keep it in a JScrollPane.
Whichever way makes more sense to you would be a good starting point. You can always change things around while you are putting this together.

So each Hex will have to be a separate image file?
No, I wouldn't make a separate file for each one. You can clip the image and make a new BufferedImage of the clipped portion and store it in each hex object. You would do this at startup in your app. So if you have 200 hex cells you might have an array of hex objects, each one having an image as one of its member variables. Of course this is a lot more work than option 1.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Moving to Game Development


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peter Granstrom
Greenhorn

Joined: Jun 08, 2009
Posts: 10
nice project.. cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boardgame to interactive GUI -- need help