Java swing small 2D game: how to model a view?

In a small 2D java swing game, what's the best solution for creating a board look?

  • Use the board component and custom paint and each square of the control panel at once?
  • Use the component for the board and create another component that simulates a square, with its own paint component, performing the task only for the square. Use a layout to place each square copy on the board?

I know that this is subjective, and I do not want to fight about it. I just need some tips to figure out how I should go. I started the project, and I use 1), feeling that something is wrong.

+4
source share
2 answers

Just go with one drawing / container panel for everything.

Consider a tile based game. Using your second solution, each tile will be an object, memory will grow rapidly, and the game will slow down when scanned.

Using the first option, you are flexible. You draw what you want, you have the panel coordinates, so everything is relative to this. Using the tile-based example, you know the height and width of the panel, draw a square and increase the X and Y coordinates as needed.

Graphical GUIs such as Swing or .NET Winforms are expensive because they have many other things that the game will not need. Therefore, to answer your question, go with option one, instead of using the panel for each check on your board.

One of the nice solutions for using the second method in combination with the first method is the Weighted Design Template . You can still use OO objects, but you will have a fraction of the amount that you usually make. Check this.

+6
source

Like Finglas, I am leaning towards your first approach. This tile based game is a concrete example. In contrast, this component game uses the GridLayout of JToggleButton . The first is somewhat more complicated, but plays well even on large screens; the latter is simpler, but sufficient for reasonable games. You may need to experiment with your game geometry and logic to decide which one is preferable.

+3
source

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


All Articles