| | |
Loading image in an Applet
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Solved Threads: 0
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....
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....
Don't ignore errors
Add a e.printStackTrace() to that catch block and check the Java Console.
Java Syntax (Toggle Plain Text)
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
----------------------------------------------
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
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Solved Threads: 0
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.
@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.
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
----------------------------------------------
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
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.
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.
Java Syntax (Toggle Plain Text)
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); } }
•
•
Join Date: Jun 2009
Posts: 1
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Adding an Image to a JPanel (Java)
- Java Game Characteristics (Java)
- gif problem (Java)
- How do I place images on a rotating divided cube using Java? (Java)
- Help for Senior Citizen (Java)
- "cannot resolve symbol"problem (Java)
- Loading images (Java)
- Loading images in AWT (Java)
- RUNDLL32.EXE is not responding (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: J2SE Exam 310-065
- Next Thread: CarPool calculator in java - please help
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






