I'm trying to load an image in an Applet.The code goes fine and the Applet Window starts but it just gives a blank window instead of showing the image.I put the html file,.class file and the image file in the same directory.Can u figure out where I went wrong??

Here's my complete code :---

import java.applet.Applet;
import java.awt.*;


public class ImageTest extends Applet
{
private Image img;


public void init()
{
img = null;
}
public void loadImage()
{
try
{
img = getImage(getCodeBase(), "image1.gif");
}
catch(Exception e) { }
}
public void paint(Graphics g)
{
if (img == null)
loadImage();
g.drawImage(img, 0, 0, this);
}
}

here's the html code:---

<applet width=300 height=300 code="ImageTest.class"> </applet>

Please figure out the problem.Thanx in advance....

Dani AI

Generated

Short, practical follow‑up (adds a few things not shown above).

A quick checklist of likely causes: the applet’s base URL vs the page URL (use the correct base when forming the image URL), the image not being fully loaded when paint runs, server/path mistakes (image not uploaded or wrong relative path), and browser/plugin differences. The Applet API documents the difference between getCodeBase() and getDocumentBase() (the code base is the directory that contains the applet; the document base is the HTML page). (docs.oracle.com)

For reliable loading (instead of relying on implicit asynchronous loads) either block until the image is ready with a MediaTracker or load synchronously with ImageIO. Example patterns:

MediaTracker (wait for an async getImage):

MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 0);
mt.waitForID(0);
if (mt.isErrorID(0)) { /* handle missing/bad image */ }

Synchronous read (paths inside JAR or by URL):

BufferedImage img = ImageIO.read(new URL(getCodeBase(), "image1.gif"));

Both approaches are standard; see the MediaTracker and Image I/O docs for details. (docs.oracle.com)

Extra troubleshooting notes tied to earlier replies: as recommended, check the Java Console for exceptions and confirm the image URL directly in a browser (ensures server/MIME/path are correct). If the applet worked in appletviewer but not a browser, browser plugin/NPAPI support (now removed in many modern browsers) can be the cause — applets are effectively obsolete in current browsers. (blog.chromium.org)

Thanks to and for the original diagnostics; the MediaTracker/ImageIO approaches above provide a more robust fix for intermittent blank images.

Recommended Answers

All 12 Replies

Don't ignore errors

catch(Exception e) { }

Add a e.printStackTrace() to that catch block and check the Java Console.

I'm fairly certain that before you can load an image you have to prepare it first.

System.out.println(prepareImage(img, 300, 400, this));

use this after you attempt to get the Image specified by your source.

I applied both the "e.printStackTrace" and prepareImage things but still get the same result as the previous one...... please do some help regarding this.....

------ the applet gets started that's why I'm not able to use Java Console.But it is not showing any image. :(

There is a menu item somewhere under the browsers menus (called Java Console or something very close to that) and the printStackTrace will be printed there (if it occurs).

I got the java console and it is not giving any type of printStackTrace..please figure out the problem.

Not sure why but this is working for me, using a sample pic called Sunset.jpg

It may be that filetypes such as .gif and .png etc are not supported by the standard 1.4 Java Applet. In this case you may want to use one of two options--

-Try JApplet
-Import a GCanvas and use GImage to upload other image types.

import java.applet.Applet;
import java.awt.*;

public class ImageTest extends Applet
{
	private Image img;

	public void init()
	{
		img = null;
	}

	public void loadImage()
	{
		try
		{
			img = getImage(getCodeBase(), "Sunset.jpg");
			System.out.println(img);
			System.out.println(prepareImage(img, 300, 400, this));
		}
		catch(Exception e){}
	}

	public void paint(Graphics g)
	{
		if (img == null)
			loadImage();
		g.drawImage(img, 0, 0, this);
	}
}
commented: Alex Edwards! I. Love. You. I have been struggling for days with getting an image on an applet and you're code worked like magic!! Thank you so very much +0

@Alex-- Thanx dude.gif images are also working fine but these are not displaying in my browser.I'm able to view them using appletviewer..While other sample Applet programs having images embedded with them are displaying on my browser.Can u figure out this problem??

it's working fine on browser too..... thanx for your support

Dear Alex,
Thanks a lot for your help in this code.
I found it so helpful and i tried it with a little bit changing in it according to my own application which is motion detection in a video stream using taken snapshots from the video.

Thanks again and I hope it to be a start for a good friendship.

B Regards,
Saad

Alex Edwards! I. Love. You. I have been struggling for days with getting an image on an applet and you're code worked like magic!! Thank you so very much.

Sir. I have a question.. <snip>

DaniWeb Member Rules include:
"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules
Please start your own new thread for your question

This thread is closed. JC

I could run Alex's code in netbeans(shows desired image) but not in eclipse(does not show the image).Any idea why? @Alex Thanks alot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.