0

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?

learner
  • 6,062
  • 14
  • 79
  • 139
  • Where is `Rabbit.jpg` located? Path issue.. – Keerthivasan Dec 26 '14 at 09:45
  • @Keerthivasan, the image is in same folder where I have my class file and html file, I have mentioned it in my question. – learner Dec 26 '14 at 09:55
  • Try removing the `/` in path. – Keerthivasan Dec 26 '14 at 10:42
  • @Keerthivasan, removed `/` from the image path, but getting same issue. – learner Dec 26 '14 at 11:21
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Dec 27 '14 at 02:27
  • Don't try loading resources (such as images) from a paint method. The images should be loaded in `init()` and cached for later use. Is this image on the run-time class path? *"..because the `getClass().getResource(..)` is returning null for me even when I gave the exact path to the image."* The methods to get resources ***never*** require a complete path to the image and that will not work. The path should always be relative to the run-time class-path of the applet. Is `Rabbit.jpg` in the same directory as the HTML? – Andrew Thompson Dec 27 '14 at 02:31
  • @AndrewThompson, Thanks for responding, I am working on applet just to learn some basics of it. Regarding the image path, I have placed the image as we as html file in same directory and I tried all the 3 approaches mentioned in my question to load the image along with those that were commented. Can you please tell me how to fix it? – learner Dec 27 '14 at 03:01
  • Try `Image image = getImage("Rabbit.jpg");`.. – Andrew Thompson Dec 27 '14 at 03:07
  • @AndrewThompson, I am getting compile time error as - `The method getImage(URL) in the type Applet is not applicable for the arguments (String)` – learner Dec 27 '14 at 03:14
  • Uggh.. My bad, use [`Applet.getImage(URL,String)`](https://docs.oracle.com/javase/8/docs/api/java/applet/Applet.html#getImage-java.net.URL-java.lang.String-) instead, like.. `getImage(getDocumentBase(), "Rabbit.jpg")` .. – Andrew Thompson Dec 27 '14 at 03:31

0 Answers0