I'm extremely new to Java. I'm in a very basic Java programming class that's really boring and tedious, and I highly doubt we're going to learn anything like this all year, but what I was wondering is...
Can a code be written to define both a string's contents as well as the string's name based on an array of integers--which will be referred back to by a constructor method--perhaps by "text" + .toString + "text" to make up the name of a certain file (an icon)?
I want to make it so that I can have an icon for north-, south-, east- and west-facing characters, direction being defined by one integer in the constructor, while using another integer in the constructor to change the set of icons used by that object. What I have written looks like this, which I built off of the existing code in the jar file our class uses as a classpath:
static ImageIcon getCharacterIcon(int direction, int app){
switch (direction) {
case NORTH: {
if (app == 1) {
if (nbm == null)
nbm = new ImageIcon(Display.class.getResource(nbmLocation));
return nbm;}
else {
if (nkarel == null)
nkarel = new ImageIcon(Display.class.getResource(nkarelLocation ));
return nkarel;}
}
case EAST: {
if (app == 1) {
if (ebm == null)
ebm = new ImageIcon(Display.class.getResource(ebmLocation));
return nbm;}
else {
if (ekarel == null)
ekarel = new ImageIcon(Display.class.getResource(ekarelLocation ));
return ekarel;}
}
case SOUTH: {
if (app == 1) {
if (sbm == null)
sbm = new ImageIcon(Display.class.getResource(sbmLocation));
return sbm;}
else {
if (skarel == null)
skarel = new ImageIcon(Display.class.getResource(skarelLocation ));
return skarel;}
}
case WEST: {
if (app == 1) {
if (wbm == null)
wbm = new ImageIcon(Display.class.getResource(wbmLocation));
return wbm;}
else {
if (wkarel == null)
wkarel = new ImageIcon(Display.class.getResource(wkarelLocation ));
return wkarel;}
}
default:
Debug.printError("Karel image or character for direction " + direction + " or " + app + " not found! Aborting...");
System.exit(7);
return null;
}
}
I don't know if it works yet, but I think it does. Fairly sure it compiles and everything, or would if I had code identifying the bm strings. I was hoping to be able to use an integer array with both karel and bm--or, at this point, just bm so far, with karel as the default--to make strings and whatnot, but I have no idea if that's possible or anything, and no one in my class (or the teacher, because he only really knows what he has to teach) would know either. I didn't want to have more of:
private static final String nkarelLocation = "/icons/kareln.gif";
private static ImageIcon nkarel = null;
for each new icon I add, but based on the information at my disposal, I have no other way. Please help?