-1

I'm trying to draw an image using Graphics2D in Java, but the path cannot be read because Illegal escape character in string literal

I am using this line of code:

g2.drawImage("C:\Users\User\Pictures\Untitled", playerX, playerY);

However, due to the backslashes in the path the path returns an error. How can I fix this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
off oof
  • 43
  • 5

1 Answers1

1

Escape \ with \

g2.drawImage("C:\\Users\\User\\Pictures\\Untitled", playerX, playerY);
Jens
  • 67,715
  • 15
  • 98
  • 113
  • 2
    Ultimately that is not going to help - `Graphics2D` has no such method. Create an `Image` with `ImageIO.read` and make that the first argument. The path to `read` can be `"C:/Users/User/Pictures/Untitled"` (though an odd name for an image file) – g00se Apr 20 '22 at 07:50