Brandon Cryderman
Brandon Cryderman

Reputation: 11

What is a cleaner, more elegant way to initialize all of these?

I am naming all of these one by one. Is there a method that takes less space?

public class Matt{
    PImage matt, 
    imgLS1, imgLS2, imgLS3, imgRS1, imgRS2, imgRS3,
    imgLSB1, imgLSB2, imgLSB3, imgRSB1, imgRSB2, imgRSB3,
    imgLW1, imgLW2, imgLW3, imgRW1, imgRW2, imgRW3,
    imgLWB1, imgLWB2, imgLWB3, imgRWB1, imgRWB2, imgRWB3;
    public Matt(){    
        imgLS1  = loadImage("./Images/Matt/MattLS1.png");
        imgLS2  = loadImage("./Images/Matt/MattLS2.png");
        imgLS3  = loadImage("./Images/Matt/MattLS3.png");
        imgRS1  = loadImage("./Images/Matt/MattRS1.png");
        imgRS2  = loadImage("./Images/Matt/MattRS2.png");
        imgRS3  = loadImage("./Images/Matt/MattRS3.png");
        imgLSB1 = loadImage("./Images/Matt/MattLSB1.png");
        imgLSB2 = loadImage("./Images/Matt/MattLSB2.png");
        imgLSB3 = loadImage("./Images/Matt/MattLSB3.png");
        imgRSB1 = loadImage("./Images/Matt/MattRSB1.png");
        imgRSB2 = loadImage("./Images/Matt/MattRSB2.png");
        imgRSB3 = loadImage("./Images/Matt/MattRSB3.png");
        imgLW1  = loadImage("./Images/Matt/MattLW1.png");
        imgLW2  = loadImage("./Images/Matt/MattLW2.png");
        imgLW3  = loadImage("./Images/Matt/MattLW3.png");
        imgRW1  = loadImage("./Images/Matt/MattRW1.png");
        imgRW2  = loadImage("./Images/Matt/MattRW2.png");
        imgRW3  = loadImage("./Images/Matt/MattRW3.png");
        imgLWB1 = loadImage("./Images/Matt/MattLWB1.png");
        imgLWB2 = loadImage("./Images/Matt/MattLWB2.png");
        imgLWB3 = loadImage("./Images/Matt/MattLWB3.png");
        imgRWB1 = loadImage("./Images/Matt/MattRWB1.png");
        imgRWB2 = loadImage("./Images/Matt/MattRWB2.png");
        imgRWB3 = loadImage("./Images/Matt/MattRWB3.png");
    }
}

Upvotes: 1

Views: 60

Answers (2)

Ted Hopp
Ted Hopp

Reputation: 234795

Since the "LS", etc., seem to have semantic meaning, I'd suggest a variation of the solution by @dasblinkenlight that uses an enum:

final int N_FILES = 3; // files/position -- could also be a variable
enum Position {
    LS, RS, LSB, RSB, LW, RW, LWB, LRB
}

Map<Position, String[]> files = new EnumMap<>(Position.class);
for (Position pos : Position.values()) {
    String[] posFiles = new String[N_FILES];
    files.put(pos, posFiles);
    for (int i = 1; i <= N_FILES; ++i) {
        posFiles[i-1] = "./Images/Matt/Matt" + pos.name() + i + ".png";
    }
}

Then you can access any element with code like this:

Position p = RS; // or any other value
int index = 0; // 0..(N_FILES-1), corresponding to suffixes 1..N_FILES
String fileName = files.get(p)[i];

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

Put your images in a Map<String,PImage>, organizing the map by image suffix. As far as accessing the images is concerned, this approach may be slightly less convenient/efficient than using variables directly, but it will save you a lot of space:

static final String[] suffixes = new String[] {"LS1", "LS2", "LS3", ..., "RWB3"};
Map<String,PImage> images = new HashMap<String,PImage>();
public Matt() {
    for (String suffix : suffixes) {
        PImage image = loadImage("./Images/Matt/Matt"+suffix+".png");
        images.put(suffix, image);
    }
}

Upvotes: 3

Related Questions