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


Sign In
Create Account


Back to top









