So I’m trying to make a simple level editor for a tile based game using swing. I thought about having an array of Images which contains tiles, then the array of Images would be painted in the main window. But how do I paint the array of Images when using swing? I’ve read about custom components but don’t know if that’s the right way to go. Could someone lead me to the right direction?
alright,
For a tool Swing actually isnt a bad way to go, as long as you realize that the way you do paiting and such for aan actual game will likely be very different.
The way you do custom paiting in Swing is to subclass a Swing component and ovveride the paintComponent() call. The msot basic component for display purposes to subclass is JPanel.
Take a look at paintCOmponent and you wills ee that it has a Graphics object passed in. This is really a Graphics2D, which is a subclass of Graphics. You can downcastt he pased in Graphics safely to a Graphcis2D.
If you look at the javadoic for Graphics and Graphics2D you wil lsee al lsorts of drawing calls including a fvew versions of drawImage which is likely what you will use to draw your tiles.
FInally, to get the screen to update when youchnage your tiles, call the repaint() method on that subclassed JPanel.
Good luck!