User:Pleun/openaanbod/processing 2: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "== // Sketch - object oriented == box b; ArrayList<box> boxes = new ArrayList<box>(); boolean record = false; void setup() { size(600, 600); b = new box(100, 100, 25,...")
 
No edit summary
 
Line 1: Line 1:
== // Sketch - object oriented ==
== // Sketch - object oriented ==


box b;
box b;


ArrayList<box> boxes = new ArrayList<box>();
ArrayList<box> boxes = new ArrayList<box>();


boolean record = false;
boolean record = false;


void setup() {
void setup() {
  size(600, 600);
  size(600, 600);


   b = new box(100, 100, 25, 25);
   b = new box(100, 100, 25, 25);
Line 28: Line 28:
     boxes.add(b);
     boxes.add(b);
   }
   }
}
}


void draw () {
void draw () {
   //b.paint();
   //b.paint();


Line 42: Line 42:
   }
   }
    
    
}
}


void keyPressed() {
void keyPressed() {
  if (key == 's') {
  if (key == 's') {
   record = !record;
   record = !record;
}  
  }  
}
}




Line 55: Line 55:




public class box {
public class box {


   int x = 10;  // properties
   int x = 10;  // properties
Line 102: Line 102:
   }
   }
    
    
}
}




Line 108: Line 108:
== circle ==
== circle ==


public class circle extends box {
public class circle extends box {


   public circle() {
   public circle() {
Line 125: Line 125:
     noStroke();
     noStroke();
   }
   }
}
}

Latest revision as of 14:27, 29 October 2015

// Sketch - object oriented

box b;
ArrayList<box> boxes = new ArrayList<box>();
boolean record = false;
void setup() {
  size(600, 600);
 b = new box(100, 100, 25, 25);
 int v = 255;
 //int x = 0;
 //int y = 10;
 background (0);
 for (int i = 0; i <1000; i++) {  // i++ = i + 1, i-- = i - 1 
   int x = (int)random(width-25);
   int y = (int)random(height-25);
   color c = color (v, v, v);
   v = (int)random(255);
   box b = new circle(x, y, 25, 25);
   b.setColor(c);
   b.setSpeed((int)random(5,10), (int)random(5,10));
   boxes.add(b);
 }
}
void draw () {
 //b.paint();
 for (int i = 0;i < boxes.size(); i++) {
   boxes.get(i).paint();
   boxes.get(i).move();
 }
 
 if(record) {
  saveFrame ("frames/#####.png"); 
 }
 
}
void keyPressed() {
if (key == 's') {
  record = !record;
 } 
}


box

public class box {
 int x = 10;  // properties
 int y = 10;
 int w = 50;
 int h = 50;
 color c = color (255);
 
 int speedX = 0;
 int speedY = 0;
 public box () {
 }
 public box(int x, int y, int w, int h) {
   this.x = x;
   this.y = y;
   this.w = w;
   this.h = h;
 }
 void setColor(color c) {
   this.c = c;
 }
 void setSpeed (int speedX, int speedY) {
   this.speedX = speedX;
   this.speedY = speedY;
 }
 void move() {
   x = x + speedX;
   if (x > width || x<0) {
     speedX = -speedX;
   }
   y = y + speedY;
   if (y > width || y<0) {
     speedY = -speedY;
   }
 }
 void paint() {
   fill(c);
   rect(x, y, w, h);
   noStroke();
 }
 
}


circle

public class circle extends box {
 public circle() {
 }
 public circle (int x, int y, int w, int h) {
   this.x = x;
   this.y = y;
   this.h = h;
   this.w = w;
 }
 void paint() {
   fill(c);
   ellipse(x, y, w, h);
   noStroke();
 }
}