NullPointerException in my game, can't be fixed - java

I'm making an rpg with a custom pixel engine, and when I try to run it I just get a NullPointerException and I know why, but when I try to initialize that variable I get another one in a different location and I fix that and now it won't run at all and I still get a NullPointerException. This is the class that gets an error, and the console tells me that the error is on the line that says screen.render();
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 java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import sprites.SpriteSheetLoader;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final int HEIGHT = 120;
public static final int WIDTH = 120;
public static final int SCALE = 3;
public static final String NAME = "TypicalRPG";
public SpriteSheetLoader loader;
private BufferedImage img = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
private boolean running = false;
private Screen screen = new Screen(WIDTH, HEIGHT, loader);
Random random = new Random();
public void start(){
running = true;
new Thread(this).start();
}
public static void main(String args[]){
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
JFrame jf = new JFrame(NAME);
jf.add(game);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
jf.setResizable(true);
game.start();
}
public void run(){
while(running){
tick();
render();
try{
Thread.sleep(5);
}
catch(InterruptedException e){ e.printStackTrace(); }
}
}
public void stop(){ running = false; }
public void tick(){
screen.render(0, 0, 0, 16, 16);
}
public void render(){
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
requestFocus();
return;
}
for(int i = 0; i < screen.pixels.length; i++){
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
public void init(){
BufferedImage sheet = null;
try {
sheet = ImageIO.read(Game.class.getResourceAsStream("res/tiles.png"));
}
catch (IOException e) {
e.printStackTrace();
}
loader = new SpriteSheetLoader(sheet);
screen = new Screen(WIDTH, HEIGHT, loader);
}
}
and this is the class that has render():
import sprites.SpriteSheetLoader;
public class Screen{
public int[] pixels;
private SpriteSheetLoader loader;
private int w, h;
int xoff = 0, yoff = 0;
public Screen(int w, int h, SpriteSheetLoader loader){
this.loader = loader;
this.w = w;
this.h = h;
pixels = new int[w * h];
}
public void render(int xpos, int ypos, int tile, int width, int height){
loader.grabTile(tile, width, height);
xpos -= xoff;
ypos -= yoff;
for(int y = 0; y < height; y++){
if(ypos + y < 0 || ypos + y >= h) continue;
for(int x = 0; x < width; x++){
if(xpos + x < 0 || xpos + x >= w) continue;
int col = loader.pixels[x + (y * height)];
if(col != -65281) pixels[(x + xpos) + (y + ypos)] = col;
}
}
}
}

Seems like you never initiate the loader in your first class.
You define your variables like this:
public SpriteSheetLoader loader;
private Screen screen = new Screen(WIDTH, HEIGHT, loader);
Hence, you pass a null-object to your Screen. And from there you try to call a method on that null-object:
loader.grabTile(tile, width, height);

You have only declared loader, but never initiated it.
public SpriteSheetLoader loader;

Related

Java 3D Engine Pixel Drawing Problems

