I am writing a game in Java which is based on a grid top down view. I am not very familiar with the JPanel paintComponent() and repaint() methods and I am having trouble creating a fast screen painting function.
Each tile is an object with a Tile.paintTile(g,x,y) to paint the tile at (x,y) on g. I want to find a nice way to paint only specific tiles, however I cannot find out how to do this.
I have a World class with a TileAt(x,y) to get the tile, but every attempt I have made has caused stuttering or thrown errors.
Any help is appreciated!
-Renmusxd
edit: what I tried:
I am not very used to Java graphics yet, I tried overwriting paintComponent and adding paintComponent(g,x,y) but it doesn’t like my not calling repaint. I no longer have the code because I substituted it with a paintAll version. Sorry.
Normally it is easier to just repaint the entire screen every time – this should be fast enough using Swing on most modern machines.
If you really want to do partial updates, then the best approach is probably:
BufferedImage, which should be the same size as your game display area (or slightly larger if you want to allow for scrolling)paintComponentmethod) then just draw the whole bufferedimage onto the screen in one go.