Digital Bauhaus Vorkurs/Projekte/Form und Raum/Code6: Difference between revisions

From Medien Wiki
Line 1: Line 1:
== Objektorientiertes Gestaltungsprogramm ==
== Objektorientiertes Gestaltungsprogramm ==


<source lang="Java">Segmente Objekt1,Objekt2,Objekt3;
<source lang="Java" highlight="26">Segmente Objekt1,Objekt2,Objekt3;
//int[] Objekt = new int[6];


void setup() {
void setup() {

Revision as of 17:39, 23 January 2011

Objektorientiertes Gestaltungsprogramm

Segmente Objekt1,Objekt2,Objekt3;

void setup() {
  size(800, 800);
  smooth();
  noStroke();
  frameRate(5);
  colorMode(HSB,360,100,100);
  
  Objekt1 = new Segmente();
  Objekt2 = new Segmente();
  Objekt3 = new Segmente();
  
  Objekt1.x = 200;  Objekt2.x = 400; Objekt3.x = 600;
  Objekt1.y = 400;  Objekt2.y = 400; Objekt3.y = 400;
  
  Objekt1.seg = 0;  Objekt2.seg = 0; Objekt3.seg = 0; // Dreieck
  Objekt1.r = 100; Objekt2.r = 100; Objekt3.r = 100;
  Objekt1.g = 90; Objekt2.g = 90; Objekt3.g = 90; // Richtung nach oben
  
  Objekt1.h = 50;  Objekt2.h = 0; Objekt3.h = 220;
  Objekt1.s = 100; Objekt2.s = 100; Objekt3.s = 100;
  Objekt1.b = 100; Objekt2.b = 100; Objekt3.b = 100;
  
  Objekt1.mouseOver = false;  Objekt2.mouseOver = false; Objekt3.mouseOver = false;
  Objekt1.visible = false; Objekt2.visible = false; Objekt3.visible = false;
}

void draw() {
  background(0);
  
  if (keyPressed == true) {
    if (key == '1') {
      Objekt1.visible = true;
    }
    if (key == '2') {
      Objekt2.visible = true;
    }
    if (key == '3') {
      Objekt3.visible = true;
    }
  }
  
  if (Objekt1.visible == true) {
    Objekt1.maleSegmente();
  }
  if (Objekt2.visible == true) {
    Objekt2.maleSegmente();
  }
  if (Objekt3.visible == true) {
    Objekt3.maleSegmente();
  }
  
  if (mouseX >= Objekt1.x-Objekt1.r && mouseX <= Objekt1.x+Objekt1.r && mouseY >= Objekt1.y-Objekt1.r && mouseY <= Objekt1.y+Objekt1.r) {
    Objekt1.mouseOver = true;
  }else {
    Objekt1.mouseOver = false;
  }
  if (mouseX >= Objekt2.x-Objekt2.r && mouseX <= Objekt2.x+Objekt2.r && mouseY >= Objekt2.y-Objekt2.r && mouseY <= Objekt2.y+Objekt2.r) {
    Objekt2.mouseOver = true;
  }else {
    Objekt2.mouseOver = false;
  }
  if (mouseX >= Objekt3.x-Objekt3.r && mouseX <= Objekt3.x+Objekt3.r && mouseY >= Objekt3.y-Objekt3.r && mouseY <= Objekt3.y+Objekt3.r) {
    Objekt3.mouseOver = true;
  }else {
    Objekt3.mouseOver = false;
  }
}

void mouseDragged() {
  if (Objekt1.mouseOver == true) {
    Objekt1.x = mouseX;
    Objekt1.y = mouseY;
  }else if (Objekt2.mouseOver == true) {
    Objekt2.x = mouseX;
    Objekt2.y = mouseY;
  }else if (Objekt3.mouseOver == true) {
    Objekt3.x = mouseX;
    Objekt3.y = mouseY;
  }
}

void keyPressed() {
  if (key == CODED) {
    if (Objekt1.mouseOver == true) {
      if (keyCode == UP) {
        if (Objekt1.seg <= 101) {
          Objekt1.seg += 1;
        }
      }
      if (keyCode == DOWN) {
        if (Objekt1.seg >= 1) {
          Objekt1.seg -= 1;
        }
      } 
      if (keyCode == RIGHT) {
        if (Objekt1.h <= 361) {
          Objekt1.h += 1;
        }
      }
      if (keyCode == LEFT) {
        if (Objekt1.h >= 1) {
          Objekt1.h -= 1;
        }
      }
    }
    if (Objekt2.mouseOver == true) {
      if (keyCode == UP) {
        if (Objekt2.seg <= 101) {
          Objekt2.seg += 1;
        }
      }
      if (keyCode == DOWN) {
        if (Objekt2.seg >= 1) {
          Objekt2.seg -= 1;
        }
      } 
      if (keyCode == RIGHT) {
        if (Objekt2.h <= 361) {
          Objekt2.h += 1;
        }
      }
      if (keyCode == LEFT) {
        if (Objekt2.h >= 1) {
          Objekt2.h -= 1;
        }
      }
    }
    if (Objekt3.mouseOver == true) {
      if (keyCode == UP) {
        if (Objekt3.seg <= 101) {
          Objekt3.seg += 1;
        }
      }
      if (keyCode == DOWN) {
        if (Objekt3.seg >= 1) {
          Objekt3.seg -= 1;
        }
      } 
      if (keyCode == RIGHT) {
        if (Objekt3.h <= 361) {
          Objekt3.h += 1;
        }
      }
      if (keyCode == LEFT) {
        if (Objekt3.h >= 1) {
          Objekt3.h -= 1;
        }
      }
    }
  }
}

Segmente

class Segmente {
  
  float x; // X-Position
  float y; // Y-Position
  
  float seg; // Segmentzähler
  int r; // Radius
  float g; // Richtung
  
  boolean mouseOver; //Maus über dem Objekt
  boolean visible; //Objekt sichtbar
  
  int h,s,b; // Farbe HSB
  
  Segmente() {
    println("Segmente erstellt!");
  }
  
  void maleSegmente() {
    fill(h,s,b);
    maleSegmente(seg,r,g,x,y);
  }
  
  void maleSegmente(float seg,int r,float g,float x,float y) {
    float segmentzahl = 3 + seg;
    float angleStep = 360/segmentzahl;
    
    beginShape();
    vertex(x, y); 
    for (float angle=0; angle<=360; angle+=angleStep) {
      float vx = x + cos(radians(angle-g))*r;
      float vy = y + sin(radians(angle-g))*r;
      vertex(vx, vy);
    }
    vertex(x + cos(radians(360-g))*r, y + sin(radians(360-g))*r);
    endShape();
  }
}