Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image is not getting loaded in applet

 
madhavi prasad
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have downloaded the folowing code from javaranch site.
Able to compile the java prog, im getting msg applet started at the status bar but image is not getting loaded,I tried printing the msg in the where im calling g.drawImage () method.String is displayed on the applet but image is not coming.can any one tel what could be the reason .

thnx & regards
madhavi


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

public class LoadAGifMsg extends Applet
{
private Image theHorse ;
private Image theMoose ;
private boolean showWaitMsg = true ; // will be set to false after image downloads

public void init()
{
// anything in here will run BEFORE the paint() method is called,
// even if you call repaint() now inside init
} // close init

public void loadGraphics()
{
// now load the graphics - this is like your "real" init
theHorse = getImage( getCodeBase() , "M1.JPEG" );
theMoose = getImage( getCodeBase() , "M2.JPEG" );
MediaTracker mt = new MediaTracker( this );
mt.addImage( theHorse , 0 );
mt.addImage( theMoose , 1 );

try
{
mt.waitForAll(); // block here until images are downloaded
}
catch ( InterruptedException e )
{
}

showWaitMsg = false ; // it is safe for paint to draw the image now
repaint();
} // close loadGraphics

public void paint( Graphics g )
{
// test a boolean to if the "loading" message should be displayed
if ( showWaitMsg )
{
g.drawString( "Please wait... loading..." , 20 , 20 );
loadGraphics(); // call the method that actually loads the graphics
}
else
{
g.drawImage( theHorse , 25 , 25 , this );
g.drawImage( theMoose , 25 , 130 , this );
}
} // close paint
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the files M1.JPEG and M2.JPEG in the same directory as the applet's HTML file?
 
madhavi prasad
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Offcourse yes , they r in the same directory......
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. I misspoke, actually -- you're using getCodeBase() for the URLs, so the images have to be in the same place as the .class file. I'll assume that the .class file is in the same directory, too. If the .class is in a JAR, then the images will have to be in there, too.

Maybe you've just got the applet's area too small in the HTML file? The coordinates given to drawString() are the lower-left corner of the String, while those given to drawImage are the top-left corner of the image, so the top of the upper image will be five pixels below the bottom of the String.
 
madhavi prasad
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your assumption is corect.Class file is also in the same directory.No class is not in jar file.Width and height are 900/900.still not able to get.........
 
madhavi prasad
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried printing the url of the image
its printing sun.awt.windows.WImage@10da5ed
i dont know from where it is picking above url
my .java,.class,.htmland .jpeg files are in c drive.

guys help me out ..........
 
alexandre soares
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by madhavi prasad:
Hi,

theHorse = getImage( getCodeBase() , "M1.JPEG" );
theMoose = getImage( getCodeBase() , "M2.JPEG" );

}


Hi,
 
alexandre soares
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by alexandre soares:


Hi,


Hi, the illustration doesn't open, therefore the jvm this seeking in the root (c , then put the complete road
example:

theHorse = getImage( getCodeBase() , "c:\\folder\\M1.JPEG" );
theMoose = getImage( getCodeBase() , "c:\\folder\\M2.JPEG" );

by
 
madhavi prasad
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When i have given the path
eg: madhavi=getImage( getCodeBase(), "C:\\M1.JPEG" );
it is throwing nullpointer exception
at the below line
g.drawImage( madhavi , 25 , 25 , this )
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You can't use absolute file paths in an applet, anyway -- that's a bad suggestion.

This is a tough one. Have you viewed the image files in some other program to make sure they're OK?
 
madhavi prasad
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont hav any other program to chk
but i hav tried giving gif images of small size
even no out put how do i solve this
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic