Loading...
Logo
Processing Forum
hello processing geniuses, 

im working on a logo that involves a harmonious group of particles that revolves around a letterform; i need some help with the particles. so heres the gist of it: each particle is generated by a tweet that consist of six different keywords: air and space, creative court, ecosystems, innovation lab, rain forest, and world of life. each keyword is represented by different colored particles, as shown below:
air and space tweet : dark purple particle
creative court tweet: orange particle
eco systems tweet : blue  particle
innovation lab tweet : pink  particle
rain forest tweet : green  particle
world of life tweet : red  particle
the differentiation of the colors, once the particles are generated by twitter feeds, allow the user to see just about which topic is most popular; if  green particles seem to populate the space more so than other colors, they get a clearer sense that the topic of rain forests is most talked about.
heres my question: for my set of codes, i was wondering if you guys can help me write how i can make this function possible.
also, i was wondering if you guys can help me how to tie in the twitter feed function to my code.
for now, the particles can be generated by clicking the screen.

for the processing code, i have set two tabs to reduce the clustering of codes.

if you prefer the actual code, you can download it here:  http://www.mediafire.com/?zqach4y6p6vb4qj

note: the gradient function is very important, so the twitter function is powered by twitter4j.

thanks! much appreciated


TAB ONE CODES:

ArrayList particles;
PShape a;
Twitter twitter;
Query q;
/*Mover[] movers = new Mover[190];
 Mover[] movers2 = new Mover[25];
 Mover[] movers3 = new Mover[7];*/
color col;
String[] terms = {
  "airandspace", "creativecourt", "ecosystems", "innovationlab", "rainforest" , "worldoflife"
};
int[] hues = {
  30, 25, 80, 60, 80
};
int index = 0;


//VOID SETUP//////////////////// 
void setup() {
  size(800, 800);
  smooth();
  background(0);
  color(HSB, 100);
  frameRate(70);

  //twitter stuff
  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey("PLmGEQtruhjtEDryFyx0rA");
  cb.setOAuthConsumerSecret("yN5ubhMXKkJjRF151m2M2awWQXmkaQzMgOkksyLtI");
  cb.setOAuthAccessToken("317405665-ojRo56DtA5KFvfujk39EM60g9Tk75DrE0f4VvO2N");
  cb.setOAuthAccessTokenSecret("oo2LazqjdMzYtiFUTlQWA3vzmifTOxUA3C8xzB11E");

  twitter = new TwitterFactory(cb.build()).getInstance();


  /*
  try{
   QueryResult result = twitter.search(q);
   ArrayList tweets = (ArrayList) result.getTweets();
   Tweet t = (Tweet) tweets.get(0);
   particles = new ArrayList();
   particles.add(new Mover(4, .07));
   println(t.getText());
   }catch(TwitterException te){
   
   }*/

  particles = new ArrayList();
  col = color(hues[index], random(80, 100), random(60, 90));
  particles.add(new Mover(col, 4, .07));
  //Mover p = (Mover)particles.get(0);
  //println(p.msg);

  a = loadShape("mos.svg");
  /*
  for (int i = 0; i < movers.length; i++) {
   movers[i] = new Mover(4, .07);
   }
   for (int i = 0; i < movers2.length; i++) {
   movers2[i] = new Mover(4, .13);
   }
   for (int i = 0; i < movers3.length; i++) {
   movers3[i] = new Mover(4, .2);
   }*/
}

void draw()
  noStroke();
  fill(255);
  rect(0, 0, width, height);

  // create a timer to call createParticles();

  for (int i=0; i<particles.size(); i++)
  {
    Mover m = (Mover) particles.get(i); 
    m.checkEdges();
    m.update();
    m.display();

    if (m.finished()) {
      // Items can be deleted with remove()
      particles.remove(i);
    }
  }
}

void mousePressed() {
  createParticles();
}

void createParticles()
{
  index = int(random(terms.length));
  col = color(hues[index], random(80, 90), random(90, 100));
  particles.add(new Mover(col, 4, .07));
}


