Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 18-3: Creating objects from a text file

Bubble[] bubbles;

void setup() {
  size(200,200);
  smooth();
  
  // Load text file as an array of Strings
  String[] data = loadStrings("data.txt");
  
  // The size of the array of Bubble objects is determined by the total number of lines in the text file.
  bubbles = new Bubble[data.length]; 
  for (int i = 0; i < bubbles.length; i ++ ) {
    // Each line is split into an array of floating point numbers.
    float[] values = float(split(data[i], "," )); 
    // The values in the array are passed into the Bubble class constructor.
    bubbles[i] = new Bubble(values[0],values[1],values[2]); 
  }
}

void draw() {
  background(255);
  
  // Display and move all bubbles
  for (int i = 0; i < bubbles.length; i ++ ) {
    bubbles[i].display();
    bubbles[i].drift();
  }

}
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 18-3: Creating objects from a text file

// A Class to describe a "Bubble"

class Bubble {
  float x,y;
  float diameter;
  float speed;
  float r,g;
  
  // The constructor initializes color and size
  // Location is filled randomly
  Bubble(float r_, float g_, float diameter_) {
    x = random(width);
    y = height;
    r = r_;
    g = g_;
    diameter = diameter_;
  }
  
  // Display the Bubble
  void display() {
    stroke(0);
    fill(r,g,255,150);
    ellipse(x,y,diameter,diameter);
  }
  
  // Move the bubble
  void drift() {
    y += random(-3,-0.1);
    x += random(-1,1);
    if (y < -diameter*2) {
      y = height + diameter*2;
    }
  }

}
88,149,22
193,78,8
90,152,56
136,18,37
47,2,55
36,142,57
10,61,31
9,121,49
156,60,12
71,200,21
  • Nathaniel Stephens

    the “float speed” variable in the Bubble class can be omitted…..doesn’t do anything.