The error occured when I typed private SpriteSheet spriteSheet = new SpriteSheet("/sprite_sheet.png");
When I commented out that part, the program worked fine.
Any ideas? My pattern is 8-bit and the spritesheet is 32x32
Game.java:
package ca.swimmerwoad.adventuregame;
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;
import ca.swimmerwoad.adventuregame.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 SpriteSheet spriteSheet = new SpriteSheet("/sprite_sheet.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);
}
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 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 = true;
while (delta >= 1) {
ticks++;
tick();
render();
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(shouldRender) {
frames++;
render();
if (System.currentTimeMillis() - lastTime > 1000) {
lastTimer +=1000;
System.out.println(frames + "," + ticks);
frames = 0;
ticks = 0;
}
}
}
}
public void tick() {
tickCount++;
for (int i=0;i < pixels.length; i++) {
pixels[i] = i + tickCount;
}
}
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();
}
public static void main(String[] args) {
new Game().start(); }
}
SpriteSheet.java
package ca.swimmerwoad.adventuregame.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 my error is:
Exception in thread "main" java.lang.NullPointerException
at sun.misc.MetaIndex.mayContain(Unknown Source)
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at java.lang.ClassLoader.getBootstrapResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.net.URLClassLoader.getResourceAsStream(Unknown Source)
at java.lang.Class.getResourceAsStream(Unknown Source)
at ca.swimmerwoad.adventuregame.gfx.SpriteSheet.<init>(SpriteSheet.java:20)
at ca.swimmerwoad.adventuregame.Game.<init>(Game.java:33)
at ca.swimmerwo
you have written
public SpriteSheet(String Path)
i think it should be
public SpriteSheet(String path)
otherwise the path from which you want to create the sprite,i.e the path variable is null, and hence the exception
the NPE is from this line
SpriteSheet.class.getResourceAsStream(path)
you have only caught IOException here
try {
image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
}
Related
I am trying to add the image sprite_sheet.png to my game project but I get these errors:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at az.rehman.gfx.SpriteSheet.<init>(SpriteSheet.java:16)
at az.rehman.game.Game.<init>(Game.java:29)
at az.rehman.game.Game.main(Game.java:127)
Here are my classes:
package az.rehman.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.getResource(path));
} catch (IOException e) {
System.out.println("SpriteSheet file exception");
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]);
}
} // constructer end
} // class end
package az.rehman.game;
import az.rehman.gfx.SpriteSheet;
import javax.swing.*;
import java.awt.*;
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 {
public 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 SpriteSheet spriteSheet = new SpriteSheet("\\sprite_sheet.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);
}
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 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 = 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++;
for (int i = 0; i < pixels.length; i++) {
pixels[i] = i + 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.dispose();
bs.show();
}
public static void main(String[] args) {
new Game().start();
}
}
So I have been trying to follow a tutorial
But it isn't working. It is a tutorial on making a sprite sheet with JFrame. I could get other things to work, but for some reason this won't. It is supposed to display a tiled image, but instead it displays a black screen.
Game.java
package ca.colescheler.game;
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;
import ca.colescheler.game.gfx.Screen;
import ca.colescheler.game.gfx.SpriteSheet;
#SuppressWarnings("unused")
public class Game extends Canvas implements Runnable {
/**
*
*/
private static final long serialVersionUID = 9086760045199246082L;
private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
public boolean running;
private static final long serialVesionUID = 1;
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;
int tickCount=1;
private SpriteSheet spriteSheet = new SpriteSheet("/sprite_sheet.png");
private Screen screen;
public static long getSerialvesionuid() {
return serialVesionUID;
}
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() {
screen = new Screen(WIDTH,HEIGHT,new SpriteSheet("/sprite_sheet.png"));
}
public void tick() {
tickCount++;
}
long now = System.nanoTime();
public synchronized void stop() {
// TODO Auto-generated method stub
}
public synchronized void start() {
// TODO Auto-generated method stub
//new Thread(this).start();
running = true;
run();
frames = 1;
ticks = 1;
}
static int frames = 0;
static int ticks = 0;
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000d/60d;
long lastTimer = System.currentTimeMillis();
double delta=0;
while (running) {
init();
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = false;
while (delta >= 1) {
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
if (shouldRender) {
render();
}
if (System.currentTimeMillis() - lastTimer > 1000) {
lastTimer += 1000;
System.out.println(frames + ", " + ticks);
frames=0;
ticks=0;
}
}
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
screen.render(pixels, 0, WIDTH);
Graphics g = bs.getDrawGraphics();
//g.drawRect(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();
}
}
SpriteSheet.java
package ca.colescheler.game.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) {
// TODO Auto-generated catch block
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);
//AARRGGBB
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]);
}
//85*0
}
}
Screen.java
package ca.colescheler.game.gfx;
public class Screen {
public static final int MAP_WIDTH = 64;
public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;
public int[] tiles = new int[MAP_WIDTH * MAP_WIDTH];
public int[] colours = new int[MAP_WIDTH * MAP_WIDTH * 4];
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;
for (int i=1;i<MAP_WIDTH*MAP_WIDTH;i++) {
colours[i*4+0] = 0xff00ff;
colours[i*4+1] = 0x00ffff;
colours[i*4+2] = 0xffff00;
colours[i*4+3] = 0xffffff;
}
}
public void render(int[] pixels, int offset, int row) {
for (int yTile = yOffset>>3;yTile <-(yOffset + height) >>3; yTile++) {
int yMin = yTile * 8 - yOffset;
int yMax = yMin + 8;
if (yMin < 0) yMin = 0;
if (yMax > height) yMax = height;
for (int xTile = xOffset >>3; xTile <=(xOffset+width) >>3; xTile++) {
int xMin = xTile * 8 - xOffset;
int xMax = xMin + 8;
if (xMin < 0) xMin = 0;
if (xMax > width) xMax = height;
int tileIndex = (xTile &(MAP_WIDTH_MASK)) + (yTile & (MAP_WIDTH_MASK)) * MAP_WIDTH;
for (int y = yMin;y<yMax;y++) {
int sheetPixel = ((y+yOffset) & 7) * sheet.width + ((xMin + xOffset) & 7);
int tilePixel = offset + xMin + y * row;
for (int x=xMin; x<xMax;x++) {
int colour = tileIndex * 4 + sheet.pixels[sheetPixel++];
pixels[tilePixel++]=colours[colour];
}
}
}
}
}}
Link to tutorial
In the file Screen.java, there is a typo. You should change height to width in the following line:
if (xMax > width) xMax = height;
Me and a partner have been creating a game for our class. And while we are able to make shapes, we are unable load the sprite. I'm not sure all this code is necessary but it seemed like the necessary classes, and any other help with our code here would be most appreciated.
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class BoardGraphics extends JPanel{
public BoardGraphics(){
initGraphics();
}
//Image Url's
String grassTS = "src/Resources/Sprites/GrassTile_1.png";
//Image objects
BufferedImage grassTI;
public void layTiles(Graphics g){
for(int i = 0; i < 21; i++){
for(int k = 0; k < 21; k++){
}
}
}
public void loadImage(String url, Image image){
try {
image = ImageIO.read(new File(url));
if(image != null){
System.out.println(url + ", has been loaded!");
}
}catch (IOException e){
e.printStackTrace();
}
}
public void initGraphics(){
setBackground(Color.WHITE);
setDoubleBuffered(true);
loadImage(grassTS, grassTI);
}
public void drawPlayer(Graphics g){
g.fillOval(5,5,32,32);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawImage(grassTI,21,21,null);
drawPlayer(g);
layTiles(g);
}
}
My partner has done most of this code, and I know very little about graphical programming in java. Also i'm trying to split these two classes apart.
import java.nio.file.Paths;
import javafx.embed.swing.*;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.Media;
import javax.swing.*;
import java.awt.*;
public class Game extends Canvas implements Runnable {
boolean running = true;
public final int Wid = 480;
public final int Hei = 480;
public final int scale = 3;
public final String name = "Hasty Harvester";
private JFrame frame;
JFXPanel in = new JFXPanel();
public Board b;
public KeyIn input;
public BoardGraphics g = new BoardGraphics();
public boolean start = true;
public Game(){
init();
frame = new JFrame(name);
frame.setPreferredSize(new Dimension(Wid, Hei));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
input = new KeyIn(in);
frame.add(in);
frame.add(g);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void run(){
long lastTime = System.nanoTime();
double ns = 100000000.0 / 60.0;
int frames = 0;
int ticks = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
Farm farmville = new Farm("src/Resources/Levels/levels.txt");
b = farmville.getMap(0);
music("src/Resources/Music/CasualGameTrack_Alexandr_Zhelanov_Ingame.mp3");
while(running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
boolean shouldrender = false;
while (delta >= 1) {
ticks++;
tick(b);
delta--;
shouldrender = true;
}
if(shouldrender) {
frames++;
render();
}
//Per second record the amount of frames + ticks, and reset them
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer +=1000;
System.out.println("Frames: " + frames + ", Ticks:" + ticks);
frames = 0;
ticks = 0;
}
}
}
public void init(){
in.setFocusable(true);
}
public void music(String mus){
Media sung = new Media(Paths.get(mus).toUri().toString());
MediaPlayer med = new MediaPlayer(sung);
med.play();
}
public void tick(Board b){
if(input.up.isPressed() == true){
System.out.println("Up");
input.up.toggle(false);
b.movePlayer("Up");
}
if(input.down.isPressed() == true){
System.out.println("down");
input.down.toggle(false);
b.movePlayer("Down");
}
if(input.right.isPressed() == true){
System.out.println("right");
input.right.toggle(false);
b.movePlayer("Right");
}
if(input.left.isPressed() == true){
System.out.println("left");
input.left.toggle(false);
b.movePlayer("Left");
}
}
public void render(){
}
public void start(){
new Thread(this).start();
running = true;
}
public void stop(){
running = false;
}
public static void main(String[] args){
new Game().start();
}
}
I found the issue was in the loadImage method as it was loading the image into a new image object.
I have linked the file I want to load and when I debug, the SpriteSheet constructor shows that the path variable is storing the path I specify. When it trying to run the image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path)); line of code, it crashes with an Illegal Argument Exception. The only thing I can think is wrong is that the file I specified will not load, but I have no idea why.
Game class:
package com.swainchris.twodgame;
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;
import com.swainchris.twodgame.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 = "2D Game";
public static boolean running = false;
private JFrame frame;
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("/SS.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);
}
public synchronized void start() {
running = true;
new Thread(this).start();
}
public synchronized void stop() {
running = false;
}
public void tick(){
tickCount++;
for(int i = 0; i < pixels.length; i++){
pixels[i] = i - 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.setColor(Color.BLACK);
g.fillOval(50,50,50,50);
g.dispose();
bs.show();
}
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 = true;
while(delta >= 1){
ticks++;
tick();
delta--;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(shouldRender){
frames++;
render();
}
if(System.currentTimeMillis() - lastTimer >= 1000){
lastTimer += 1000;
frame.setTitle("2D Game! FPS: " + frames + " UPS: " + ticks);
frames = 0;
ticks = 0;
}
}
}
public static void main(String[] args) {
new Game().start();
}
}
SpriteSheet class:
package com.swainchris.twodgame.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]);
}
}
}
Error:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.swainchris.twodgame.gfx.SpriteSheet.<init>(SpriteSheet.java:20)
at com.swainchris.twodgame.Game.<init>(Game.java:30)
at com.swainchris.twodgame.Game.main(Game.java:137)
Works now, all i did was copy all the code to a new project file. Didn't change anything at all besides the name of the project. No idea why this worked but it did.
I have been watching tutorials on how to make a 3D game in Java using Eclipse. I have copied all the code word for word and am not getting the same result and it is very frustrating. At the moment all I am trying to do is create a small square of randomly generated pixels and all I'm getting is a blank window. This is the code and classes I am using.
Class1 = Display
package com.mime.testgame2;
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.mime.testgame2.graphics.Render;
import com.mime.testgame2.graphics.Screen;
public class Display extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int width = 800;
public static final int height = 600;
public static String title = "3D Game Pre-Alpha 0.0.1";
private Thread thread;
private boolean running = false;
private Render render;
private Screen screen;
private BufferedImage img;
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();
}
private 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.add(game);
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setTitle(title);
}
}
Class2 = Render
package com.mime.testgame2.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 y = 0; y < render.Height; y++) {
int yPix = y + yOffset;
for(int x = 0; x < render.Width; x++) {
int xPix = x + xOffset;
pixels[xPix + yPix * Width] = render.pixels[x + y * rend er.Width];
}
}
}
}
Class3 = Screen
package com.mime.testgame2.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);
}
}
Can anyone help me?