/*
//VOID DRAW////////////////////
 void draw() {
 noStroke();
 fill(255);
 rect(0, 0, width, height);
/*
 for (int i = 0; i < movers.length; i++) {
 movers[i].update();
 movers[i].checkEdges();
 if (!finished) movers[i].display(); 
 movers[i].fadeOut();
 }
 for (int i = 0; i < movers2.length; i++) {
 movers2[i].update();
 movers2[i].checkEdges();
 if (!finished) movers2[i].display();
 movers2[i].fadeOut();
 }
 for (int i = 0; i < movers3.length; i++) {
 movers3[i].update();
 movers3[i].checkEdges();
 if (!finished) movers3[i].display();
 movers3[i].fadeOut();
 }*/
//shape (a, -200, -200, 1200, 1200);
//}




TAB TWO CODES:

class Mover {

  PVector location;
  PVector velocity;
  PVector acceleration;
  //fade out funtion; the larger the number, the longer////////////////////
  float life = 3550;
  float topspeed;
  int size;
  float multiplier;
  color col;
  String msg;

  Mover(color tcol, int xsp, float xmu) {
    size = int(random(4, 1));
    location = new PVector(random(width), random(height));
    velocity = new PVector(random(width), random(height));
    topspeed = xsp;
    multiplier = xmu;
    msg = getMsg();
    int x = int(random(0, 255));

    float c = random(0, 80);
    float m = random(0, 55);
    float y = random(0, 0);
    float k = random(0, 30);
    //col = convert(c, m, y, k);
    col = tcol;
  }

  //fade out funtion////////////////////
  boolean finished() {
    // Balls fade out
    life-=10;
    if (life < 0) {
      return true;
    } 
    else {
      return false;
    }
  }

  void fadeOut() {
    // Balls fade out
    life--;
  }

  //VOID UPDATE////////////////////
  void update() {
    PVector mouse = new PVector(width/2, height/2);
    PVector dir = PVector.sub(mouse, location);
    dir.normalize();
    dir.mult(multiplier);
    acceleration = dir;

    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity);
  }

  //COLOR//9*I;//////////////////
  color convert(float c, float m, float y, float k) {
    c = c / 100;
    m = m / 100;
    y = y / 100;
    k = k / 100;

    c = ( c * ( 1 - k ) + k );
    m = ( m * ( 1 - k ) + k );
    y = ( y * ( 1 - k ) + k );

    float r = ( 1 - c ) * 255;
    float g = ( 1 - m ) * 255;
    float b = ( 1 - y ) * 255;

    return color(r, g, b);
  }

  //VOID DISPLAY////////////////////
  void display() {
    noStroke();
    fill(col, life/10.0);
    ellipse(location.x, location.y, size, size);
  }

  void checkEdges() {

    if (location.x > width) {
      location.x = 0;
    }
    else if (location.x < 0) {
      location.x = width;
    }

    if (location.y > height) {
      location.y = 0;
    }
    else if (location.y < 0) {
      location.y = height;
    }
  }


  String getMsg()
  {
    try {
      q = new Query(terms[index]);
      q.setRpp(1);

      QueryResult result = twitter.search(q);
      //Cast the list of tweets into an ArrayList
      ArrayList tweets = (ArrayList) result.getTweets();

      //and instead of a for loop, just get(0);
      //Pull out this particular Tweet object. 
      Tweet t = (Tweet) tweets.get(0);
      return t.getText();

      //println(usr+ ":  " + msg + "   ("+d+")");
    }
    catch(TwitterException te) {
      //println(te);
      return te.toString();
    }
  }
}

Replies(8)

I can't even test your Code, I get an error at
Copy code
  1. Twitter twitter;
and I couldn't figure out how to use the library...

I tried this before, I don't know what's going on...




i think what you have to do is download the twitter4j-2.2.5 plug-in, place their folder (mac) finder > document > processing, go to twitter4j-2.2.5 > lib, and drag the java jar file called "twitter4j-core-2.2.5" into the processing document.

if that doesnt work, you can delete the twitter4j functions in the processing file.

good question by the way, i totally forgot about that. the problem with twitter4j-2.2.5 is that even though you have all the files, the file "twitter4j-core-2.2.5", has to be placed in the document page, and then dragged into the processing file.

