Search code examples
javaimageicon

image icon java error invalid escape sequence


package shooterGuy;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Display extends JPanel{

int width = 800;
int height = 800;
JFrame j;
public void run(){
    j = new JFrame("Shooter Guy");
    j.setVisible(true);
    j.setSize(width, height);
    j.setResizable(true);
    j.setLocationRelativeTo(null);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    loadPics();

}

public void paintComponent(Graphics g){
    g.drawImage(bg, 0, 0, null);
}
Image bg;
public void loadPics(){
    bg = new ImageIcon("K:\files\jamiesstuff\java\Java Pictures\Back.png").getImage();

}
}

This is my display class and it underlines "K:\files\jamiesstuff\java\Java Pictures\Back.png" and says the error is "Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )" I do not know what that means or how to fix it. I did the same exact thing on a mac and it worked fine. Please tell me how to fix this error.


Solution

  • You have to escape the backslash or using slash as path separator:

    bg = new ImageIcon("K:\\files\\jamiesstuff\\Java\\Java Pictures\\Back.png").getImage();
    

    or

    bg = new ImageIcon("K:/files/jamiesstuff/java/Java Pictures/Back.png").getImage();