0
public class Images extends Frame{
    static Image img;
    Images(){
        Image img = Toolkit.getDefaultToolkit().getImage("C:\Users\HP-PC\Desktop\104APPLE\two.jpg"); 

        MediaTracker track = new MediaTracker(this);
        track.addImage(img, 0);
        try{
            track.waitForID(0);
        }catch(InterruptedException ie){}

        this.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }
    public void paint(Graphics g){
        g.drawImage(img,200, 200, null);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Images i = new Images();
        i.setSize(400,400);
        i.setVisible(true);
        i.setIconImage(img);
        i.setTitle("Not Working...");
    }

}

I have provided a local path to the image but it is not working. Instead it shows the error Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
When I replace \ with \\\ the error disappears but the image does not get loaded.
Please help.

Hugues M.
  • 19,846
  • 6
  • 37
  • 65

1 Answers1

0

You need to double \ characters in string literals (not single or triple), like so:

Image img = Toolkit.getDefaultToolkit().getImage("C:\\Users\\HP-PC\\Desktop\\104APPLE\\two.jpg"); 

See here for details, all answers with positive scores are interesting.

Hugues M.
  • 19,846
  • 6
  • 37
  • 65