Results 1 to 8 of 8
  1. #1
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default 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.

    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);
    	}
    Just trying to get this to run. It doesn't. This line has NullPointerException-

    Java Code:
    worldImage.drawRect(r*5, c*5, 5, 5);
    Last edited by MrFish; 10-28-2010 at 05:48 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Also look into Graphics#copyArea(...)

    db

  4. #4
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    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);
    			}
    		}
    	}
    
            . . .
    
    }

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default

    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.

  6. #6
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    That's all the information I needed. Now I will look into BufferedImages

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default

    Quote Originally Posted by MrFish View Post
    That's all the information I needed. Now I will look into BufferedImages
    Hm, ok. Let us know how it goes.

  8. #8
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

Similar Threads

  1. using multiple methods for graphics
    By jforce93 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2010, 07:05 PM
  2. Clear Graphics Objects from Jpanel
    By DavidG24 in forum AWT / Swing
    Replies: 2
    Last Post: 05-20-2009, 10:34 PM
  3. how to deserialize multiple objects in a file
    By xcallmejudasx in forum Advanced Java
    Replies: 11
    Last Post: 12-16-2008, 06:29 PM
  4. Can I store multiple objects in an array
    By lareauk in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 04:57 AM
  5. Can I use vectors to store multiple types of objects
    By Nathand in forum Advanced Java
    Replies: 6
    Last Post: 04-28-2008, 08:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •