I have created a simple Applet program that displays an image, this is my program:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.ImageIcon;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
Dimension d = getSize();
g.setColor(Color.BLACK);
g.fillRect(0, 0, d.width, d.height); // paint background
g.setFont(new Font("San-serif", Font.BOLD, 24));
g.setColor(new Color(255, 215,0));
g.drawString("Hello, world!", 60, 40);
// g.drawImage(getImage(getCodeBase(), "Rabbit.jpg"), 20, 60, this);
//g.drawImage(getImage(getCodeBase(), getClass().getResource("/Rabbit.jpg").getFile()), 20, 60, this);
ImageIcon image2 = new ImageIcon(getClass().getResource("/Rabbit.jpg"));
g.drawImage(image2.getImage(), 20, 60, this);
}
}
Now, in eclipse I copied the Rabbit.jpg
image to bin
directory and when I run the applet it is working fine and I can see the image.
Now if I place the class file in a directory on my machine and also the image Rabbit.jpg
in same path of my class file and then I created the below html file:
<applet code="HelloWorldApplet.class" width="350" height="350">
Java applet
</applet>
If I run the command appletviewer sample.html
then I can see the applet is loaded along with the image. Now if I open the sample.html
file in browser then the browser is not displaying the image, it just shows the text "Hello, world!"
Can you please tell me how I can see the image also as embedded in my applet?