1

I tried to display an Image in applet window but it's not working. I have placed the image in the same directory of the applet file.

Here is my sample code

import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;

public class Animation extends Applet{

    Image img;

    public void init(){
        img=getImage(getDocumentBase(),"images.jpg");           
    }

    public void paint(Graphics g){
         g.drawImage(img, 40, 60, this);
         g.drawString("This is first step", 10, 30);
    }

}
  • *"I have placed the image in the same directory of the applet file"* - Then try using `ImageIO.read(getClass().getResource("images.jpg"))` instead. `getImage` is trying to load it from the web server or execution context that the applet viewer is using – MadProgrammer Jul 07 '15 at 11:54
  • `Applet` was superseded by `JApplet` over 15 years ago, you might consider using it instead. Also, you should be calling `super.paint` before you do any custom painting and `JLabel` is a better way to display an image – MadProgrammer Jul 07 '15 at 11:55
  • 1
    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. 3) Is the image in the same directory as the HTML that is loading the applet? That is where the JRE will be looking for the image. – Andrew Thompson Jul 08 '15 at 07:03
  • *"I have placed the image in the same directory of the applet file."* - What do you mean by this line? Which directory? – Saifuddin Hitawala Jul 10 '15 at 05:38
  • My Animation.java file is located in c:\workspace\applet\Animation.java and the image i want to load in applet is located in c:\workspace\applet\images.jpg – Balaji Muthusamy Jul 10 '15 at 06:15
  • Try this location: c:\workspace\applet\bin\images.jpg – Saifuddin Hitawala Jul 10 '15 at 06:38
  • Thankyou @madProgrammer i got output.. replaced getImage with ImageIO.read(getClass().getResource("images.jpg")) – Balaji Muthusamy Jul 10 '15 at 07:15

2 Answers2

0

Just as @MadProgrammer said, you can simply use the IO method for reading the image from it's proper URL.
Else you can try putting the image in /bin folder of your workspace.
If it still doesn't work try doing this.

Community
  • 1
  • 1
0
/*
<applet class='Applet3' width=400 height=400>
</applet>
*/
import java.awt.*;
import java.applet.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;


public class Applet3 extends Applet {
Image img;

@Override
    public void init() {
    try { 
        img = ImageIO.read(getClass().getResource("g.jpg")) ;

    } catch (IOException ex) {
        Logger.getLogger(Applet3.class.getName()).log(Level.SEVERE, null, ex);
    }
    }
  public void paint(Graphics g) {  
    g.drawImage(img, 30,30, this);  
  }

}
  • Please refer above code. I have included method ImageIO.read(getClass().getResource("g.jpg")) suggested by @madProgrammer. I am using netbeans. To copy image to correct location Please refer below link. https://www.youtube.com/watch?v=fEPm2Nz6ocM&t=124s – Parag Kambli Nov 19 '17 at 17:14