简体   繁体   中英

Java - Import main class in subfolder for Image

in my project I need to create an image for each object when is created.

    public abstract class Pirate extends ImageView {
    protected static int pirateSpeed, pirateScore;
    protected String word;

    public Pirate(String face, String word) {
        this.setImage(new Image(PiratesAndTreasures.class.getResource("/resources/" + face).toExternalForm()));
        this.word = word;
    }
}

PiratesAndTreasures is the main class and the project structure looks like this:

在此处输入图片说明

As far as I understood I need to access main class when I am using this:

new Image(TheMainClass.class.getResource("/resources/" + picture).toExternalForm()));

..well here is the issue: I couldn't find a way to import from an upper folder or from the "src".

Any help would be much appreciated.

UPDATE: It states "Cannot resolve symbol PiratesAndTreasures"

You could try using ImageIO.read(getClass().getResource("/resources/" + picture)) That way its not dependent on wherever the main class is. This should work the same as what you have above since all these sub folders are under src.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM