Multiple Graphics Objects?
I'm trying to make a game with many tiny tiles. The 2d array holding these tiles is 1000x1000 in size and expected to get bigger. In this game I will need to paint a color to each tile depending if it's dirt, grass, sky, etc. Obviously, with 1 million tiles, painting each tile everytime the paintComponent method is called is a terrible idea. But the array is subject to change by the user from time to time. What I wanted to do was create multiple graphics objects and paint them on top of one another like layers. This way, the player and ai layer that will constantly update won't also make the more inactive layers paint themselves as well. I don't know how to do this and I tried to make multiple graphics objects but got errors. NullPointerException. I'm confused on how Graphics works because I know it's abstract (don't even know what that is). So how do I do this?
See my beautiful diagram:
http://img200.imageshack.us/img200/7842/layersi.png
The game is centered completely around this tile system and cannot be changed. The gameplay is centered around it.
Code:
public void updateWorldGrid()
{
for(int r = 0; r < 1000; r++)
{
for (int c = 0; c < 1000; c++)
{
worldImage.drawRect(r*5, c*5, 5, 5);
}
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
updateWorldGrid();
drawWorld();
for(WorldObject w: WorldObjectArray)
{
draw(g, w);
}
}
public void drawWorld()
{
paint(worldImage);
}
Just trying to get this to run. It doesn't. This line has NullPointerException-
Code:
worldImage.drawRect(r*5, c*5, 5, 5);