Results 1 to 5 of 5
  1. #1
    CTucker1327 is offline Member
    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0

    Default Can't draw a BufferImage array.

    Alright, so I'm just starting off with the Graphics portion of Java and what I'm trying to do is create a Tile Based Map Editor, right now, what I'm doing is Importing an Image, cutting it into different squares, and then saving those squares in a BufferedImage array, however. I can't draw them? (Nothing shows up).

    Please Excuse the C# Conventions, as I find it easier to read this way (The way the brackets are)

    SpriteSheet.java

    Java Code:
    package com.lpstudios.mapeditor;
    
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    public class SpriteSheet 
    {
    
    	public int xSize, ySize;
    	
    	public int rows, columns;
    	
    	public BufferedImage spriteSheet;
    	
    	public BufferedImage[][] sprites;
    	
    	public SpriteSheet(String dir, int xSize, int ySize)
    	{
    		this.xSize = xSize;
    		this.ySize = ySize;
    		try 
    		{
    			System.out.println(dir);
    			this.spriteSheet = ImageIO.read(new File(dir));
    			this.rows = this.spriteSheet.getHeight() / this.ySize;
    			this.columns = this.spriteSheet.getWidth() / this.xSize;
    			
    			for(int xAxis = 0; xAxis < this.columns; xAxis++)
    			{
    				for(int yAxis = 0; yAxis < this.rows; yAxis++)
    				{
    					try 
    					{
    						sprites[xAxis][yAxis] = spriteSheet.getSubimage(xAxis * this.xSize,
    								yAxis * this.ySize, this.xSize, this.ySize);
    					} 
    					catch(NullPointerException e)
    					{
    						System.out.println("[Error]: Loading");
    					}
    				}
    			}
    			
    		} 
    		catch(IOException e)
    		{
    			e.printStackTrace();
    		}
    		
    		sprites = new BufferedImage[columns][rows];
    		System.out.println(sprites.length);
    	}
    	
    	public void render(Graphics g) 
    	{
    		for(int xAxis = 0; xAxis < this.columns; xAxis++)
    		{
    			for(int yAxis = 0; yAxis < this.rows; yAxis++)
    			{
    				try 
    				{
    					g.drawImage(sprites[xAxis][yAxis], xAxis * this.xSize,
    							yAxis * this.ySize, this.xSize, this.ySize, null);
    				} 
    				catch(NullPointerException e)
    				{
    					System.out.println("[Error]: Drawing");
    				}
    			}
    		}
    	}
    	
    }

    Here's the render function in the main-game loop

    Java Code:
    	public void render()
    	{
    		BufferStrategy bs = getBufferStrategy();
    		if(bs == null)
    		{
    			createBufferStrategy(3);
    			return;
    		}
    		
    		Graphics g = bs.getDrawGraphics();
    		g.setColor(Color.BLACK);
    		g.fillRect(0,  0, 800, 600);
    		//Render here
    		spriteSheet.render(g);
    		//End Render
    		
    		g.dispose();
    		bs.show();
    	}

    This is initialized in the Main Constructor.

    Java Code:
    spriteSheet = new SpriteSheet("Tilesheet.png", 32, 32);



    However, nothing is drawn, it's just showing the screen as black.
    Last edited by CTucker1327; 11-22-2013 at 07:39 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Can't draw a BufferImage array.

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    CTucker1327 is offline Member
    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0

    Default Re: Can't draw a BufferImage array.

    Quote Originally Posted by Norm View Post
    Can you make a small, complete program that compiles, executes and shows the problem?

    I figured it out.

    I was setting the values of the array before actually constructing it.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Can't draw a BufferImage array.

    Line 54 looked like it creates a new array AFTER the previous array had been filled.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default Re: Can't draw a BufferImage array.

    im drawing buffered images in an array here: Animated

Similar Threads

  1. how can I draw remaning cards in the array deck.
    By ganesh.gothi@gmail.com in forum Java Applets
    Replies: 6
    Last Post: 04-20-2012, 02:05 PM
  2. how can I draw a board in 2d array?
    By java_fun2007 in forum New To Java
    Replies: 8
    Last Post: 12-28-2011, 05:58 PM
  3. Replies: 1
    Last Post: 11-08-2011, 05:04 AM
  4. access array list item from draw
    By trishtren in forum New To Java
    Replies: 8
    Last Post: 04-19-2011, 12:34 PM
  5. Replies: 5
    Last Post: 08-15-2009, 05:57 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
  •