I can't get my ImageHandler class working and need some help.
First of all, here are my 4 classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package InvadersGame;
import java.awt.image.BufferedImage; import java.io.PrintStream; import java.util.HashMap; import javax.imageio.ImageIO;
public class ImageHandler { private HashMap images; public ImageHandler() { images = new HashMap(); }
public BufferedImage loadImage(String name) { try { java.net.URL url = null; url = getClass().getResource(name); System.out.println(url); return ImageIO.read(url); } catch(Exception e) { System.exit(0); System.out.println("image not found"); return null; } } public BufferedImage getImage(String name) { BufferedImage image = (BufferedImage)images.get(name); if(image == null) { System.out.println("image null"); } else System.out.println("not null"); return image; } public HashMap getImages() { return images; }
public void setImages(HashMap val) { images = val; }
} |
and....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package InvadersGame;
import java.awt.Graphics; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.Graphics2D;
public class GameEntities { protected double x, y; protected int width, height; protected String imageName; protected boolean deletionMark = false; protected SpaceInvaders invaders; protected ImageHandler imageHandler; public GameEntities(SpaceInvaders invaders) { this.invaders = invaders; imageHandler = invaders.getImageHandler(); }
public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public String getImageName() { return imageName; }
public void setImageName(String name) { imageName = name; BufferedImage image = imageHandler.getImage(imageName); height = image.getHeight(); width = image.getWidth(); } public SpaceInvaders getGame() { return invaders; } public boolean collidesWith(GameEntities other) { Rectangle thisEntity = new Rectangle(); Rectangle otherEntity = new Rectangle(); thisEntity.setBounds((int)this.getX(), (int)this.getY(), this.getWidth(), this.getHeight()); otherEntity.setBounds((int)other.getX(),(int)other.getY(), other.getWidth(), other.getHeight()); return thisEntity.intersects(otherEntity); } public void move(){} public void paint(Graphics2D g) { g.drawImage(imageHandler.getImage(imageName), (int)x, (int)y, invaders); } } |
and....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package InvadersGame;
import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.awt.Graphics2D; import java.lang.Math; import java.util.Random;
public class AlienEntity extends GameEntities { int alienType; int speedx = 2; int speedy = 2; int firingFrequency; public AlienEntity(SpaceInvaders invaders) { super(invaders); } public void move() { y = y + speedy; } public void paint(Graphics2D g) { this.setImageName("alien4.gif"); } } |
and finally...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| package InvadersGame;
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.Graphics2D; import java.awt.geom.*; import java.util.ArrayList; import java.util.Random; import java.awt.image.BufferStrategy;
public class SpaceInvaders extends Canvas{ JPanel panel; Graphics2D g2; boolean gameRunning = true; ArrayList<GameEntities> entitiesList; AnimationThread thread; private BufferStrategy buffer; ImageHandler imageHandler; class AnimationThread extends Thread { public void run() { while(true) { paintAlien(); moveAlien(); try { sleep(50); } catch(InterruptedException e){} } } } public SpaceInvaders() { super(); imageHandler = new ImageHandler(); JFrame window = new JFrame("Space Invaders"); JPanel panel = (JPanel)window.getContentPane(); setBounds(0, 0, 800, 600); panel.add(this); window.setBounds(0, 0, 800, 600); window.setVisible(true); window.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setFocusable(true); this.createBufferStrategy(2); buffer = getBufferStrategy(); thread = new AnimationThread(); initialise(); } public void initialise() { entitiesList = new ArrayList(); for(int i=0; i<10; i++) { AlienEntity alien = new AlienEntity(this); alien.setX(60*i); entitiesList.add(alien); } } public void paintAlien() { Graphics2D g = (Graphics2D)buffer.getDrawGraphics(); g.setColor(Color.black); g.fillRect(0, 0, getWidth(), getHeight()); for(int i=0; i<10; i++) { entitiesList.get(i).paint(g); } buffer.show(); } public void moveAlien() { for(int i=0; i<10; i++) { entitiesList.get(i).move(); System.out.println(entitiesList.get(i).getY()); } } public void setBufferstrategy(BufferStrategy buffer) { buffer = buffer; } public ImageHandler getImageHandler() { return imageHandler; }
public static void main(String[] args) { SpaceInvaders canvas = new SpaceInvaders(); canvas.thread.start(); } } |
What happens is in the AlienEntity paint() method I set the image name as alien4.gif:
1 2 3 4
| public void paint(Graphics2D g) { this.setImageName("alien4.gif"); } |
which calls the setImageName() method in the GameEntities class:
1 2 3 4 5 6 7 8
| public void setImageName(String name) { imageName = name; BufferedImage image = imageHandler.getImage(imageName); height = image.getHeight(); width = image.getWidth(); } |
What this does is call the getImage(String name) method in the image handler class which
should load the appropriate image:
1 2 3 4 5 6 7 8 9 10 11
| public BufferedImage getImage(String name) { BufferedImage image = (BufferedImage)images.get(name); if(image == null) { System.out.println("image null"); } else System.out.println("not null"); return image; } |
I've tried running the spaceInvaders class and get a null pointer exception. I put a print line in the image loader method (in ImageHandler) which says that url = null:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public BufferedImage loadImage(String name) { try { java.net.URL url = null; url = getClass().getResource(name);
System.out.println(url);
return ImageIO.read(url); } catch(Exception e) { System.exit(0); System.out.println("image not found"); return null; } } |
I assume the whole problem lies in the fact that it's not finding my image and so can't load it. I've put the image in the same folder and from within netbeans set the working directory to this folder in a hope that it would find and load the image.
Can someone PLEASE check my code to see if there's any fundamental problem and let me know how to fix this bug.
Cheers
