[英]Getting information from an Enum
So i have Enum called Terrain 所以我让Enum称为Terrain
public enum Terrain {
DIRT(4, "res/imgs/DirtTile.png"), GRASS(5, "res/imgs/GrassTile.png");
private String filePath;
private Image image;
private boolean imageLoaded;
private int value;
Terrain(int val, String imagePath) {
value = val;
filePath = imagePath;
imageLoaded = false;
}
public Image getImage() {
if (!imageLoaded) {
loadImage();
}
return image;
}
public void loadImage() {
try {
image = ImageIO.read(new File(filePath));
} catch (IOException e) {
System.err.println("Failed to load image!");
e.printStackTrace();
}
imageLoaded = true;
}
}
What I want is to be able to compare an int to the terrain value so that if it does equal, that terrain will be drawn to the screen. 我想要的是能够将int与地形值进行比较,这样如果它相等,那么该地形将被绘制到屏幕上。
I was thinking something like 我在想类似的东西
if(int==Terrain.value){}
But I'm not really sure how to go about this. 但我真的不确定如何解决这个问题。 If someone could help me out on this, I have an int array which i want to compare it to and if it does equal the value, I store that tile in a seperate image array.
如果有人可以帮助我,我有一个int数组,我想比较它,如果它确实相等的值,我将该瓷砖存储在一个单独的图像数组中。
EDIT: 编辑:
For all those saying I should put a getValue() method, I want to compare my value to the all of the Enums stuff and if it does equal(to 4 for example, then I can set my image(to dirt). 对于那些说我应该放置一个getValue()方法的人,我想将我的值与所有Enums的东西进行比较,如果它相等(例如4,那么我可以设置我的图像(到污垢)。
I'm checking this in a different class. 我在另一个班级检查这个。
you already have a method for getImage
. 你已经有了
getImage
的方法。 Simply add a similar method for getValue
只需为
getValue
添加一个类似的方法
public int getValue() {
return value;
}
then you can do 那么你可以做到
if (2 == Terrain.DIRT.getValue())
//doSomething()
One possibility is add a static function to the Terrain enum as: 一种可能性是在Terrain枚举中添加一个静态函数:
static Terrain getTerrainFor(int val)
{
for (Terrain t : Terrain.values())
{
if (t.value() == val)
return t;
}
return t;
}
call this (Terrain.getTerrainFor(int)) do logic 调用this(Terrain.getTerrainFor(int))做逻辑
Add a getValue()
method to the enum: 在枚举中添加
getValue()
方法:
public int getValue() {
return value;
}
Wherever you have an instance of the Terrain
enum, call the getValue()
method and do the comparison as you like: 只要你有
Terrain
枚举的实例,就可以调用getValue()
方法并根据需要进行比较:
Terrain t = Terrain.DIRT;
if (t.getValue() == 4) {
System.our.println("The terrain value is 4!");
}
enum Terrain{
private int value;
private static final Map<Integer, Terrain> terrainMap;
static{
terrainMap = new HashMap<Integer, Terrain>();
for(Terrain type: values()){
terrainMap.put(type.value, type);
}
}
public static Terrain getTerrain(int value){
return terrainMap.get(value);
}
}
Code that uses this enum: 使用此枚举的代码:
Terrain terrain = Terrain.getTerrain(4);
switch(terrain){
case DIRT:
//draw dirt
}
In most cases like this, the best would be to get rid of the ints and use the enum type instead. 在大多数情况下,最好的方法是摆脱整数并使用枚举类型。
Terrain terrain = somevalue;
if( terrain == Terrain.DIRT ) {
// do something
}
If you want to get a Terrain
from an int
you can use java's built in valueOf(String)
(This would be the easiest solution to your problem). 如果你想从一个
int
获得一个Terrain
你可以使用java的内置valueOf(String)
(这将是你问题的最简单的解决方案)。
int i = 2;
Terrain t = Terrain.valueOf("" + i);
Of course you have to convert the int to a string (I used concatenation with an empty String for that. 当然你必须将int转换为一个字符串(我使用了一个空字符串连接。
EDIT: The best performing solution to your problem would be to use a static array, that contains the Terrain
at the index, with a static method, that get's it out of there. 编辑:对你的问题最好的解决方案是使用一个静态数组,它包含索引处的
Terrain
,使用静态方法,从而得到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.