Results 1 to 8 of 8
Thread: Multiple Graphics Objects?
- 10-28-2010, 04:45 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
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:

The game is centered completely around this tile system and cannot be changed. The gameplay is centered around it.
Just trying to get this to run. It doesn't. This line has NullPointerException-Java 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); }
Java Code:worldImage.drawRect(r*5, c*5, 5, 5);
Last edited by MrFish; 10-28-2010 at 04:48 AM.
- 10-28-2010, 05:22 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
It looks like worldImage is null. You can check this using System.out.println() just before you call drawRect().
The game is centered completely around this tile system and cannot be changed.
Cannot? Anyway, I'll just observe that it's one thing to have a whole bunch of addressable components of the world and quite another for them to be components with all the overhead that that entails. Some widgets in the Swing library - like table cells which are rather tile like - use the idea of a renderer which acts as a "rubber stamp" capable of doing whatever visual stuff is needed whereever it is needed.
- 10-28-2010, 09:54 AM #3
Also look into Graphics#copyArea(...)
db
- 10-28-2010, 12:44 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
Yes, CANNOT.
I already knew it was null. Why is it null? It's not like I can do Graphics worldImage = new Graphics();
Java Code:public class Display extends JPanel { private static ArrayList<WorldObject> WorldObjectArray; private static WorldGrid World; private static JFrame Frame; private static Graphics worldImage; . . . 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); } } } . . . }
- 10-28-2010, 01:11 PM #5
Why don't you just use something like a BufferedImage, draw all of the tiles on at the beginning, then update only tiles that have changed when appropriate? Then all you'd need to do in paintComponent was draw the image.
But worldImage is null because you never set it to anything. You get a Graphics instance as a parameter in paintComponent(), if you wanted to go that route.
- 10-28-2010, 01:13 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
That's all the information I needed. Now I will look into BufferedImages
- 10-28-2010, 01:36 PM #7
- 10-29-2010, 07:37 PM #8
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
Similar Threads
-
using multiple methods for graphics
By jforce93 in forum Advanced JavaReplies: 3Last Post: 04-25-2010, 06:05 PM -
Clear Graphics Objects from Jpanel
By DavidG24 in forum AWT / SwingReplies: 2Last Post: 05-20-2009, 09:34 PM -
how to deserialize multiple objects in a file
By xcallmejudasx in forum Advanced JavaReplies: 11Last Post: 12-16-2008, 05:29 PM -
Can I store multiple objects in an array
By lareauk in forum New To JavaReplies: 9Last Post: 05-29-2008, 03:57 AM -
Can I use vectors to store multiple types of objects
By Nathand in forum Advanced JavaReplies: 6Last Post: 04-28-2008, 07:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks