Loading image in an Applet

Thread Solved

Join Date: Jun 2008
Posts: 14
Reputation: funtoosh is an unknown quantity at this point 
Solved Threads: 0
funtoosh funtoosh is offline Offline
Newbie Poster

Loading image in an Applet

 
0
  #1
Jun 6th, 2008
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....
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Loading image in an Applet

 
0
  #2
Jun 6th, 2008
Don't ignore errors
  1. catch(Exception e) { }

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

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Loading image in an Applet

 
0
  #3
Jun 6th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: funtoosh is an unknown quantity at this point 
Solved Threads: 0
funtoosh funtoosh is offline Offline
Newbie Poster

Re: Loading image in an Applet

 
0
  #4
Jun 6th, 2008
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.
Last edited by funtoosh; Jun 6th, 2008 at 1:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Loading image in an Applet

 
0
  #5
Jun 6th, 2008
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).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: funtoosh is an unknown quantity at this point 
Solved Threads: 0
funtoosh funtoosh is offline Offline
Newbie Poster

Re: Loading image in an Applet

 
0
  #6
Jun 6th, 2008
I got the java console and it is not giving any type of printStackTrace..please figure out the problem.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Loading image in an Applet

 
0
  #7
Jun 6th, 2008
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.

  1.  
  2. import java.applet.Applet;
  3. import java.awt.*;
  4.  
  5. public class ImageTest extends Applet
  6. {
  7. private Image img;
  8.  
  9. public void init()
  10. {
  11. img = null;
  12. }
  13.  
  14. public void loadImage()
  15. {
  16. try
  17. {
  18. img = getImage(getCodeBase(), "Sunset.jpg");
  19. System.out.println(img);
  20. System.out.println(prepareImage(img, 300, 400, this));
  21. }
  22. catch(Exception e){}
  23. }
  24.  
  25. public void paint(Graphics g)
  26. {
  27. if (img == null)
  28. loadImage();
  29. g.drawImage(img, 0, 0, this);
  30. }
  31. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: funtoosh is an unknown quantity at this point 
Solved Threads: 0
funtoosh funtoosh is offline Offline
Newbie Poster

Re: Loading image in an Applet

 
0
  #8
Jun 6th, 2008
@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??
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: funtoosh is an unknown quantity at this point 
Solved Threads: 0
funtoosh funtoosh is offline Offline
Newbie Poster

Re: Loading image in an Applet

 
0
  #9
Jun 6th, 2008
it's working fine on browser too..... thanx for your support
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 1
Reputation: Saad Jareer is an unknown quantity at this point 
Solved Threads: 0
Saad Jareer Saad Jareer is offline Offline
Newbie Poster

Re: Loading image in an Applet

 
0
  #10
Jun 21st, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC