w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Java JPanel import image using eclipse

Access the image via the classpath. You can do it by using getClass().getResource() which returns an URL you can pass the ImageIcon(URL) or ImageIO.read(URL) (which returns an Image. The latter is preferred, as an exception will be thrown is something is wrong with the path

BufferedImage viewPort;
...
try {
    viewPort =
ImageIO.read(getClass().getResource("/pictures/viewport.png"));
} catch (IOException ex) { 
    // print some message 
}

Your pictures should be in the src

ProjectRoot
         src
            pictures
                  viewport.png




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.