-1

My code:

 public enum Rank{

//1.1 enum y filename
ACE(1, "resources/e1.png"),TWO(2, "resources/e2.png"),THREE(3, "resources/e3.png"),
FOUR(4, "resources/e4.png"),FIVE(5, "resources/e5.png"),SIX(6, "resources/e6.png"),
SEVEN(7, "resources/e7.png"),EIGHT(8, "resources/e8.png"), NINE(9, "resources/e9.png"),
TEN(10, "resources/e10.png"), JACK(11, "resources/e11.png"),QUEEN(12, "resources/e12.png"),
KING(13, "resources/e13.png");

private final int rankValue;
private final type? imageFile;

//1.2 constructor
private Rango(int rankValue, type? imageFile){
    this.rankValue= rankValue;
    this.imageFile= imageFile;
}

How can I get images be read in a method? What assigned type can read those image filepaths? Any advice please? Thanks.

Edit: is it possible to use Strings instead i.e. String imageFile and then use that type I'm missing to set it read the String filename?

Diego C
  • 35
  • 7
  • You're starting to add a lot of complexity and entanglement to your enum. Instead, consider having a separate `ImageLoader` class and naming your resources like `card/ACE.png`. – chrylis -cautiouslyoptimistic- Apr 19 '20 at 01:48
  • nothing learned since your pevious question https://stackoverflow.com/q/61199559/203657 ? – kleopatra Apr 19 '20 at 04:46
  • @kleopatra It got much more complex than that.I mean, I understand it but would like to split the code into specific classes to keep organized. And the filepath isn't reaching my project. Right now I have 6 or 7 classes and 3 controllers classes plus fxml to link with and increasing. I wish I could show all my code but not so public and asking bit by bit is getting me nowhere. Sorry. – Diego C Apr 19 '20 at 05:02

1 Answers1

2

UPDATE: Changed to load JavaFX Image in the constructor, in order to not use Java 9 feature, and to cache the image instead of loading on every access.

You can access the file using getResourceAsStream().

There are many articles on the web for how to use that, but something like this would work:

public enum Rank {

    ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7),
    EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13);

    private final int rankValue;
    private final Image image;

    private Rank(int rankValue) {
        this.rankValue = rankValue;
        String imageFile = "/resources/e" + rankValue + ".png";
        try (InputStream in = getClass().getResourceAsStream(imageFile)) {
            if (in == null)
                throw new IllegalStateException("Image not available for Rank." + name());
            this.image = new Image(in);
        } catch (IOException e) {
            throw new IllegalStateException("Error loading image for Rank." + name(), e);
        }
    }

    public int getRankValue() {
        return this.rankValue;
    }

    public Image getImage() {
        return this.image;
    }
}

If you don't want the file name to be derived from the rankValue, pass in imageFile as parameter instead.

Andreas
  • 147,606
  • 10
  • 133
  • 220
  • I didn't understand what you did to the public byte[ ] getImage. I am using java 8 and I think I don't have it. Using maven archetype javafx. I don't manage to add this code working. – Diego C Apr 19 '20 at 01:58
  • 1
    My guess is that for most use cases for an `enum`, it would be better to load the image data once (i.e. load it in the constructor and cache it in a field) than to reload it each time on demand. Since you have a fixed number of enums, and they're singletons, the memory trade-off is well controlled. – James_D Apr 19 '20 at 02:36
  • I found out what was best for this: Image img = new Image(myclass.class.getResources("resources/image.png").toExternalForm()); Now, packages in maven archetype must be the same as your project and then work in different project.packages instead all in one. – Diego C Apr 19 '20 at 09:07