Hello all!
[quote]Oh…
Another quick question:
Am I referencing the images correctly? Am I supposed to start the reference as if I’m in the src (“sprites/alien.gif”) folder or somewhere else (in which case, where)?
I can’t reference any of the gifs (getting the fail msg I’ve implemented) Sad
ref = “sprites/homer.gif”
but when it gets to assigning URL, url = null
-_-
[/quote]
I am thoroughly annoyed with my current problem. I’m attempting to load an image and display it on screen (simple, huh?), but I can’t figure out what I’m doing wrong. To explain the code real quick - Screen class creates a JFrame and initiates everything. Sprite class is a wrapper for Image, contains a reference to an Image, a constructor and a draw method. SpriteStore is a singleton class for managing Sprite - mainly, I don’t want to load the same Sprite (Image) more than once. Thus - each unique sprite is stored in a HashMap (hashing is done according to the reference), upon requesting a Sprite, the HashMap is checked first. If the Sprite is already is the HashMap, I return the Sprite, otherwise, I load the sprite via the reference, store it in the HashMap and return it. Pretty simple and well understood.
Then, I decide to test it out, and load a random (.gif) image and draw it anywhere on the JFrame. The code breaks at:
Sprite testSprite = spriteStore.getSprite("sprites/homer2.gif");
at the screen init. The error is:
Exception in thread "main" java.lang.NullPointerException
at Screen.<init>(Screen.java:17)
at Screen.main(Screen.java:70)
I have no idea whats going on. The sprites/homer2.gif is located in my src folder. (I’m using Eclipse, if its relevant)
Here are three simple test classes:
import java.awt.Canvas;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.image.BufferStrategy;
import java.awt.Graphics2D;
import java.awt.Color;
public class Screen extends Canvas {
private BufferStrategy strategy;
private JFrame frame;
private JPanel mainPanel;
private boolean working;
private SpriteStore spriteStore = SpriteStore.get();
Sprite testSprite = spriteStore.getSprite("sprites/homer2.gif");
private Screen() {
frame = new JFrame("Accelerated Graphics Testing");
mainPanel = (JPanel)frame.getContentPane();
mainPanel.setPreferredSize(new Dimension(800,600));
mainPanel.setLayout(null);
this.setBounds(0, 0, 800, 600);
mainPanel.add(this);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
this.createBufferStrategy(2);
strategy = this.getBufferStrategy();
//initEntity();
working = true;
runningLoop();
}
/*private void initiateTestSprite(String ref) {
testSprite = spriteStore.getSprite(ref);
}*/
private void runningLoop() {
long lastLoopTime = System.currentTimeMillis(), delta;
//initiateTestSprite("sprites/alien.gif");
while(working) {
delta = System.currentTimeMillis() - lastLoopTime;
lastLoopTime = System.currentTimeMillis();
Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
g.setColor(Color.CYAN);
g.fillRect(0, 0, 800, 600);
//
testSprite.draw(g, 400, 200);
//
g.dispose();
strategy.show();
try {
Thread.sleep(500);
} catch(InterruptedException e) {}
}
}
public static void main(String[] args) {
new Screen();
}
}
import java.awt.Graphics;
import java.awt.Image;
public class Sprite {
private Image image;
public Sprite(Image image) {
this.image = image;
}
public int getWidth() {
return image.getWidth(null);
}
public int getHeight() {
return image.getHeight(null);
}
public void draw(Graphics g, int x, int y) {
g.drawImage(image, x, y, null);
}
}
import java.util.HashMap;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.awt.GraphicsConfiguration;
import java.awt.Transparency;
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpriteStore {
// singleton logic
private static SpriteStore single;
public static SpriteStore get() {
return single;
}
//
private HashMap sprites = new HashMap();
public Sprite getSprite(String ref) {
// if referenced .gif is loaded, return according sprite
if(sprites.get(ref) != null)
return (Sprite)sprites.get(ref);
// otherwise, load the referenced sprite, insert it into the HashMap and return the newly inserted sprite
BufferedImage sourceImage = null;
try {
URL url = this.getClass().getClassLoader().getResource(ref);
if(url == null)
fail("Can't find that :( " + ref);
sourceImage = ImageIO.read(url);
} catch(IOException e) {
fail("Failed to load :( " + ref);
}
// accelerated graphics memory allocation
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
Image image = gc.createCompatibleImage(sourceImage.getWidth(), sourceImage.getHeight(), Transparency.BITMASK);
image.getGraphics().drawImage(sourceImage,0,0,null);
Sprite sprite = new Sprite(image);
sprites.put(ref, sprite);
return sprite;
}
private void fail(String msg) {
System.err.println(msg);
System.exit(0);
}
}
In case it’s relevant, I was following this tutorial.
Thanks in advance,
Regards