Wednesday, August 7, 2024

Eclipse: how to read images from resources

Create resources folder

  1. create "res" folder via "Package Explorer".
  2. right click on "res" folder -> "Build Path" -> "Use as Source Folder". "res" will  be root of your resource.
  3. create "images" folder inside "res" folder. Copy your images into this folder. Path your file becomes "/images/example.jpg".

Accessing images from the "images" folder, use this piece of code into your java source code:

        String cName = "example.jpg";
        String sFName = "/images/"+cName;
        BufferedImage img = ImageIO.read(this.getClass().getResource(sFName));

References:

https://stackoverflow.com/questions/31152233/opening-image-asset-java-jdk-8

No comments:

Post a Comment