Passing variables with constructor
in
Programming Questions
•
1 year ago
Probably another stupid question. I'm trying to combine several sketches in one running program and using "import java.lang.reflect.Constructor". I downloaded the method, but I can't open different image in each sketch.
Main code is :
import java.lang.reflect.Constructor;
final int W = 940, H = 480, BG = #000000, fps = 30, seconds = 6;
boolean export = false;// true je m'enregistre des images
PApplet[] sketches;
String[] names;
int s;
public PFont font1;
public PImage img;
void setup() {
size(W, H);
Class[] classes = getClass().getDeclaredClasses();
int n = classes.length;
sketches = new PApplet[n];
names = new String[n];
for(int i=0; i<n; i++) {
try {
Constructor c = classes[i].getDeclaredConstructor(getClass());
sketches[i] = (PApplet) c.newInstance(this);
sketches[i].init();
names[i] = split(c.getName(), "$")[1];
} catch (Exception e) {}
}
font1 = createFont("arial",12);
frameRate(fps);
}
void draw() {
frame.setTitle(int(frameRate) + " fps"+ int(frameCount)+"Fra Count");
if(frameCount % (seconds * fps) == 1) {
// stop running sketch
if(frameCount>1) {
println("stopping " + names[s]);
sketches[s].stop();
s = (s+1) % sketches.length;
}
// start next sketch
println("starting " + names[s]);
sketches[s].run();
}
sketches[s].handleDraw();
PImage img = sketches[s].get();
img.loadPixels();
image(img, 0, 0);
if(export && frameCount <= fps*seconds*sketches.length) saveFrame("showreel-#####.png");
}
The Tab with sketches :
class Sketch1 extends PApplet {
//img =loadImage("MyPortrait.jpg");
image(img,0,0);
}
//=============================================================
class Sketch2 extends PApplet {
void setup() {
size(W, H, P3D);
}
void draw() {
background(BG);
lights();
noStroke();
translate(W/2, H/2, 0);
sphere(100);
}
}
1