Custom Java Swing Layout

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

enter image description here

Each little hexagon is an image.

Suppose I have two Java classes. Canvas (large hexagon) and hexagon. First, this is a whole board from which I randomly generate all the small hexagons. Both classes are derived from JPanel. Now I have a GridLayout. How to organize the layout as follows?

+4
source share
3 answers

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

You can use the contains () method of the form to track the mouse click.

+3
source

You can

1) common path

  • by painting JPanel/JComponent with override paintComponent() (I assumed there is Image/BufferedImage/Icon/ImageIco n)

2) put Icon/ImageIcon in Icon/ImageIcon

+3
source

The layout control point allows you to automatically adjust the layout when the 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 should absolutely be fine.

+1
source

Source: https://habr.com/ru/post/1399773/


All Articles