Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Sprite Sheet help

timer setpreferredsize

  • Please log in to reply
13 replies to this topic

#1 floorman

floorman

    CC Regular

  • Member
  • PipPipPip
  • 29 posts

Posted 18 February 2012 - 05:02 PM

icons.png Hi,

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

  • 0

#2 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 19 February 2012 - 05:36 AM

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.
  • 0

#3 floorman

floorman

    CC Regular

  • Member
  • PipPipPip
  • 29 posts

Posted 19 February 2012 - 08:58 AM

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

#4 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 19 February 2012 - 09:02 AM

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.
  • 0

#5 floorman

floorman

    CC Regular

  • Member
  • PipPipPip
  • 29 posts

Posted 19 February 2012 - 10:57 AM

The code is current. and render() is drawing to the screen. init() gets the image.
  • 0

#6 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 19 February 2012 - 11:01 AM

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

#7 floorman

floorman

    CC Regular

  • Member
  • PipPipPip
  • 29 posts

Posted 19 February 2012 - 11:58 AM

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).
  • 0

#8 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 19 February 2012 - 12:05 PM

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.
  • 0

#9 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 20 February 2012 - 04:04 PM

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

  • 0

#10 floorman

floorman

    CC Regular

  • Member
  • PipPipPip
  • 29 posts

Posted 21 February 2012 - 09:48 AM

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

#11 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 21 February 2012 - 10:07 AM

Can you post your working code in case anyone else has this problem?
  • 0

#12 floorman

floorman

    CC Regular

  • Member
  • PipPipPip
  • 29 posts

Posted 21 February 2012 - 02:01 PM

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.
  • 0





Also tagged with one or more of these keywords: timer, setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download