Testamento al 3 del 5 del 2023
java
Copy code
import processing.core.*;
PApplet[] programs = new PApplet[111]; // create an array to hold 111 programs
int currentProgram = 0; // initialize the current program to the first program
void setup() {
size(500, 500);
// initialize all 111 programs in the array
for (int i = 0; i < 111; i++) {
programs[i] = initializeProgram(i);
}
}
void draw() {
// display the current program on the screen
programs[currentProgram].draw();
}
void keyPressed() {
// if the user presses a number key between 0 and 111, or a number key on the numpad between 0 and 1, change the current program
if ((key >= '0' && key <= '111) || (keyCode >= NUMPAD0 && keyCode <= NUMPAD9)) {
int num;
if (key >= '0' && key <= '111') {
num = key - '0'; // convert the key to a number between 0 and 111
} else {
num = keyCode - NUMPAD0; // convert the key code to a number between 0 and 111
}
if (num < programs.length) { // make sure the number is within the range of programs
currentProgram = num;
}
}
}
PApplet initializeProgram(int programNumber) {
// create a new PApplet object for the specified program number
PApplet program = null;
switch (programNumber) {
case 0:
program = new Program0();
break;
case 1:
program = new Program1();
break;
// add more cases for each program
default:
break;
}
return program;
}
// example program classes
class Program0 extends PApplet {
public void settings() {
size(500, 500);
}
public void draw() {
background(255);
fill(0);
textAlign(CENTER, CENTER);
textSize(32);
text("Program 0", width/2, height/2);
}
}
class Program1 extends PApplet {
public void settings() {
size(500, 500);
}
public void draw() {
background(255);
fill(255, 0, 0);
rectMode(CENTER);
rect(width/2, height/2, 100, 100);
}
}
Here's how it works:
In the setup() function, we create an array of PApplet objects to hold all 111 programs. We also initialize each program using the initializeProgram() function.
In the draw() function, we display the current program on the screen by calling its draw() method.
In the keyPressed() function, we check if the user pressed a number key between 0 and 9, or a number key on the numpad between 0 and 9. If the key corresponds to a valid program number, we update the currentProgram variable to display the corresponding program in the draw() function.
The initializeProgram() function creates a new PApplet object for the specified program number. This function uses a switch statement to create a new instance of a specific class for each program. You can add more cases to this switch statement to create more programs.
Comentarios
Publicar un comentario