Reputation: 245
I'm working on a simple game as a learning exercise. The game consists of a grid of squares, each of which has a background image and can contain any number of objects and player tokens layered on top of each other. At the moment, the board is just a JPanel, and the images are drawn using drawImage() in the paintComponent() method. This technically works, but it's rather clunky and probably not the best way to do it. Now, I'd like to be able to repaint a given square without repainting the whole board, and it seems my original implementation is lacking.
The solution I came up with was to have the squares represented by a custom Icon class, and the board by a JPanel with a GridLayout containing the custom Icons. The problem there is that eventually, some of the background or object images will be animated, and there may be multiple animated things on a square. I was thinking I'd do this with animated GIFs.
I remember reading that using an ImageObserver in the drawImage method will cause the animation to show up, but it doesn't look like Icon is a ImageObserver. Would using the board JPanel as the ImageObserver be a good idea? Is there a better way to do it besides animated GIFs?
The other solution that came up was a grid of JLayeredPanes with JLabels containing the appropriate images layered on each other, but for some reason, that seems like swatting a fly with a sledgehammer.
Upvotes: 0
Views: 99