| Setup-Draw |
// globale Variabeln
// setup() wird beim Start einmal ausgeführt
void setup() {
...
}
// draw() wird pro Sek. frameRate-mal wiederholt
void draw() {
...
}
|
lauffähiges Beispiel:
void setup() {
size(200, 200);
frameRate(30);
strokeWeight(2);
}
void draw() {
background(204);
stroke(random(0, 180));
line(0,0, width, random(height));
}
|
| Key-Mouse-Event |
// globale Variabeln
// setup() wird beim Start einmal ausgeführt
void setup() {
...
}
// draw() wird pro Sek. frameRate-mal wiederholt
void draw() {
...
}
// wird ausgeführt, wenn die Maus gedrückt wird
void mousePressed() {
...
}
// wird ausgeführt, wenn eine Taste gedrückt wird
void keyPressed() {
...
}
|
lauffähiges Beispiel:
color hintergrund;
boolean schalter = true;
void setup() {
size(200, 200);
frameRate(30);
strokeWeight(2);
textAlign(CENTER);
textSize(14);
hintergrund = color(30, 230, 30);
}
void draw() {
background(hintergrund);
stroke(random(100, 255), 20, 20);
if (schalter == true) {
line(0, 0, width, random(height));
}
else {
line(0, random(height), width, 0);
}
text("Maus-Links | Maus-Rechts", width / 2, height-25);
text("l = links | r = rechts", width / 2, height-5);
}
void mousePressed() {
if (mouseButton == LEFT) {
hintergrund = color(30, 230, 30);
}
else {
hintergrund = color(30, 30, 230);
}
}
void keyPressed() {
if (key == 'l' || key == 'L') {
schalter = true;
}
if (key == 'r' || key == 'R') {
schalter = false;
}
}
|
| doppelte For-Schleife |
for (int j = 0; j < anzZeilen; j++) {
for (int i = 0; i < anzSpalten; i++) {
...
}
}
|
lauffähiges Beispiel:
int[][] meinArray = {
{ 230, 190, 90, 0, 255 },
{ 255, 80, 190, 130, 30 },
{ 210, 20, 150, 210, 255 }
};
int seite = 60;
int anzSpalten = 5;
int anzZeilen = 3;
void setup() {
size(anzSpalten * seite, anzZeilen * seite);
for (int j = 0; j < anzZeilen; j++) {
for (int i = 0; i < anzSpalten; i++) {
fill(meinArray[j][i]);
rect(i*seite, j*seite, seite, seite);
}
}
}
|