my 3 programs seem to work alone but when I try to run them together It seems to give me an out of bounds error but I cant tell where exactly if anyone can tell me where exactly it is and how to fix the error that would be great, I wanted to use a debugger but im having some problems getting it to work.
Here is my Code for the 3 Classes :
Render.java
package com.mime.Game.graphics;
public class Render {
public final int width;
public final int height;
public final int[] pixels;
public Render(int width, int height) {
this.width = width;
this.height = height;
pixels = new int[width * height];
}
public void draw(Render render, int xOffset, int yOffset) {
for(int x = 0; x < this.height; x++) {
int yPix = x + yOffset;
for(int y = 0; y < this.width; y++) {
int xPix = y + xOffset;
pixels[xPix + yPix * width] = render.pixels[x + y *
render.width];
}
}
}
}
Display.java
package com.mime.Game;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import com.mime.Game.graphics.Render;
import com.mime.Game.graphics.Screen;
import java.awt.image.DataBufferInt;
public class Display extends Canvas implements Runnable{
public static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
private Thread thread;
private Screen screen;
private BufferedImage img;
private boolean running = false;
private Render render;
private int[] pixels;
public Display() {
screen = new Screen(WIDTH, HEIGHT);
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
}
private void start() {
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
System.out.println("Working");
}
public void stop() {
if(!running)
return;
running = false;
try{
thread.join();
}catch(Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public void run() {
while(running) {
tick();
render();
}
}
private void tick() {
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
screen.render();
for(int i = 0; i < WIDTH * HEIGHT; i++) {
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
g.dispose();
bs.show();
}
public static void main(String[] args) {
Display game = new Display();
JFrame frame = new JFrame();
frame.setResizable(false);
frame.setSize(WIDTH, HEIGHT);
frame.add(game);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Chronicles of Walshy Alpha 0.01");
game.start();
}
}
screen.java :
package com.mime.Game.graphics;
import java.util.Random;
public class Screen extends Render{
private Render test;
public Screen(int width, int height) {
super(width, height);
Random random = new Random();
test = new Render(256, 256);
for(int i = 0; i < 256*256; i++) {
test.pixels[i] = random.nextInt();
}
}
public void render() {
draw(test, 0, 0);
}
}
In your Render.draw method you copy pixels from another Render object and you use this.width and this.height to access those pixels. This will fail when the other Render object's pixel size is smaller, which is the case in your code. You get an ArrayOutOfBoundsException when reading from render.pixels[x + y * render.width] because you're trying to read pixels from a 256*256 array as if it was a 800*600 array.
Additionally if you ever call draw with offset values, you will get another ArrayOutOfBoundsException, because you're simply adding the offsets to your x and y values without checking if the resulting index is too big when writing to pixels[xPix + yPix * width].
To fix this, you have to skip any value that goes beyond the width or height of either pixels array:
if (xPix > this.width || yPix > this.height || x > render.width || y > render.height)
{
continue;
}

How do I solve this array out of bounds exception? [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
Hello fellow hydrogen based life form. I learning how to make 3-Dimensional gaems from a top secret website (www.youtube.com) and I was learning from a very nice Youtuber. My project looks like this:
There are 3 classes: Main, Screen and Render
I get this exception :( :
Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 65600
at Render.Draw(Render.java:20)
at Screen.render(Screen.java:19)
at Main.render(Main.java:74)
at Main.run(Main.java:59)
at java.lang.Thread.run(Unknown Source)
I shall upload my code here too below this. Please help me become good at this.
Main class:
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
public class Main extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Nexus Overload";
private Thread t;
private boolean Running = false;
#SuppressWarnings("unused")
private Render ren;
private Screen s;
private BufferedImage img;
private BufferStrategy bs;
private int[] pixels;
public Main() {
s = new Screen(WIDTH, HEIGHT);
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
}
private void start() {
if (Running)
return;
Running = true;
t = new Thread(this);
t.start();
}
#SuppressWarnings("unused")
private void stop() {
if (!Running)
return;
Running = false;
try {
t.join();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public void run() {
while (Running) {
tick();
render();
}
}
private void tick() {
}
private void render() {
bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
s.render();
for (int i = 0; i < WIDTH * HEIGHT; i++) {
pixels[i] = s.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(img, WIDTH, HEIGHT, null);
g.dispose();
bs.show();
}
public static void main(String[] args) {
Main m = new Main();
JFrame frame = new JFrame();
frame.getContentPane().add(m);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(350, 100, WIDTH, HEIGHT);
frame.setTitle(TITLE);
frame.setVisible(true);
m.start();
}
}
Render class:
public class Render {
public final int width;
public final int height;
public final int[] pixels;
public Render(int width, int height) {
this.width = width;
this.height = height;
this.pixels = new int[width * height];
}
public void Draw (Render ren, int xOffset, int yOffset) {
for (int y = 0; y < ren.height; y++) {
int yPix = y + yOffset;
for (int x = 0; x < ren.width; x++) {
int xPix = x + xOffset;
pixels[xPix + yPix * width] = ren.pixels[xPix + yPix * width];
}
}
}
}
Screen class:
import java.util.Random;
public class Screen extends Render{
private Render ren;
public Screen(int width, int height) {
super(width, height);
Random r = new Random();
ren = new Render(256, 256);
for (int i = 0; i < 256 * 256; i++) {
ren.pixels[i] = r.nextInt();
}
}
public void render() {
Draw(ren, 0, 0);
}
}
For those who want image with eclipse debugging, here:
With variables tab selected:
Your exception is pointing us to the Draw method in the Render class (NB: Java conventions state that method names should be lower case so this method should actually be called draw(Render ren, int xOffset, int yOffset). I would first try and set the int y in the outer for loop to 1 to see if that helps, you may also been to do the same for int x in the inner for loop...

Minecraft clone on java eclipse error

I watched a Youtube video of someone making a MineCraft clone, which compiled and ran without error. But when I copied it completely, I got an error.
The error is in the Render class: int pixels = new int(width * height);
Render:
package com.mads.minecraft.graphics;
public class Render {
public final int width;
public final int height;
public int[] pixels;
public Render(int width, int height){
this.width = width;
this.height = height;
int pixels = new int(width * height);
}
public void draw(Render render, int xOffset, int yOffset){
for (int y = 0; y < render.height; y++){
int yPix = y + yOffset;
if(yPix < 0 || yPix >= height) continue;
for (int x = 0; y < render.width; x++){
int xPix = x + xOffset;
if(xPix < 0 || xPix >= width) continue;
pixels[xPix + yPix + width] = render.pixels[x + y * render.width];
}
}
}
}
Display:
package com.mads.minecraft;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import com.mads.minecraft.graphics.Screen;
public class Display extends Canvas implements Runnable{
static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Minecraft Alpha";
private Thread thread;
private boolean running = false;
private BufferedImage img;
private Screen screen;
private int[] pixels;
public Display(){
screen = new Screen(WIDTH, HEIGHT);
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
}
public void start(){
if(running) return;
running = true;
thread = new Thread(this);
thread.start();
}
public void stop(){
if(!running) return;
running = false;
try {
thread.join();
} catch (Exception e){
e.printStackTrace();
System.exit(0);
}
}
public void run(){
while(running){
tick();
render();
}
}
public void tick(){}
public void render(){
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
screen.render();
for (int i = 0; i < WIDTH * HEIGHT; i++){
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(img, WIDTH, HEIGHT, null);
g.dispose();
bs.show();
}
public static void main(String []args){
Display game = new Display();
JFrame frame = new JFrame();
frame.add(game);
frame.pack();
frame.setTitle(TITLE);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
game.start();
}
}
Screen:
package com.mads.minecraft.graphics;
import java.util.Random;
public class Screen extends Render{
private Render test;
public Screen(int width, int height) {
super(width, height);
Random random = new Random();
test = new Render(256, 256);
for(int i = 0; i < 256 * 256; i++) {
test.pixels[i] = random.nextInt();
}
}
public void render(){
draw(test, 0, 0);
}
}
Following is an invalid statement which you need to replace:
int pixels = new int(width * height); (int is a primitive data type not reference type, so you can not create an object)
by
pixels = new int[width * height]; (You can create an array of int. You have already declared the variable pixels at the instance level as int[], so no need to declare again)
Hope, this helps.
Apparently you're trying to initialize an array in Render. All you have to do is to replace int pixels = new int(width * height) in Renders constructor with this:
pixels = new int[width * height];

I am creating a 2d game, i am trying to import some fonts but i keep getting the error

package aaron.game;
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.awt.*;
import javax.swing.JFrame;
import aaron.game.gfx.Colours;
import aaron.game.gfx.Font;
import aaron.game.gfx.Screen;
import aaron.game.gfx.SpriteSheet;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 160;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 3;
public static final String NAME = "Game";
private JFrame frame;
public boolean running = false;
public int tickCount = 0;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
private int[] colours = new int[6*6*6];
private Screen screen;
public InputHandler input;
public Game(){
setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void init(){
int index = 0;
for(int r = 0;r<6;r++){
for(int g = 0;g<6;g++){
for(int b = 0;b<6;b++){
int rr= (r*255/5);
int gg= (g*255/5);
int bb= (b*255/5);
colours[index++] = rr<<16| gg<<8 | bb;
}
}
}
screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
input = new InputHandler(this);
}
public synchronized void start() {
running = true;
new Thread(this).start();
}
public synchronized void stop() {
running = false;
}
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D/60D;
int frames = 0;
int ticks = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
init();
while (running){
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
while(delta >= 1){
ticks++;
tick();
delta -= 1;
shouldRender = true;}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (shouldRender){
frames++;
render();
}
if(System.currentTimeMillis() - lastTimer >= 1000){
lastTimer += 1000;
System.out.println(ticks + " ticks, " + frames + " frames");
frames = 0;
ticks = 0;
}
}
}
public void tick(){
tickCount++;
if(input.up.isPressed()){screen.yOffset--;}
if(input.down.isPressed()){screen.yOffset++;}
if(input.left.isPressed()){screen.xOffset--;}
if(input.right.isPressed()){screen.xOffset++;}
for (int i = 0; i < pixels.length; i++){
pixels[i] = i + tickCount;
}
}
public void render(){
BufferStrategy bs = getBufferStrategy();
if (bs == null){
createBufferStrategy(3);
return;
}
for(int y=0;y<32;y++){
for(int x=0;x<32;x++){
boolean flipX = x%2==1;
boolean flipY = y%2==1;
screen.render(x<<3, y<<3, 0, Colours.get(555,505,055,550), flipX, flipY);
}
}
// Font.render("Hello Wolrd! 0157", screen, 0, 0, Colours.get(000, -1, -1, 555));
for(int y=0;y<screen.height;y++){
for(int x=0;x<screen.width;x++){
int colourCode=screen.pixels[x+y*screen.width];
if(colourCode<255)pixels[x+y*WIDTH]=colours[colourCode];
}
}
Graphics g = bs.getDrawGraphics();
//g.setColor(Color.BLACK);
//g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
public static void main(String[] args){
new Game().start();
}
}
^This is my main class [ // Font.render("Hello Wolrd! 0157", screen, 0, 0, Colours.get(000, -1, -1, 555));] < this is where i am getting the error...
package aaron.game.gfx;
public class Font {
private static String chars = "" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ " + "0123456789.,:;'\"!?$%()-=+/ ";
public static void render(String msg, Screen screen, int x, int y, int colour, int scale) {
msg = msg.toUpperCase();
for (int i = 0; i < msg.length(); i++) {
int charIndex = chars.indexOf(msg.charAt(i));
if (charIndex >= 0) screen.render(x + (i * 8), y, charIndex + 30 * 32, colour, false, false);
}
}
}
This is the class for reading my fonts
package aaron.game.gfx;
public class Screen {
public static final int MAP_WIDTH = 64;
public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;
public int[]pixels;
public int xOffset = 0;
public int yOffset = 0;
public int width;
public int height;
public SpriteSheet sheet;
public Screen(int width, int height, SpriteSheet sheet){
this.width = width;
this.height = height;
this.sheet = sheet;
pixels=new int[width * height];
}
public void render(int xPos, int yPos, int tile, int colour){
render(xPos, yPos, tile, colour, false, false);
}
public void render(int xPos, int yPos, int tile, int colour, boolean mirrorX, boolean mirrorY){
xPos -= xOffset;
yPos -= yOffset;
int xTile = tile %32;
int yTile = tile /32;
int tileOffset=(xTile<<3)+(yTile<<3)*sheet.width;
for(int y=0;y<8;y++){
if(y+yPos<0 || y+yPos>=height)continue;
int ySheet = y;
if(mirrorY) ySheet = 7 - y;
for(int x=0;x<8;x++){
if(x+xPos<0 || x+xPos>=width)continue;
int xSheet = x;
if(mirrorX) xSheet = 7 - x;
int col=(colour>>(sheet.pixels[xSheet+ySheet*sheet.width+tileOffset]*8))&255;
if(col<255) pixels[(x+xPos)+(y+yPos)*width]=col;
}
}
}
}
This is my display class, anyway this error is killing me and i hae no idea how to fix it whatsoever, I have looked all over the place bot a single place, the error i am getting is
The method render(String, Screen, int, int, int, int) in the type Font is not applicable for the arguments (String, Screen, int, int, int)
Try cleaning and rebuilding the project. It seems like an older version of the program is being run...

(Java) Getting error with 2D Game

I'm having an issue with my 2D game that I am following a tutorial for. I've scanned my code several times and cannot seem to resolve it. Sorry if my question sounds stupid, but I do welcome any help whatsoever.
Here's my source:
package game;
import gfx.SpriteSheet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable
{
private static final long serialVersionUD = 1L;
public static final int WIDTH = 160;
public static final int HEIGHT = WIDTH/12*9;
public static final int SCALE = 3;
public static final String NAME = "Game";
private JFrame frame;
public boolean running = false;
public int tickCount = 0;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
private SpriteSheet spriteSheet = new SpriteSheet("SpriteSheet.png");
public Game()
{
setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private synchronized void start()
{
running = true;
new Thread(this).start();
}
private synchronized void stop()
{
running = false;
}
public void run()
{
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / 60D;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
while(running)
{
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = false;
while(delta >= 1)
{
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException ex) {
}
if(shouldRender)
{
frames++;
render();
}
if(System.currentTimeMillis() - lastTimer >= 1000)
{
lastTimer += 1000;
System.out.println(frames + ", " + ticks);
frames = 0;
ticks = 0;
}
}
}
//Updates all of internal variables and logic and stuff.
public void tick()
{
tickCount++;
for(int i = 0; i < pixels.length; i++)
{
pixels [i] = i + tickCount;
}
}
//prints the updated logic.
public void render()
{
BufferStrategy bs = getBufferStrategy();
if(bs == null)
{
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
//Main method
public static void main(String[]args)
{
new Game().start();
}
}
package gfx;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpriteSheet
{
public String path;
public int width;
public int height;
public int[] pixels;
public SpriteSheet(String path)
{
BufferedImage image = null;
try
{
image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
} catch (IOException e)
{
e.printStackTrace();
}
if(image == null)
{
return;
}
this.path = path;
this.width = image.getWidth();
this.height = image.getHeight();
pixels = image.getRGB(0, 0, width, height, null, 0, width);
for(int i = 0; i < pixels.length; i++)
{
pixels[i] = (pixels[i] & 0xff / 64);
}
for(int i = 0; i < 8; i++)
{
System.out.println(pixels[i]);
}
}
}
And here's a link to my video tutorial I'm using: Tutorial
I'm using eclipse on a 64 bit operating system(Windows).
Again, any help would be greatly appreciated, and thank you.
EDIT:
Error message:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1348)
at gfx.SpriteSheet.<init>(SpriteSheet.java:21)
at game.Game.<init>(Game.java:34)
at game.Game.main(Game.java:144)
Sorry, forgot error message. XD
The error message is telling you what's wrong: Your image path is wrong. The image path must be given relative to the class files location.

Categories