I would consider the 2D array as a model different from the GUI, that the data itself is likely to be stored in some kind of data file, possibly in a text file, that there will be methods for reading the data that the 2D array should be stored, maybe ints, possibly custom Cell classes (again, still not a GUI). Then the GUI would have to interpret the model and display the data incorrectly. Perhaps this can be done by creating a JLabels 2D grid stored in JPAnel that uses GridLayout, and then use ImageIcons to store the images and set the icon of each JLabel based on the state of the model.
Change 1
Thus, the classes used include:
- TileType: an enumeration that associates the concept of a tile with the numbers stored in a data file
- TileCell: not a GUI class, contains a TileType field, can also contain a list of elements that can be found in a cell (if the game needs it). May have information about their neighbors.
- TileCellGrid: A non-GUI class containing a TileCells 2D mesh.
- GridDataIO: utility class for reading and writing grid data to a file.
- GameGrid: A GUI class that will contain a GridLayout, using a JPanel that contains JLabels, whose ImageIcons display the images that you list in your OP.
Edit 2
regarding your question:
Ok, how can I set the correct image for each tag?
I would use an observable / observable pattern and add a listener to the model. Whenever a model changes, it should thus notify the GUI or view. Then the view requests an array of data, will pass through it and will change the icons of the images that need to be changed when it passes through the array.
source share