I have an image and I want to display it in the applet, The problem is the image wont display. Is there something wrong with my code?
Thanks...
Here's my code :
import java.applet.Applet;
import java.awt.*;
public class LastAirBender extends Applet
{
Image aang;
public void init()
{
aang = getImage(getDocumentBase(), getParameter("images.jpg"));
}
public void paint(Graphics g)
{
g.drawImage(aang, 100, 100, this);
}
}
1) we living .. in 21century, then please JApplet instead of Applet
2) for Icon/ImageIcon would be better to look for JLabel
3) please what's
getImage(getDocumentBase(), getParameter("images.jpg"));
there I'll be awaiting something like as
Well , above answers are correct. This is the code I used to display image. Hope it helps:
In above code, we create an image class object and get image from location specified by codebase. Then plot the image using drawImage method. Those who are interested in knowing value of getCodeBase() and getDocumentBase() methods can add following code in paint method. They are actually location of src folder in your project folder:-
One more point to note:- Make sure images and classes don't have same name in src folder. This was preventing my image to be displayed.
I suspect you are doing something wrong, and that should be just plain:
What is the content of HTML/applet element? What is the name of the image? Is the image in the same directory as the HTML?
Update 1
The 2nd (changed) line of code will try to load the
images.jpg
file in the same directory as the HTML.Of course, you might need to add a
MediaTracker
to track the loading of the image, since theApplet.getImage()
method returns immediately (now), but loads asynchronously (later).Update 2
Try this exact experiment:
Save this source as
${path.to.current.code.and.image}/FirstAirBender.java
.Then go to the prompt and compile the code then call applet viewer using the source name as argument.
You should see your image in the applet, painted at 100x100 from the top-left.