About preparing a risk game card with a swing in java

I want to start coding a game project called Risk, and my first goal is to build a map correctly. So it is logical that each territory should be JButton, but JButton are oriented in a straight line. Moreover, I know that each territory should be a component, so I can use mouse event listeners for each of them. well my question

  • Should I try to draw each territory with coordinates, lines, shapes, etc.? or
  • Is there a way to draw and combine each territory regularly?

On the other hand, this is a link to the map of the game.

Risk map

+4
source share
2 answers

try to make fixed territories, so you should not have headaches when resizing your actual territory, change the color of only the territory that you occupied recently, for example, in Dune2, if you know this game. And I think that finally you should, and put this jbutton under the numbers on your map (or what will be finally better if you replace the numbers with the name of the territory, and you put this button under it). I hope my answer answered your doubts :)

+4
source

I feel that it will be a great job. An easy way out would be to simply put the JButtons under the numbers.

If you still feel like creating irregular clicks, I suggest creating Polygon instances (java.awt) for each country. They are created using arrays of points x and y, which define the angles. Conveniently enough, there is a Polygon.contains (x, y) method that lets you know if (x, y) is in your polygon. If you use a large JPanel that covers the entire map and gets the mouse location relative to the JPanel, you can notify each country about whether the mouse is inside it.

+4
source

All Articles