I'm building a board game type game in JavaFX. Different GameObjects have different ObjectTypes. I'm currently storing a few static Images and referencing them when I need a new ImageView.
public class GameObject extends ImageView {
private static final Image BG_IMG = new Image(RESPATH + "BG.png");
private static final Image NULL_IMG = new Image(RESPATH + "NULL.png");
private static final Image T2_IMG = new Image(RESPATH + "T2.png");
private static final Image T3_IMG = new Image(RESPATH + "T3.png");
private static final Image T4_IMG = new Image(RESPATH + "T4.png");
private static final Image T5_IMG = new Image(RESPATH + "T5.png");
private static final Image T6_IMG = new Image(RESPATH + "T6.png");
//ObjectType is an enum type
private final ObjectType OBJECT_ID;
private Image img;
public GameObject(ObjectType t) {
OBJECT_ID = t;
setImage();
setImage(img);
}
private void setImage() {
switch (OBJECT_ID) {
case T2: img = (T2_IMG);
break;
case T3: img = (T3_IMG);
break;
case T4: img = (T4_IMG);
break;
case T5: img = (T5_IMG);
break;
case T6: img = (T6_IMG);
break;
case BG: img = (BG_IMG);
break;
default: img = NULL_IMG;
break;
}
}
I was wondering if this was a good way to implement the Images since I will be using the same Image for multiple ImageViews.
The other way I would implement this:
private void setImage() {
img = new Image(RESPATH + OBJECT_ID.name() + ".png")
}
If I did it this way, I'd just get rid of the static Images.