Jump to content

Sprite Sheet help

- - - - -

  • Please log in to reply
13 replies to this topic

#1
floorman

floorman

    Newbie

  • Members
  • PipPip
  • 29 posts
Attached File  icons.png   1.42K   18 downloadsHi,

I'm trying to load a SubImage from a BufferedImage in Java and I've had no luck with loading the part of the image, I keep on getting a ton of NullPointerException but I can't figure out what is null and what I'm doing wrong.

I've removed some code because it was wasn't working, At the moment this will load a black screen, but no matter what other code I try to put in, I cannot load the first tile.

Here is the code:


package com.manick.test;


import java.awt.BorderLayout;

import java.awt.Canvas;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.image.BufferStrategy;

import java.awt.image.BufferedImage;

import java.awt.image.DataBufferInt;

import java.io.IOException;


import javax.imageio.ImageIO;

import javax.swing.JFrame;


import com.manick.test.graphics.Screen;

import com.manick.test.graphics.SpriteSheet;


public class Game extends Canvas implements Runnable {

	private static final long serialVersionUID = 1L;


	public static final int HEIGHT = 240;

	public static final int WIDTH = 300;

	public static final int SCALE = 3;


	private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

	private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

	private boolean running = false;

	private int tickCount;

	private Screen screen;

	private SpriteSheet sheet;

	private BufferedImage sprite;


	public void start() {

		running = true;

		new Thread(this).start();

	}


	public void stop() {

		running = false;

	}


	public void init() {

		try {

			screen = new Screen(WIDTH, HEIGHT, new SpriteSheet(ImageIO.read(Game.class.getResourceAsStream("/icons.png"))));

		} catch (IOException e) {

			e.printStackTrace();

		}


		sprite = sheet.getSprite(0, 0, 7, 7);

	}


	public void run() {

		long lastTime = System.nanoTime();

		double unprocessed = 0;

		double nsPerTick = 1000000000.0 / 60.0;

		int frames = 0;

		int ticks = 0;

		long lastTimer1 = System.currentTimeMillis();

		

		init();


		while (running) {

			long now = System.nanoTime();

			unprocessed += (now - lastTime) / nsPerTick;

			lastTime = now;

			boolean shouldRender = true;

			

			init();


			while (unprocessed > 1) {

				ticks++;

				tick();

				unprocessed -= 1;

				shouldRender = true;

			}


			try {

				Thread.sleep(2);

			} catch (InterruptedException e) {

				e.printStackTrace();

			}


			if (shouldRender) {

				frames++;

				render();

			}


			if (System.currentTimeMillis() - lastTimer1 > 1000) {

				lastTimer1 += 1000;

				System.out.println(ticks + " ticks, " + frames + " fps");

				frames = 0;

				ticks = 0;

			}

		}

	}


	public void tick() {

		tickCount++;

	}


	public void render() {

		BufferStrategy bs = getBufferStrategy();

		if (bs == null) {

			createBufferStrategy(3);

			return;

		}

		

		Graphics g = bs.getDrawGraphics();

		g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

		g.drawImage(sprite, WIDTH / 2, HEIGHT / 2, 15, 15, null);

		g.dispose();

		bs.show();

	}


	public static void main(String[] args) {

		Game game = new Game();

		game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

		game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

		game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));


		JFrame frame = new JFrame();

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.setLayout(new BorderLayout());

		frame.add(game);

		frame.pack();

		frame.setResizable(false);

		frame.setLocationRelativeTo(null);

		frame.setVisible(true);


		game.start();

	}

}

package com.manick.test.graphics;


public class Screen {

	int x, y, w, h;

	public SpriteSheet sheet;


	public Screen(int w, int h, SpriteSheet sheet) {

		this.w = w;

		this.h = h;

		this.sheet = sheet;

	}

}


package com.manick.test.graphics;


import java.awt.image.BufferedImage;


public class SpriteSheet {

	BufferedImage image;

	BufferedImage sub;

	

	public SpriteSheet(BufferedImage image) {

		this.image = image;

	}

	

	public BufferedImage getSprite(int x, int y, int w, int h) {

		BufferedImage sub = image.getSubimage(x, y, w, h);

		return sub;

	}

}


thanks.

Edited by floorman, 19 February 2012 - 11:57 AM.
Forgot the image...derp


#2
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts

Quote

I keep on getting a ton of NullPointerException but I can't figure out what is null
Look at the error message that is printed out. It should have the source line number.
Then look at that source code to see what variables are used on it.
If you can not see which variable is null, add a println statement just before the statement where the exception occurs and print out the values of all the variables used on that line to see which one is null.

Otherwise post the full text of the error messages here.

#3
floorman

floorman

    Newbie

  • Members
  • PipPip
  • 29 posts
Well, I've been playing around a bit and the error is gone but it is still not displaying the image.

#4
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Can you post the current version of the code?

What method is drawing to the screen? I don't see where you override the paint method.

#5
floorman

floorman

    Newbie

  • Members
  • PipPip
  • 29 posts
The code is current. and render() is drawing to the screen. init() gets the image.

#6
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Where do you call the init() method?
If you think it is being called, insert a println to show that it is.

#7
floorman

floorman

    Newbie

  • Members
  • PipPip
  • 29 posts
init() is being called in run, suddenly now, the Thread gets suspended (it only happens whenever that method is being called inside the run() method). And the code has been updated (sorry, forgot about that).

#8
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts

Quote

the Thread gets suspended
Please explain. Post the full text of the error message.

Try debugging the code by drawing some shapes on the screen to see if they will show.

#9
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
You never initialize sheet.

public void init() {

		try {

			sheet = new SpriteSheet(ImageIO.read(new File("icons.png")));

			screen = new Screen(WIDTH, HEIGHT, sheet);

		} catch (IOException e) {

			e.printStackTrace();

		}


		sprite = sheet.getSprite(0, 0, 7, 7);

	}


#10
floorman

floorman

    Newbie

  • Members
  • PipPip
  • 29 posts
I've been playing around with the code and I've managed to resolve it myself. Thanks all for the help anyway! :)

#11
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Can you post your working code in case anyone else has this problem?

#12
floorman

floorman

    Newbie

  • Members
  • PipPip
  • 29 posts
I could provide a link to the video I watched which helped me? I'd just rather not give out my source unless really necessary.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users