# Possible to use a variable as literal code? (from variables to ArrayList)

**URL:** https://discourse.processing.org/t/possible-to-use-a-variable-as-literal-code-from-variables-to-arraylist/40903
**Category:** Coding Questions
**Created:** [February 12, 2023, 4:55pm UTC](https://discourse.processing.org/t/possible-to-use-a-variable-as-literal-code-from-variables-to-arraylist/40903 "2023-02-12T16:55:16Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)
#### Post date: [February 12, 2023, 4:55pm UTC](https://discourse.processing.org/t/possible-to-use-a-variable-as-literal-code-from-variables-to-arraylist/40903/1 "2023-02-12T16:55:16Z")

</div>

so this is very complicated. im making an art software and i have many pgraphics, and i need to speak to the pgraphics. im wondering if there is any method, i could make it so it doest always is:

```auto
if(currentlayer == 1) {
  layer1.paint();
}
if(currentlayer == 2) {
  layer2.paint();
}
if(currentlayer == 3) {
  layer3.paint();
}

```

i need to do it for hundreds of layers. is there a way to make something like

```auto
   AsCode(currentlayer).paint();

```

if this is possible with complicated stuff, or a library please help me!  
it is not possible to make it switch and only use one pgraphics. performance is not a problem!

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [February 12, 2023, 5:07pm UTC](https://discourse.processing.org/t/possible-to-use-a-variable-as-literal-code-from-variables-to-arraylist/40903/2 "2023-02-12T17:07:08Z")

</div>

```auto
for(int i=0;i<layers;i++){
  layer.get(i).paint;
}

```

Use for loops and something to store you layers. ArrayList, or array, or anything else that is appropriate.

enhanced for loop

```auto
for(Layers l layers){
  l.paint();
}

```

---

<div class="post-metadata">

### Author: ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)
#### Post date: [February 12, 2023, 5:24pm UTC](https://discourse.processing.org/t/possible-to-use-a-variable-as-literal-code-from-variables-to-arraylist/40903/3 "2023-02-12T17:24:27Z")

</div>

OMG you are a certified genius! thank you so much!

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 12, 2023, 5:30pm UTC](https://discourse.processing.org/t/possible-to-use-a-variable-as-literal-code-from-variables-to-arraylist/40903/4 "2023-02-12T17:30:30Z")

</div>

You can also use `currentLayer` as an index for your ArrayList :

`layer.get(currentLayer).paint();`

Instead of using the `switch()` command

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 12, 2023, 5:31pm UTC](https://discourse.processing.org/t/possible-to-use-a-variable-as-literal-code-from-variables-to-arraylist/40903/5 "2023-02-12T17:31:47Z")

</div>

> [@paulgoux](#):
>
> `for(Layers l layers){`

`for(Layers l : layers){`

With `:`

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [February 12, 2023, 5:53pm UTC](https://discourse.processing.org/t/possible-to-use-a-variable-as-literal-code-from-variables-to-arraylist/40903/6 "2023-02-12T17:53:48Z")

</div>

Hello @Aryszin,

I see that there are responses already…

I was working on this and will share nevertheless:

```auto
PGraphics [] paintings = new PGraphics[500];

void setup()
  {
  size(800, 800);
  background(128);
  for(int i = 0; i<paintings.length; i++)
    {
    paintings[i] = createGraphics(400, 400);
    }
    
   for(int i = 0; i<paintings.length; i++)
    {
    paintings[i].beginDraw();
    paintings[i].clear();
    paintings[i].fill(random(256), random(256), random(256));
    paintings[i].circle(choice(20, 400-20), choice(20, 400-20), 40);
    paintings[i].endDraw();
    }
  
  // for() loop
  for(int i = 0; i<paintings.length; i++)
    {
    image(paintings[i], 0, 0);  
    }
  
  // Enhanced for loop
  for(PImage p : paintings)
    {
    image(p, 400, 0);  
    }
  }
    
void draw()
  {
  int num = ((frameCount-1)%100);
  image(paintings[num], 0, 400);
  }

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/e/8e9da11cecca047e4170b71676b24aad7f0071c0.png)

I may have over-exampled a bit.

`:)`

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 12, 2023, 9:27pm UTC](https://discourse.processing.org/t/possible-to-use-a-variable-as-literal-code-from-variables-to-arraylist/40903/7 "2023-02-12T21:27:48Z")

</div>

Or

```auto
class Layer { 
    
    PGraphics pg; 
    
    void paint() {
        image(pg, 400, 0);  
    } // method 
    
} // class

```

**before setup()**

`ArrayList<Layer> layers = new ArrayList();`

**in setup()**

```auto
.....
layers.add(newPG);

```

**in draw()**

`layer.get(currentLayer).paint();`
