0

I am trying to set a file path in a CSS command.

My Main-Class has this path:

/Users/magister/MySnake/src/main/java/SnakeGame.java

Out of this very class I'm trying to use this command:

root.setStyle(""
                + "-fx-background-image: url('/resources/images/gras.png');"
                + "-fx-background-size: 20 20;"
                + "-fx-background-repeat: repeat;"
                + "-fx-border-color: black;"
                + "-fx-border-style: solid;"
                + "-fx-border-width: 2;");

The image gras.png has this path:

/Users/magister/MySnake/src/main/resources/images/gras.png

I've been trying out amounts of alternatives for the path in the css-command/statement. But it hasn't ever been working so far.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • 1
    If your project setup follows common practice your path should be '/images/gras.png'. – mipa Dec 21 '20 at 13:44
  • It's working now! But why so? Why do I have to ignore /recources/? How to check the project setup? (Unfortunately I don't see how to rate your comment as helpful, @mipa ...) – MagisterInformaticus Dec 21 '20 at 13:53
  • you have to understand how resources are looked-up, see f.i. https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other – kleopatra Dec 21 '20 at 13:56
  • 1
    So many people seem to forget, or just ignore, the fact that Java is a compiled language. The structure of the *source* folder is completely irrelevant at runtime - all that's important is the structure of the resulting *build*. If you look in your build folder (or jar file, if you're running from a jar) you'll see the relevant structure. Typically, `resources` is used as a "resource root", which means its contents (including `images` in your case) are just copied to the root of the classpath in the resulting build. – James_D Dec 21 '20 at 19:29

0 Answers0