now it's running.

but as I said I have no idea about this twitter4j

you have to put more twitter-stuff into draw() as I said

Cheers, Chrisir


do you know how i can allow keywords to generate color-coded particles?

air and space tweet : dark purple particle
creative court tweet: orange particle
eco systems tweet : blue  particle
innovation lab tweet : pink  particle
rain forest tweet : green  particle
world of life tweet : red  particle


use switch

...

case " air and space"
fill (col11);
break;

Greetings, Chrisir




what's
Copy code
  1. setRpp

doing?


Anyway, I played with it a little.

Instead of using switch I build an array of colors (called "myCoolColorsForEachWord").

Your "terms" array must have the same size as those colors obviously, because they use the same index.

The program is dead slow.  
This is because of getMsg.
Maybe you find a way of loading it asynchronous.

At the moment I use draw() as a loop and check for the next word (it is given by index which is used in getMsg) every 121 times or so only.
I pulled getMsg out of your class.

Greetings, Chrisir



Copy code
  1. // TAB ONE CODES:
  2. ArrayList particles;
  3. PShape a;
  4. Twitter twitter;
  5. Query q;
  6. /*
  7.  Mover[] movers = new Mover[190];
  8.  Mover[] movers2 = new Mover[25];
  9.  Mover[] movers3 = new Mover[7];
  10.  */
  11. color col;
  12. String[] terms = {
  13.   "airandspace", "creativecourt", "ecosystems", "innovationlab", "rainforest", "worldoflife"
  14. };
  15. /*
  16. int[] hues = {
  17.  30, 25, 21, 60, 80, 90
  18.  };
  19.  */
  20. color[] myCoolColorsForEachWord = {
  21.   color (255, 0, 0),
  22.   color (0, 255, 0),
  23.   color (0, 0, 255),
  24.   color (255, 255, 0),
  25.   color (0, 255, 255),
  26.   color (110, 255, 22)
  27. };
  28. int index = 0;
  29. int indexOld = -1;
  30. int waitForNextCheckOfTwitter = 0;
  31. String newMessage = "";
  32. //VOID SETUP////////////////////
  33. void setup() {
  34.   // check for Error ---
  35.   if ( terms.length != myCoolColorsForEachWord.length ) {
  36.     // if ( terms.length != hues.length ) {   
  37.     println ("**************************************************");
  38.     println ("**************  big mistake     ******************"); 
  39.     println ("**************************************************"); 
  40.     // QUIT
  41.     exit();  // QUIT
  42.   }
  43.   size(800, 800);
  44.   smooth();
  45.   background(0);
  46.   color(HSB, 100);
  47.   frameRate(70);
  48.   //twitter stuff
  49.   ConfigurationBuilder cb = new ConfigurationBuilder();
  50.   cb.setOAuthConsumerKey("PLmGEQtruhjtEDryFyx0rA");
  51.   cb.setOAuthConsumerSecret("yN5ubhMXKkJjRF151m2M2awWQXmkaQzMgOkksyLtI");
  52.   cb.setOAuthAccessToken("317405665-ojRo56DtA5KFvfujk39EM60g9Tk75DrE0f4VvO2N");
  53.   cb.setOAuthAccessTokenSecret("oo2LazqjdMzYtiFUTlQWA3vzmifTOxUA3C8xzB11E");
  54.   twitter = new TwitterFactory(cb.build()).getInstance();
  55.   /*
  56.       try{
  57.    QueryResult result = twitter.search(q);
  58.    ArrayList tweets = (ArrayList) result.getTweets();
  59.    Tweet t = (Tweet) tweets.get(0);
  60.    particles = new ArrayList();
  61.    particles.add(new Mover(4, .07));
  62.    println(t.getText());
  63.    }catch(TwitterException te){
  64.   
  65.    }*/
  66.   particles = new ArrayList();
  67.   // col = color(hues[index], random(80, 100), random(60, 90));
  68.   col =  color (0, 255, 0);
  69.   particles.add(new Mover(col, 4, .07));
  70.   //Mover p = (Mover)particles.get(0);
  71.   //println(p.msg);
  72.   // a = loadShape("mos.svg");
  73.   /*
  74.       for (int i = 0; i < movers.length; i++) {
  75.    movers[i] = new Mover(4, .07);
  76.    }
  77.    for (int i = 0; i < movers2.length; i++) {
  78.    movers2[i] = new Mover(4, .13);
  79.    }
  80.    for (int i = 0; i < movers3.length; i++) {
  81.    movers3[i] = new Mover(4, .2);
  82.    }*/
  83. }
  84. void draw()
  85. {
  86.   if ( index >= terms.length ) {
  87.     index=0;
  88.   }
  89.   noStroke();
  90.   /* fill(255);
  91.    rect(0, 0, width, height); */
  92.   background (255);
  93.   // get stuff
  94.   // newMessage = getMsg();
  95.   // newMessage = "hello ... "; 
  96.   // println ( newMessage );
  97.   if (index!=indexOld) {
  98.     //for (int index=0; index<terms.length; index++) {
  99.     newMessage = getMsg();
  100.     // found something?
  101.     if (newMessage!="") {
  102.       createParticles(index);
  103.     } // if
  104.     // } // for
  105.     indexOld=index;
  106.   }
  107.   /*
  108.   for (int i=0; i<terms.length; i++) {
  109.    if (newMessage.equals(terms[i])) {
  110.    createParticles(i);
  111.    } // if
  112.    } // for
  113.    */
  114.   // create a timer to call createParticles();
  115.   // create no timer
  116.   for (int i=0; i<particles.size(); i++)
  117.   {
  118.     Mover m = (Mover) particles.get(i);
  119.     m.checkEdges();
  120.     m.fadeOut();
  121.     m.update();
  122.     m.display();
  123.     if (m.finished()) {
  124.       // Items can be deleted with remove()
  125.       particles.remove(i);
  126.     }
  127.   } // for
  128.   waitForNextCheckOfTwitter ++;
  129.   if (waitForNextCheckOfTwitter >= 121) {
  130.     index ++;
  131.     waitForNextCheckOfTwitter = 0;
  132.   }
  133. } // func
  134. // ===========================================================================
  135. void mousePressed() {
  136.   createParticles( int(random(terms.length)) );
  137. }
  138. void createParticles(int indexForArray)
  139. {
  140.   // index = 3; //  int(random(terms.length));
  141.   // col = color(hues[indexOfHues], random(80, 90), random(90, 100));
  142.   col =   myCoolColorsForEachWord [indexForArray];
  143.   particles.add(new Mover(col, 4, .07));
  144. }
  145. /*
  146. String getMsgTEST()
  147.  {
  148.  q = new Query(terms[index]);
  149.  q.setRpp(1);
  150.  println(q);
  151.  QueryResult result = twitter.search(q);
  152.  // println (result);
  153.  //Cast the list of tweets into an ArrayList
  154.  ArrayList tweets = (ArrayList) result.getTweets();
  155.  // println  (tweets.size());
  156.  //and instead of a for loop, just get(0);
  157.  //Pull out this particular Tweet object.
  158.  if (tweets.size()>0) {
  159.  Tweet t = (Tweet) tweets.get(0);
  160.  return t.getText();
  161.  }
  162.  else {
  163.  return "";
  164.  }
  165.  //println(usr+ ":  " + msg + "   ("+d+")");
  166.  }*/
  167. String getMsg()
  168. {
  169.   try {
  170.     q = new Query(terms[index]);
  171.     q.setRpp(1);
  172.     QueryResult result = twitter.search(q);
  173.     println (result);
  174.     //Cast the list of tweets into an ArrayList
  175.     ArrayList tweets = (ArrayList) result.getTweets();
  176.     println  (tweets.size());
  177.     //and instead of a for loop, just get(0);
  178.     //Pull out this particular Tweet object.
  179.     if (tweets.size()>0) {
  180.       Tweet t = (Tweet) tweets.get(0);
  181.       return t.getText();
  182.     }
  183.     else {
  184.       return "";
  185.     }
  186.     //println(usr+ ":  " + msg + "   ("+d+")");
  187.   }
  188.   catch(TwitterException te) {
  189.     println("Error --------------------------------------------");
  190.     println(te);
  191.     return te.toString();
  192.   }
  193. }
  194. /*
  195.     //VOID DRAW////////////////////
  196.  void draw() {
  197.  noStroke();
  198.  fill(255);
  199.  rect(0, 0, width, height);
  200. /*
  201.  for (int i = 0; i < movers.length; i++) {
  202.  movers[i].update();
  203.  movers[i].checkEdges();
  204.  if (!finished) movers[i].display();
  205.  movers[i].fadeOut();
  206.  }
  207.  for (int i = 0; i < movers2.length; i++) {
  208.  movers2[i].update();
  209.  movers2[i].checkEdges();
  210.  if (!finished) movers2[i].display();
  211.  movers2[i].fadeOut();
  212.  }
  213.  for (int i = 0; i < movers3.length; i++) {
  214.  movers3[i].update();
  215.  movers3[i].checkEdges();
  216.  if (!finished) movers3[i].display();
  217.  movers3[i].fadeOut();
  218.  }*/
  219. //shape (a, -200, -200, 1200, 1200);
  220. //}
  221. // ==================================================================
  222. // TAB TWO CODES:
  223. class Mover {
  224.   PVector location;
  225.   PVector velocity;
  226.   PVector acceleration;
  227.   //fade out function; the larger the number, the longer////////////////////
  228.   float life = 3550;
  229.   float topspeed;
  230.   int size;
  231.   float multiplier;
  232.   color col;
  233.   String msg;
  234.   Mover(color tcol, int xsp, float xmu) {
  235.     size = int(random(4, 1));
  236.     location = new PVector(random(width), random(height));
  237.     velocity = new PVector(random(width), random(height));
  238.     topspeed = xsp;
  239.     multiplier = xmu;
  240.     msg = getMsg();
  241.     int x = int(random(0, 255));
  242.     float c = random(0, 80);
  243.     float m = random(0, 55);
  244.     float y = random(0, 0);
  245.     float k = random(0, 30);
  246.     //col = convert(c, m, y, k);
  247.     col = tcol;
  248.   }
  249.   //fade out function////////////////////
  250.   boolean finished() {
  251.     // Balls fade out
  252.     // life-=10;
  253.     if (life <= 0) {
  254.       return true;
  255.     }
  256.     else {
  257.       return false;
  258.     }
  259.   }
  260.   void fadeOut() {
  261.     // Balls fade out
  262.     // life--;
  263.     life-=10;
  264.   }
  265.   //VOID UPDATE////////////////////
  266.   void update() {
  267.     PVector mouse = new PVector(width/2, height/2);
  268.     PVector dir = PVector.sub(mouse, location);
  269.     dir.normalize();
  270.     dir.mult(multiplier);
  271.     acceleration = dir;
  272.     velocity.add(acceleration);
  273.     velocity.limit(topspeed);
  274.     location.add(velocity);
  275.   }
  276.   //COLOR//9*I;//////////////////
  277.   color convert(float c, float m, float y, float k) {
  278.     c = c / 100;
  279.     m = m / 100;
  280.     y = y / 100;
  281.     k = k / 100;
  282.     c = ( c * ( 1 - k ) + k );
  283.     m = ( m * ( 1 - k ) + k );
  284.     y = ( y * ( 1 - k ) + k );
  285.     float r = ( 1 - c ) * 255;
  286.     float g = ( 1 - m ) * 255;
  287.     float b = ( 1 - y ) * 255;
  288.     return color(r, g, b);
  289.   }
  290.   //VOID DISPLAY////////////////////
  291.   void display() {
  292.     noStroke();
  293.     fill(col, life/10.0);
  294.     ellipse(location.x, location.y, size, size);
  295.   }
  296.   void checkEdges() {
  297.     if (location.x > width) {
  298.       location.x = 0;
  299.     }
  300.     else if (location.x < 0) {
  301.       location.x = width;
  302.     }
  303.     if (location.y > height) {
  304.       location.y = 0;
  305.     }
  306.     else if (location.y < 0) {
  307.       location.y = height;
  308.     }
  309.   }
  310. }  // class
  311. // ====================================================