Is it possible to load an image from a variable?

So, let's say I have variable of any type which equals 1. Could I make an image named 1, then call it with the variable?

Tagged:

Answers

  • edited June 2016 Answer ✓

    Java demands that all variable names be known at compile time.
    You may try a HashMap<Integer,PImage> out though:
    https://Processing.org/reference/HashMap.html

  • Answer ✓
    int i = (int)random(5);
    PImage img = loadImage("filename_" + i + ".jpg");
    

    if you have image files called filename_0.jpg, filename_1.jpg ... filename_4.jpg then it'll load the relevant one. ("filename_" can be anything, including nothing)

    (if you have more than 10 images then it's best to zero-pad the names (filename_00.jpg) and format the variable accordingly using nf())

  • ayy chris wassup m8

Sign In or Register to comment.