Skip to content

Displying Image in Applet

Java will allow the applet to load the data holding the HTML file that started the applet (theDocumentBase) and the directory from which class file was loaded (codeBase). Both documentBase and codeBase return directories as URL object respectively.

Code

  • We've assumed there is an image named blue.jpg.
    import java.awt.*;
    import java.applet.*;
    /*
        <applet code = "DisplayImage" height = "400" width = "400"></applet>
    */
    
    public class DisplayImage extends Applet {
    
        public Image picture;
    
        public void init() {
            picture = getImage(getDocumentBase(), "blue.jpg");
        }
    
        public void paint(Graphics g){
            g.drawImage(picture,30,30,this);
        }
    }