Search code examples
javaarraysimageswingimageicon

Using an String array element as a variable name?


I'm trying to import many pictures into a visible grid line to appear as a bar (of health, to be exact) that will decrease (adds a white image in place of bar's placement) when damage is taken. So I decide instead of writing out chucks of code for each of the 10 possible health point (not to mention there are other player stats that will be done in the same format), I decided upon scraping together a "for" loop with two if-else statements to attempt a loop to fill each grid element as long as the health value is greater than the "for" loop's "i". There is a slightly different image if the player happens to max their health at 10, hence the second "if-else" loop needed.<p> My main question concerns the name of my loop, for I used an array to act as a kind of holder for string names that I wished to become the ImageIcon names, yet I've failed to either arrange the code properly or find a source explaining how one would go about using an array string name as the name for an ImageIcon.

Here is the loops and data:<p>

String[] array1 ={"hOne","hTwo","hThree","hFour","hFive","hSix","hSeven","hEight","hNine","hTen"};
    String tempY = " ", tempN = " ", tempF = " ", tempNF = " ";
        //row 1, health
        statsPanel.add(stat1);
    for (int i=0; i<=9; i++){
        if ((i+1)<heal){
        tempY=array1[i];
        ImageIcon tempY = new ImageIcon("C:\\Users\\Kunkle\\hea.png");  
        ColorPanel tempYz = new ColorPanel(Color.black, tempY);
        statsPanel.add(tempYz);
            }
        else {
        tempN=array1[i];    
        ImageIcon tempN = new ImageIcon("C:\\Users\\Kunkle\\non.png");  
        ColorPanel tempNz = new ColorPanel(Color.black, tempN);
        statsPanel.add(tempNz);
            }
        if (i==8 && heal==10){
        tempF=array1[i];
        ImageIcon tempF = new ImageIcon("C:\\Users\\Kunkle\\shea.png");  
        ColorPanel tempFz = new ColorPanel(Color.black, tempF);
        statsPanel.add(tempFz);
            }
        else {
        tempNF=array1[i];   
        ImageIcon tempNF = new ImageIcon("C:\\Users\\Kunkle\\shea.png");  
        ColorPanel tempNFz = new ColorPanel(Color.black, tempNF);
        statsPanel.add(tempNFz);
            }
}

Everything has been properly declared variable and import-wise, and the only 3 errors I get is the four lines where the array element is is stated after the first "ImageIcon" in each declaration of a new image. Does there happen to be a way to keep code short with a "for" loop (or several) but still bring in an image under a new name for every pass?


Solution

  • You cannot do what you are attempting to do. Java is not a dynamic language that can substitute variable values for names, a la Perl where $x="a"; $$x=3; results in variable $a having the value 3.

    The closest thing you can do is use a Map<String,ImageIcon> to associate strings with objects. If you want to associate both an ImageIcon and ColorPanel with a name you would need a wrapper object to hold references to both, and you have a Map<String,MyWrapper> instead.

    I would provide a code sample but that would require seeing the code of statsPanel, which you haven't provided. My guess is statsPanel would be (or contain) the Map<> I mentioned above.