Arbeiten mit Klassen: ein simples Malprogramm zur Erklärung: Unterschied zwischen den Versionen
Zeile 4: | Zeile 4: | ||
{{#ev:youtube|https://youtu.be/FBY7UdSXXqw}} | {{#ev:youtube|https://youtu.be/FBY7UdSXXqw}} | ||
+ | |||
+ | = Der Code aus den Videos zum selbst ausprobieren = | ||
+ | <pre> | ||
+ | Button farbButton = new Button("Rot", 1, 0, 0, 100, 50); | ||
+ | int farbe = 1; | ||
+ | int pinselStaerke = 1; | ||
+ | color col = color(255, 0, 0); | ||
+ | |||
+ | Button pinselButton = new Button("Pinsel: " + pinselStaerke, 2, farbButton.getWidth(), 0, 100, 50); | ||
+ | |||
+ | long buttonLastPressed = 0; | ||
+ | |||
+ | void setup() { | ||
+ | size(800, 400); | ||
+ | background(240); | ||
+ | } | ||
+ | |||
+ | void draw() { | ||
+ | |||
+ | if (mousePressed) { | ||
+ | strokeWeight(pinselStaerke); | ||
+ | stroke(col); | ||
+ | point(mouseX, mouseY); | ||
+ | } | ||
+ | |||
+ | buttonAuswerten(farbButton.anzeigen()); | ||
+ | buttonAuswerten(pinselButton.anzeigen()); | ||
+ | } | ||
+ | |||
+ | void buttonAuswerten(int b) { | ||
+ | switch(b) { | ||
+ | case -1: | ||
+ | // do nothing | ||
+ | break; | ||
+ | case 1: | ||
+ | // Knopf mit Id #1 ist gedrückt | ||
+ | if (millis() - buttonLastPressed>500) { | ||
+ | println("KNOPF 1 GEDRÜCKT !!"); | ||
+ | farbe++; | ||
+ | if (farbe>4) { | ||
+ | farbe = 1; | ||
+ | } | ||
+ | if (farbe==1) { | ||
+ | farbButton.setLabel("Rot"); | ||
+ | col = color(255,0,0); | ||
+ | } | ||
+ | if (farbe==2) { | ||
+ | farbButton.setLabel("Grün"); | ||
+ | col = color(0,255,0); | ||
+ | } | ||
+ | if (farbe==3) { | ||
+ | farbButton.setLabel("Gelb"); | ||
+ | col = color(255,255,0); | ||
+ | } | ||
+ | if (farbe==4) { | ||
+ | farbButton.setLabel("Radierer"); | ||
+ | col = color(240); | ||
+ | } | ||
+ | buttonLastPressed = millis(); | ||
+ | } | ||
+ | break; | ||
+ | case 2: | ||
+ | if (millis() - buttonLastPressed>500) { | ||
+ | println("KNOPF 2 GEDRÜCKT !!"); | ||
+ | pinselStaerke++; | ||
+ | if (pinselStaerke > 9) { | ||
+ | pinselStaerke = 1; | ||
+ | } | ||
+ | pinselButton.setLabel("Pinsel: "+ pinselStaerke); | ||
+ | buttonLastPressed = millis(); | ||
+ | } | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | class Button { | ||
+ | // Klassenvariablen | ||
+ | String label; | ||
+ | int id; | ||
+ | int xPos, yPos, width_, height_; | ||
+ | |||
+ | |||
+ | // Constructor | ||
+ | Button(String l, int id_, int x, int y, int w, int h) { | ||
+ | label = l; | ||
+ | id = id_; | ||
+ | xPos = x; | ||
+ | yPos = y; | ||
+ | width_ = w; | ||
+ | height_ = h; | ||
+ | } | ||
+ | |||
+ | int anzeigen() { | ||
+ | int r = -1; | ||
+ | if ((mouseX>=xPos) && (mouseX<=xPos+width_) && (mouseY>=yPos) && (mouseY<=yPos+height_)) { | ||
+ | stroke(204,102,0); | ||
+ | fill(255); | ||
+ | if (mousePressed) { | ||
+ | fill(120); | ||
+ | r = id; | ||
+ | } | ||
+ | } else { | ||
+ | stroke(0,0,0); | ||
+ | fill(255); | ||
+ | } | ||
+ | //fill(255); | ||
+ | strokeWeight(1); | ||
+ | rect(xPos, yPos, width_, height_); | ||
+ | fill(0); | ||
+ | textAlign(CENTER, CENTER); | ||
+ | text(label, xPos+width_/2, yPos+height_/2); | ||
+ | return r; | ||
+ | } | ||
+ | |||
+ | int getWidth() { | ||
+ | return width_; | ||
+ | } | ||
+ | |||
+ | int getHeight() { | ||
+ | return height_; | ||
+ | } | ||
+ | |||
+ | void setLabel(String l) { | ||
+ | label = l; | ||
+ | } | ||
+ | } | ||
+ | </pre> |
Version vom 21. März 2020, 20:48 Uhr
Der Code aus den Videos zum selbst ausprobieren
Button farbButton = new Button("Rot", 1, 0, 0, 100, 50); int farbe = 1; int pinselStaerke = 1; color col = color(255, 0, 0); Button pinselButton = new Button("Pinsel: " + pinselStaerke, 2, farbButton.getWidth(), 0, 100, 50); long buttonLastPressed = 0; void setup() { size(800, 400); background(240); } void draw() { if (mousePressed) { strokeWeight(pinselStaerke); stroke(col); point(mouseX, mouseY); } buttonAuswerten(farbButton.anzeigen()); buttonAuswerten(pinselButton.anzeigen()); } void buttonAuswerten(int b) { switch(b) { case -1: // do nothing break; case 1: // Knopf mit Id #1 ist gedrückt if (millis() - buttonLastPressed>500) { println("KNOPF 1 GEDRÜCKT !!"); farbe++; if (farbe>4) { farbe = 1; } if (farbe==1) { farbButton.setLabel("Rot"); col = color(255,0,0); } if (farbe==2) { farbButton.setLabel("Grün"); col = color(0,255,0); } if (farbe==3) { farbButton.setLabel("Gelb"); col = color(255,255,0); } if (farbe==4) { farbButton.setLabel("Radierer"); col = color(240); } buttonLastPressed = millis(); } break; case 2: if (millis() - buttonLastPressed>500) { println("KNOPF 2 GEDRÜCKT !!"); pinselStaerke++; if (pinselStaerke > 9) { pinselStaerke = 1; } pinselButton.setLabel("Pinsel: "+ pinselStaerke); buttonLastPressed = millis(); } break; } } class Button { // Klassenvariablen String label; int id; int xPos, yPos, width_, height_; // Constructor Button(String l, int id_, int x, int y, int w, int h) { label = l; id = id_; xPos = x; yPos = y; width_ = w; height_ = h; } int anzeigen() { int r = -1; if ((mouseX>=xPos) && (mouseX<=xPos+width_) && (mouseY>=yPos) && (mouseY<=yPos+height_)) { stroke(204,102,0); fill(255); if (mousePressed) { fill(120); r = id; } } else { stroke(0,0,0); fill(255); } //fill(255); strokeWeight(1); rect(xPos, yPos, width_, height_); fill(0); textAlign(CENTER, CENTER); text(label, xPos+width_/2, yPos+height_/2); return r; } int getWidth() { return width_; } int getHeight() { return height_; } void setLabel(String l) { label = l; } }