Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying an image

Status
Not open for further replies.

welshone

Programmer
Jul 30, 2001
414
GB
Hello,
I am trying to display an image in an Applet but am having problems.

String filename="anim1.gif";

anim1.gif is the name of the picture, but where do I need to copy the picture to so the applet can see it ?

 
Somewhere on your server - perhaps next to the applet, or in an 'images' directory, would be normal practice.
 
alright sebj, ?

I have it in the following folder :
c:/development/websphere_workspace/Game/images/anim1.gif

my code is :

public class MagnifyImage extends Applet {

Image theImage;
int scalefactor;
int scaleWidth, scaleHeight;

public void init() {

String filename="c:/development/websphere_workspace/Game/images/anim1.gif";
theImage = getImage(getDocumentBase(), filename);
scalefactor = Integer.valueOf("2").intValue();

}

public void paint (Graphics g) {
int x = theImage.getWidth(this);
int y = theImage.getHeight(this);
scaleWidth = x * scalefactor;
scaleHeight = y * scalefactor;
g.drawImage(theImage, 0, 0, scaleWidth, scaleHeight, this);
}

}

doesn't seem to work, I've also tried just :
string filename = "anim1.gif";
and some others ?
am I missing something important ?

 
Hi,

I believe you are misunderstaning the methods from Applet class that you are using.

The method "public Image getImage(URL url,String name)" has documentation which states that
"Returns an Image object that can then be painted on the screen. The url argument must specify an absolute URL. The name argument is a specifier that is relative to the url argument. "

Now you are passing the URL as "getDocumentBase()" method, which returns the absolute URL to the directory which your applet class resides. Thats fine. But the second parameter you are passing is the image name, of "c:/development/websphere_workspace/Game/images/anim1.gif" - now the docs state that if you supply an image filename it must be of a path relative to that of the URL given. Well - this isn't is it ! The docs also state that the method returns immediately - with or without an image, and will not throw an exception to indicate the file does not exist.

So .... the solution is to have an "images" directory next to your applet - so that when you call "getImage()" - you specify it like :
Code:
    String filename="images/anim1.gif";
    theImage = getImage(getDocumentBase(), filename);

Hope this helps.

Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top