Aryszin
February 12, 2023, 4:55pm
1
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:
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
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!
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
for(Layers l layers){
l.paint();
}
2 Likes
Aryszin
February 12, 2023, 5:24pm
3
OMG you are a certified genius! thank you so much!
1 Like
Chrisir
February 12, 2023, 5:30pm
4
You can also use currentLayer
as an index for your ArrayList :
layer.get(currentLayer).paint();
Instead of using the switch()
command
1 Like
glv
February 12, 2023, 5:53pm
6
Hello @Aryszin ,
I see that there are responses already…
I was working on this and will share nevertheless:
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);
}
I may have over-exampled a bit.
:)
Chrisir
February 12, 2023, 9:27pm
7
Or
class Layer {
PGraphics pg;
void paint() {
image(pg, 400, 0);
} // method
} // class
before setup()
ArrayList<Layer> layers = new ArrayList();
in setup()
.....
layers.add(newPG);
in draw()
layer.get(currentLayer).paint();
1 Like