Das Raumschiff-Beispiel mit einem Button kombiniert

Aus DDSWiki
Wechseln zu: Navigation, Suche

Raumbutton.gif

Code zum selbst Ausprobieren

int winkel = 180;
int x = 150;
int y = 150;

Button b = new Button("r", 1, 0, 0, 30, 30, 12, 0);

Raumschiff schiff = new Raumschiff(x,y,15);


void setup() {
  background(100);
  size(400,400);
  schiff.setScale(2);
}

void draw() { 
  background(100);
  //schiff.setPosition(x,y);
  schiff.setRotatation(winkel);
  schiff.display();
  int b_ = b.showAndCheckButton();
  if (b_!=-1) {
    winkel = winkel + 10;
  }  
}


void keyPressed() {
  if (key == 'l') {
    winkel = winkel + 10;
  }
  if (key == 'k') {
    winkel = winkel - 10;
  }    
  if (key == 'q') {
    y = y - 1;
  }  
  if (key == 'a') {
    y = y + 1;
  }   
  
}

class Raumschiff {
  // Wir machen unser eigenes Dreieck und verwenden nicht den Befehl triangle(x1,y1,x2,y2,x3,y3) denn 
  // So müssen wir nicht mühsam den Mittelpunkt berechnen
  int x, y; // Mittelpunkt
  int hoeheSchiff, breiteSchiff;
  float scale_ = 1;
  int rotation = 0;
  
  Raumschiff(int x_, int y_, int h) {
    x = x_; y = y_; hoeheSchiff = h;
  }
  
  void setScale(float s) {
    scale_ = s;
  }
  
  void setRotatation(int r) {
    rotation = r;
  }
  
  void setPosition(int x_, int y_) {
    x = x_; y = y_;
  }
  
  void calculateNextPosition() {
    
  }
  
  void display() {
    pushMatrix();
    translate(x,y);
    scale(scale_);
    rotate(radians(rotation));
    translate(-x,-y);
    triangle(x-hoeheSchiff, y-hoeheSchiff, x+hoeheSchiff, y-hoeheSchiff, x, y+hoeheSchiff);  
    //scale(scale_);
    popMatrix();

    //translate(x,y);
  }
}


// ===================KLASSE BUTTON=================================
class Button {

  // Interne Variablen der Klasse. Man könnte auch das Schlüsselwort 'private' voranstellen
  String label;
  int id;
  int xPos, yPos, width_, height_;
  int textSize_;
  int nextScreen;
  boolean buttonIsTransparant = false;

  // Constructor 
  Button(String l, int i, int x, int y, int w, int h, int ts, int s) {
    label = l; // Beschriftung des Knopfes
    id = i; // Eine eindeutige ID für den Knopf
    xPos = x; // Für die Positionierung und Dimensionierung des Knopfes
    yPos = y; 
    width_ = w; 
    height_ = h;
    textSize_ = ts;
    nextScreen = s; // Zu welchem Screen führt der Button?
  }

  int showAndCheckButton() {
    push();
    int r = -1;
    if ((mouseX>=xPos) && (mouseX<=xPos+width_) && (mouseY>=yPos) && (mouseY<=yPos+height_)) {
      strokeWeight(1);
      stroke(204, 102, 0);
      fill(255);
      if (mousePressed) {
        r = nextScreen;
        fill(120);
      }
    } else {
      strokeWeight(1);
      stroke(0, 0, 0);
      fill(255);
    }  
    if (!buttonIsTransparant) {
      rect(xPos, yPos, width_, height_);
      fill(0);
      textAlign(CENTER, CENTER);
      textSize(textSize_);
      text(label, xPos+width_/2, yPos+height_/2);
      
    }
    pop();
    return r;
  }
  
  int getWidth() {
    return width_;
  }
  
  int getHeight() {
    return height_;
  }
  
  void makeTransparant(boolean t) {
    buttonIsTransparant = t;
  }
  
  void setLabel(String l) {
    label = l;
  }
}