Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

processing shapesSpawn

By: a guest on Oct 6th, 2011  |  syntax: None  |  size: 1.08 KB  |  views: 301  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. List shapes = new ArrayList();
  2.  
  3. void setup()
  4. {
  5.  size(800, 800);
  6.  smooth();
  7.  for(int i = 0; i < 100; i++)
  8.  if(i % 2 == 0)
  9.  shapes.add(new Square());
  10.  else
  11.  shapes.add(new Circle());
  12. }
  13.  
  14. void draw()
  15. {
  16.  background(255, 255, 255);
  17.  Iterator i = shapes.iterator();
  18.  while(i.hasNext())
  19.  {
  20.  ((Shape)i.next()).drawAndStep();
  21.  }
  22. }
  23.  
  24. abstract class Shape
  25. {
  26.  float x, y, r, xdir, ydir;
  27.  color f, s;
  28.  Shape()
  29.  {
  30.  x = random(width/4, width/2);
  31.  y = random(height/4, height/2);
  32.  r = random(20, 40);
  33.  xdir = random(-10, 10);
  34.  ydir = random(-10, 10);
  35.  color rc = color((int)random(255), (int)random(255), (int)random(255));
  36.  f = color(rc, 100);
  37.  s = color(rc, 200);
  38.  }
  39.  
  40.  void drawAndStep()
  41.  {
  42.  fill(f);
  43.  stroke(s);
  44.  strokeWeight(2);
  45.  draw();
  46.  x+=xdir;
  47.  y+=ydir;
  48.  if(x + r/2 >= width || x - r/2 <= 0)
  49.  xdir *= -1;
  50.  if(y + r/2 >= height || y - r/2 <= 0)
  51.  ydir *= -1;
  52.  }
  53.  
  54.  abstract void draw();
  55. }
  56.  
  57. class Square extends Shape
  58. {
  59.  void draw()
  60.  {
  61.  rectMode(CENTER);
  62.  rect(x, y, r, r);
  63.  }
  64. }
  65.  
  66. class Circle extends Shape
  67. {
  68.  void draw()
  69.  {
  70.  ellipse(x, y, r, r);
  71.  }
  72. }
  73.  
  74.