Question

I am trying to load an Image as a part of an Enum but it throws an exception, I could not find a way to sorround it with try catch, is there any other way, or it not possible?

Here is some commented code, I am using Slick2d Library.

    public enum Material {
//An enum of different materials, with different propierties and images

GRASS (5, false, null, true),

//Here I try to load an Image, but loading images throw an Exception, a Slick Exception I tried surronding it with try catch but its not working.
DIRT (5, false, new Image("res/Dirt.png"), true), 

WOOD (10, false, null, false), 

SNOW (1, false, null, false), 

ICE (1, false, null, false), 

LEAVES (1, false, null, false), 

STONE(15, false, null, false), 

COBBLESTONE(10, false, null, false), 

LOG(15, false, null, false),

AIR(1, false, null, false);


///variables
private int Resis;
private boolean traspasable;
private static Image I;
private boolean TreesCanGrow;

//getters
public int getResis(){
    return this.Resis;
}

public Image getTile(){
    return this.I;
}

public boolean isTraspasable(){

return this.traspasable;

}

public boolean getTreesCanGrow(){

return this.TreesCanGrow;

}


///constructor
private Material(int ExplosionResis, boolean isTraspasable, Image I, boolean TCG){  
this.Resis = ExplosionResis;
this.traspasable = isTraspasable;
this.TreesCanGrow = TCG;    
}


}

Edit: new Contructor

///constructor
try {
    private Material(int ExplosionResis, boolean isTraspasable, String image, boolean TCG){ 
        this.Resis = ExplosionResis;
        this.traspasable = isTraspasable;
        this.TreesCanGrow = TCG;    
        this.I = new Image(image);
    }
}
catch (SlickException e) {
    e.printStackTrace();
}

}
Était-ce utile?

La solution

I don't recommend putting code that can fail in an enum constructor or as parameters of an enum value declaration. If the code throws an exception, the enum class will likely not load.

You could try something like this:

/** An enum of different materials, with different properties and images. */
public enum Material {
  GRASS (5, false, null, true),

  DIRT (5, false, "res/Dirt.png", true), 

  ...

  AIR(1, false, null, false);


  private final int resis;
  private final boolean traspasable;
  private final String imagePath;
  private Image image;
  private final boolean treesCanGrow;

  private Material(int resis, boolean traspasable, String imagePath,
      boolean treesCanGrow) {  
    this.resis = resis;
    this.traspasable = traspasable;
    this.imagePath = imagePath;
    this.treesCanGrow = treesCanGrow;    
  }

  public synchronized Image getTile() throws SlickException {
    if (image == null && imagePath != null) {
      image = new Image(imagePath);
    }
    return image;
  }
}

Personally, however, I prefer enums to be immutable, so if it was my code, I would put the creation of the Image somewhere else (perhaps in a Map<Material, Image> that is lazy-loaded).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top