4

I want to make board(map) like this in Java.

enter image description here

Each small hexagon is image.

Suppose I have two Java classes. Canvas(big hexagon) and Hexagon. First is entire board from which I generate randomly all small hexagons. Both classes derived from JPanel. Now I have GridLayout. How can I arrange layout like this?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Tigran Tokmajyan
  • 1,937
  • 7
  • 25
  • 36
  • 1
    gridLayout does rows and columns. Yours would have to overlap. You might have to use absolute positioning (no layout manager). – stark Mar 05 '12 at 12:48
  • If you may, Try your hands at [GroupLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html), that might help you sort things out. – nIcE cOw Mar 05 '12 at 12:55
  • May I suggest a completely different approach: Don't use a toolkit which is designed to create Desktop-Applications for games. – Bobby Mar 05 '12 at 13:09

3 Answers3

3

Why do you need the small hexagon panels? I would rather just define List (list of Hexagons) each with desired position and just override paintComponent() method of main JPanel. You can use this http://java-sl.com/shapes.html to create hexagon shapes.

To track mouse click you can use contains() method of Shape.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • +1 This related [Q&A](http://stackoverflow.com/q/3687176/230513) has a nice sscce and some tips on the geometry. – trashgod Mar 05 '12 at 16:52
3

you can

1) common way

  • by painting to the JPanel/JComponent by override paintComponent() (I asumed that there are Image/BufferedImage/Icon/ImageIcon)

2) by place Icon/ImageIcon to the JLabel

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

The point of layout manages is to make it possible for the layout to auto-adjust when components change their size or the window does.

It looks like your hexagons will always be the same size, so you really don't need a layout manager, and positioning the hexagons absolutely should be fine.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720