Images

For basic image operations you deal with images as instances of the class     java.awt.Image

As of Java 1.4 it is possible to load and draw image files encoded as JPEG, GIF and PNG. After Java 5.0 the bitmap formats BMP and WBMP also can be used

To load an image in an applet, you can use one of the overloaded getImage() methods for loading from a URL:
         Image img = getImage(URL);      URL is created by the Applet class methods  
getCodeBase() or   getDocumentBase().
The Applet class method getCodeBase() provides the URL for the location of the applet's class file.  getDocumentBase() gets the URL of the document in which this applet is embedded. 
or      Image img = getImage(URL,file path);   file path is a String value to be appended to this URL
i.e.
         Image img =   getImage(getCodeBase());
or      
Image img = getImage(getDocumentBase());
or      Image img = getImage(getCodeBase(),"images/img.jpg");                          
or     
Image img = getImage(getDocumentBase(),"images2/img.jpg");



For an application, use

  Image img = Toolkit.getDefaultToolkit().getImage(URL or file path);

The Toolkit is a class in the java.awt package that provides various resources and tools for the display system. One of the Toolkit methods is getImage() that functions much like the getImage() method in the Applet class. It is overloaded to take either a String filename parameter specifying the location of the image file or a URL parameter identifying the image file.

Before calling getImage(), one must have a reference to the Toolkit instance in use. The static method Toolkit.getDefaultToolkit() returns a reference to that Toolkit.

pImg001
application
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class Ex1 {
    public static void main (String[] arg){
        JFrame f = new JFrame("Ex 1");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyCanvas  cs = new MyCanvas();
        f.add("Center",cs);
        f.setSize(250,350);
        cs.setBackground(Color.yellow);
        cs.setForeground(Color.BLUE);
        cs.setFont(new Font("TimesRoman",Font.ITALIC,24));
        cs.repaint();
        f.setVisible(true);
    }

}
class MyCanvas extends Canvas{
    int x=50,y=70;
    int iw=100,ih=100;
    Image img=Toolkit.getDefaultToolkit().getImage("1_MAGIC.JPG");
    public void paint (Graphics g){     
        g.drawString("This an image", 20, 20) ;
        g.drawImage(img, iw, ih,120,120, this);

    }
}

pImg002
application with mouse
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class Ex1 {
    static Image img=Toolkit.getDefaultToolkit().getImage("1_MAGIC.JPG");
    static MyCanvas  cs = new MyCanvas();
    public static void main (String[] arg){
        JFrame f = new JFrame("Ex 1");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        cs.addMouseListener(new MyMouse());
        f.add(cs);
        f.setSize(250,350);
        cs.setBackground(Color.yellow);
        cs.setForeground(Color.BLUE);
        cs.setFont(new Font("TimesRoman",Font.ITALIC,24));
        cs.repaint();
        f.setVisible(true);
    }
    static public class MyMouse extends MouseAdapter {
        public void mouseClicked(MouseEvent ev) {
            cs.x=ev.getX();
            cs.y=ev.getY();
            cs.repaint();
            System.out.println("x="+ev.getX() + " y="+ ev.getY());
        }
    }


    static class MyCanvas extends Canvas{
        int x=50,y=70;
        public void paint (Graphics g){      
            g.drawString("This an image", 20, 20) ;
            g.drawImage(img, x, y,120,120, this);

        }
    }
}



pImg003
Applet
import java.awt.*;
import java.applet.*;
public class Ex extends Applet {
    Image img;
    int iw=50,ih=50;
    public void init(){
        img = getImage(getCodeBase(), "images/1_MAGIC.JPG");
        setSize(250,350);
        setBackground(Color.yellow);
        setForeground(Color.BLUE);
        setFont(new Font("TimesRoman",Font.ITALIC,24));
    }
    public void paint (Graphics g){      
        g.drawString("This is an image", 20, 20) ;
        g.drawImage(img, iw, ih,120,120, this);
    }
}