Hi,

I am trying to load an image into an applet, however unsuccessfully.
What's wrong here?

package sess31;

// Import the AWT graphics package
import java.awt.*;
import java.applet.*;

public class myownapplet extends Applet
{
    Image image;
    public void init() {
        // Load image
        //image = getImage(getDocumentBase(), "show14.gif");
    }
    public void paint(Graphics g) {
        // Draw image
        g.drawImage(image, 0, 0, this);
    }
}
Re: load image in applet 80 80

Hi,

I can't see any faults in your code, so my guess is that the program is unable to find the image.
If your folder structure looks like this:
.\src\sess31\myownapplet.java
then place the image like this:
.\src\image.gif

or specify the location in the path.

Hope this helps!

Re: load image in applet 80 80

Thank you for your answer.
At the moment I have the image file in that location but the image is not appearing in the applet.

However if I try with the same image from an URL, it works:

image = getImage(getDocumentBase(), "http://www.iki.rssi.ru/IPL/show14.gif");

I don't get it why is this. Should the image file be in another location?


Hi,

I can't see any faults in your code, so my guess is that the program is unable to find the image.
If your folder structure looks like this:
.\src\sess31\myownapplet.java
then place the image like this:
.\src\image.gif

or specify the location in the path.

Hope this helps!

Re: load image in applet 80 80

How are you running the applet? Make sure that the image is available from where you run it.

Re: load image in applet 80 80

The image is there.
I am attaching the project, so if you could take a look I apreciate very much :)

Re: load image in applet 80 80

Do you get a security exception when launching the applet? If that is the case you need to sign it before you can run it.

If not, it might just be becuase the image doesn't reside in the build folder. Try adding it there.

Re: load image in applet 80 80

It works now, thank you very much for the help.
I didn't know that the image had to be in the build folder.

Cheers!

Do you get a security exception when launching the applet? If that is the case you need to sign it before you can run it.

If not, it might just be becuase the image doesn't reside in the build folder. Try adding it there.

Re: load image in applet 80 80

tq.. this solve my problems =)

Re: load image in applet 80 80

You can Run this program.First copy it in jdk/bin directory then copy rn1.jpg named any picture in ur jdk/bin directory. Ok Run this program now

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public class ronyphoto1 extends Applet
{
Image Img1;


public void init()
{
    URL codebase=getCodeBase();

    showStatus(codebase.toString());

    Img1=getImage(codebase,"rn1.jpg");
    //Img2=getImage(codebase,"Image2.gif");
}


public void paint(Graphics g)
{
    g.drawImage(Img1,10,10,this);
    //g.drawImage(Img2,400,10,this);
}

}
Be a part of the DaniWeb community

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