Does the same happen if you run it from the appletviewer?
Main Topics
Browse All TopicsI get an odd exception when my applet attempts to create a java.awt.image from a PNG file. I can't see what it is that I'm doing wrong :(
This is the interesting bit of the stack trace:
sun.awt.image.PNGImageDeco
at sun.awt.image.PNGImageDeco
at sun.awt.image.PNGImageDeco
at sun.awt.image.PNGImageDeco
at sun.awt.image.InputStreamI
at sun.awt.image.ImageFetcher
at sun.awt.image.ImageFetcher
I write out the buffer length that I get from the inputstream, and it is the same size as the PNG file. So I assume that my code has read all of the available data.
I've shortened the code that does this below, removing only the printStrackTrace() calls and log messages.
String s = "image.png";
InputStream in = getClass().getResourceAsSt
if (in == null) {
System.err.println("Error.
}
try {
buffer = new byte[in.available()];
} catch (IOException e) {
System.err.println("Error.
}
try {
in.read(buffer);
} catch (IOException e) {
System.err.println("Error.
}
image = Toolkit.getDefaultToolkit(
tracker.addImage(image, 1);
try {
tracker.waitForAll();
} catch (InterruptedException e) {
}
Any ideas?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
> buffer = new byte[in.available()];
in.available() returns the number of bytes currently available to read(), not necessarily the number of bytes in the image
> in.read(buffer);
This does not necessarily fill the buffer.
I'd suggest simply using the following to load your image:
ImageIcon icon = new ImageIcon(getClass().getRe
Image i = icon.getImage();
Business Accounts
Answer for Membership
by: htjPosted on 2003-06-27 at 08:29:09ID: 8814121
One more thing to note is that this only occurs when I attempt to run the applet in a browser.
It seems to work when I let my IntelliJ IDE run it.