Well, I’m not saying it can’t be made to work, but Swing components have some pretty heavyweight features attached to them that I don’t really see a scroller game utilizing (tooltips for the sprites? A plugable look and feel for the interface?)
Here’s quick answers to your question: An ImageObserver is an interface that will recieve update events from a Image source (typically) about changes to the image (typically done during loading). To implement, just Create a class, implement the updateImage method of the interface, and do some work when the method is called based on the parameters…it will fire the event when the image is loaded, the height is known, etc. Nothing terribly useful that I can see, usually I just use a MediaTracker to monitor the loading of images.
You wouldn’t normally implement the Graphics class, you are fine to use the BufferedImage and get the graphics for it using createGraphics() as you said.
To load the image from a file, you can do the following:
MediaTracker tracker = new MediaTracker(new Container());
try
{
InputStream in = this.getClass().getResourceAsStream(fileName);
ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream();
byte[] currentBytes = new byte[2048];
int bytesRead = 0;
while ((bytesRead = in.read(currentBytes)) != -1)
imageByteStream.write(currentBytes,0,bytesRead);
loadedImage = Toolkit.getDefaultToolkit().createImage(imageByteStream.toByteArray());
tracker.addImage(loadedImage, 0);
}
catch (Exception e)
{}
try
{tracker.waitForAll();}
catch (InterruptedException e)
{}
// added following lines to support drawing onto loaded image
returnImage = new BufferedImage(loadedImage.getWidth(null),loadedImage.getHeight(null),BufferedImage.TYPE_INT_ARGB);
returnImage.getGraphics().drawImage(loadedImage,0,0,null);
We load the bytes of the image into a byte[] because the version of createImage(String) looks for a filename. Using getResourceAsStream is nice because you just put the image in the classpath (like in the JAR) somewhere, and the classloader will find it. We mae a new BufferedImage because the image that is produced from the Toolkit.createImage() is not modifyable, so we just draw the image from the Toolkit onto a BufferedImage.
I wish I had a good tutorial for you, I’ve only learned by doing myself, but I think there’s some code examples on the old javagaming site.
-Chris