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....

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.....

@masijade------ 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.