I have a code that doesnt work.. im still trying to finish it but when i created
ID: 3566586 • Letter: I
Question
I have a code that doesnt work.. im still trying to finish it but when i created the bullet .. it doesnt work.
Java, in eclipse .. it shows me the error
Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.game.src.main.BufferImageLoader.loadImage(BufferImageLoader.java:13)
at com.game.src.main.Game.init(Game.java:40)
at com.game.src.main.Game.run(Game.java:75)
at java.lang.Thread.run(Unknown Source)
-----------------------------------------------------------------------------------------------------------------------------------
package com.game.src.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 320;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 2;
public final String TITLE = "JAVA SPACE INVADERS";
private boolean running = false;
private Thread thread;
//instead of displaying image from the start, it will load image
// Buffer whole window, that is why it needs dimension, and use red green blue
private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_BGR);
private BufferedImage spriteSheet = null;
private BufferedImage bulletPic = null;
private BufferedImage background = null;
private Player p;
private Controller c;
public void init(){
requestFocus(); // dont have to press window screen
BufferImageLoader loader = new BufferImageLoader();
try {
spriteSheet = loader.loadImage("/sprite_sheet.png"); // spacecraft image
bulletPic = loader.loadImage("bulletpic.png");
background =loader.loadImage("/background.png"); // background image
}
catch(IOException e) {
e.printStackTrace();
}
//we need to add before the player in order to work
addKeyListener(new KeyInput(this));
p = new Player(200, 200, this); // reffering to game
c = new Controller(this);
}
private synchronized void start() {
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop(){
if(!running)
return;
running = false;
try {
thread.join(); // joins all thread together
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(1);
}
public void run() { // GAME LOOP
init();
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1) {
tick();
updates++;
delta --;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000; // dont want to loop again
System.out.println(updates + "Ticks, Fps " + frames);
updates = 0;
frames = 0;
}
}
stop();
}
private void tick() { // everything that updates
p.tick();
c.tick();
}
private void render() { // everything that renders
//it return null if it hasn't created it yet
//Initialized once, image loading up, and once it loaded it will show to monitor
BufferStrategy bs = this.getBufferStrategy(); // taking from canvas
if(bs == null){
createBufferStrategy(3); // 3 buffers, loading two image, which will increase speed display
return;
}
Graphics g = bs.getDrawGraphics(); // Draws out Buffers
// anything between these --->
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
g.setColor(Color.black); //color of background
g.fillRect(0, 0, 800, 800);
g.drawImage(background, 0, 0, null);
p.render(g);
c.render(g);
// this we can draw <----
g.dispose();
bs.show();
}
public void keyPressed(KeyEvent e){ // didnt work before because we capitalized the K
int key = e.getKeyCode(); //need to get the x and set the x, since its private
if(key == KeyEvent.VK_RIGHT){
p.setVelX(5);
}
else if(key == KeyEvent.VK_LEFT){
p.setVelX(-5);
}
else if(key == KeyEvent.VK_DOWN){
p.setVelY(5);
}
else if(key == KeyEvent.VK_UP){
p.setVelY(-5);
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_RIGHT){
p.setVelX(0); // comes from player class
}
else if(key == KeyEvent.VK_LEFT){
p.setVelX(0);
}
else if(key == KeyEvent.VK_DOWN){
p.setVelY(0);
}
else if(key == KeyEvent.VK_UP){
p.setVelY(0);
}
else if(key == KeyEvent.VK_SPACE){
c.addBullet(new Bullet(p.getX(), p.getY(), this)); // comes from controller class
} // with player class
}
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 frame = new JFrame(game.TITLE);
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
public BufferedImage getSpriteSheet(){
return spriteSheet;
}
public BufferedImage getBulletPic(){
return bulletPic;
}
}
-----------------------------------------------------------------------------
package com.game.src.main;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class BufferImageLoader {
private BufferedImage image;
public BufferedImage loadImage(String path) throws IOException{
image = ImageIO.read(getClass().getResource(path));
return image;
}
---------------------------------------------------------------------------------------------
package com.game.src.main;
import java.awt.image.BufferedImage;
public class SpriteSheet {
private BufferedImage image;
public SpriteSheet(BufferedImage image){
this.image = image;
}
public BufferedImage grabImage(int col, int row, int width, int height){
BufferedImage img = image.getSubimage((col * 32) - 32, (row * 32) - 32, width, height);
return img;
}
}
---------------------------------------------------------------------------------------------
package com.game.src.main;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Player {
private double x; //coordinate of player
private double y;
//make key movement smoother
private double velX = 0;
private double velY = 0;
private BufferedImage player;
public Player(double x, double y, Game game){ // set in parameter
this.x = x;
this.y = y;
SpriteSheet ss = new SpriteSheet(game.getSpriteSheet());
player = ss.grabImage(1, 1, 32, 32);
}
public void tick(){ // adding x and velX(setter)
x+=velX;
y+=velY;
//spacecraft is restricted
if(x <=0)
x = 0;
if(x>=640-19)
x=640-19;
if(y <0)
y = 0;
if(y >=480-32)
y=480-32;
}
public void render(Graphics g){
g.drawImage(player, (int)x, (int)y, null); //cast as int bc drawImage work for int not double
}// need to get and set private x and y
public double getX(){
return x;
}
public double getY(){
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void setVelX(double velX) {
this.velX = velX;
}
public void setVelY(double velY) {
this.velY = velY;
}
}
-------------------------------------------------------------------------------
package com.game.src.main;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class KeyInput extends KeyAdapter{
Game game;
public KeyInput(Game game){
this.game = game;
}
public void keyPressed(KeyEvent e){ // didnt work before because we capitalized the K
game.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
game.keyReleased(e);
}
}
-------------------------------------------------------------------------------------
package com.game.src.main;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Bullet {
private double x; // location of bullet
private double y; // lag and long
BufferedImage image;
public Bullet(double x, double y, Game game) {
this.x = x;
this.y = y;
SpriteSheet ss = new SpriteSheet(game.getBulletPic());
image = ss.grabImage(1, 1, 32, 32);
}
public void tick() { // clock
y -= 10; // speed of Bullet
}
public void render(Graphics g){
g.drawImage(image, (int) x, (int) y, null);
}
public double getY(){
return y;
}
}
---------------------------------------------------------------------------
package com.game.src.main;
import java.awt.Graphics;
import java.util.LinkedList;
public class Controller {
//hold a bunch of bullet objects
private LinkedList b = new LinkedList();
Bullet TempBullet;
Game game;
public Controller(Game game){
this.game = game;
}
public void tick() {
for(int i=0; i < b.size(); i++){
TempBullet = b.get(i);
if(TempBullet.getY() < 0)
removeBullet(TempBullet);
TempBullet.tick();
}
}
public void render(Graphics g) {
for(int i=0; i < b.size(); i++){
TempBullet = b.get(i);
TempBullet.render(g);
}
}
public void addBullet(Bullet block){
b.add(block);
}
public void removeBullet(Bullet block){
b.remove(block);
}
}
Explanation / Answer
package com.game.src.main;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Bullet {
private double x; // location of bullet
private double y; // lag and long
BufferedImage image;
public Bullet(double x, double y, Game game) {
this.x = x;
this.y = y;
SpriteSheet ss = new SpriteSheet(game.getBulletPic());
image = ss.grabImage(1, 2, 32, 32);
}
public void tick() { // clock
y -= 10; // speed of Bullet
}
public void render(Graphics g){
g.drawImage(image, (int) x, (int) y, null);
}
public double getY(){
return y;
}
}
package com.game.src.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 320;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 2;
public final String TITLE = "JAVA SPACE INVADERS";
private boolean running = false;
private Thread thread;
//instead of displaying image from the start, it will load image
// Buffer whole window, that is why it needs dimension, and use red green blue
private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_BGR);
private BufferedImage spriteSheet = null;
private BufferedImage bulletPic = null;
private BufferedImage background = null;
private boolean shooting = false;
private Player p;
private Controller c;
public void init(){
requestFocus(); // dont have to press window screen
BufferImageLoader loader = new BufferImageLoader();
try {
spriteSheet = loader.loadImage("/sprite_sheet.png"); // spacecraft image
bulletPic = loader.loadImage("/bulletpic.png");
background =loader.loadImage("/background.png"); // background image
}
catch(IOException e) {
e.printStackTrace();
}
//we need to add before the player in order to work
addKeyListener(new KeyInput(this));
p = new Player(200, 200, this); // reffering to game
c = new Controller(this);
}
private synchronized void start() {
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop(){
if(!running)
return;
running = false;
try {
thread.join(); // joins all thread together
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(1);
}
public void run() { // GAME LOOP
init();
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1) {
tick();
updates++;
delta --;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000; // dont want to loop again
System.out.println(updates + "Ticks, Fps " + frames);
updates = 0;
frames = 0;
}
}
stop();
}
private void tick() { // everything that updates
p.tick();
c.tick();
}
private void render() { // everything that renders
//it return null if it hasn't created it yet
//Initialized once, image loading up, and once it loaded it will show to monitor
BufferStrategy bs = this.getBufferStrategy(); // taking from canvas
if(bs == null){
createBufferStrategy(3); // 3 buffers, loading two image, which will increase speed display
return;
}
Graphics g = bs.getDrawGraphics(); // Draws out Buffers
// anything between these --->
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
g.setColor(Color.black); //color of background
g.fillRect(0, 0, 800, 800);
g.drawImage(background, 0, 0, null);
p.render(g);
c.render(g);
// this we can draw <----
g.dispose();
bs.show();
}
public void keyPressed(KeyEvent e){ // didnt work before because we capitalized the K
int key = e.getKeyCode(); //need to get the x and set the x, since its private
if(key == KeyEvent.VK_RIGHT){
p.setVelX(5);
}
else if(key == KeyEvent.VK_LEFT){
p.setVelX(-5);
}
else if(key == KeyEvent.VK_DOWN){
p.setVelY(5);
}
else if(key == KeyEvent.VK_UP){
p.setVelY(-5);
}
if(key == KeyEvent.VK_SPACE && !shooting){
shooting = true;
c.addBullet(new Bullet(p.getX(), p.getY(),this));
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_RIGHT){
p.setVelX(0); // comes from player class
}
else if(key == KeyEvent.VK_LEFT){
p.setVelX(0);
}
else if(key == KeyEvent.VK_DOWN){
p.setVelY(0);
}
else if(key == KeyEvent.VK_UP){
p.setVelY(0);
}
else if(key == KeyEvent.VK_SPACE){
c.addBullet(new Bullet(p.getX(), p.getY(), this)); // comes from controller class
} // with player class
else if(key == KeyEvent.VK_SPACE){
shooting = false; // force player to keep hitting space to shoot
}
}
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 frame = new JFrame(game.TITLE);
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
public BufferedImage getSpriteSheet(){
return spriteSheet;
}
public BufferedImage getBulletPic(){
return bulletPic;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.