Hi!
I have just started learning Processing, and am trying to map a Twitter stream in 3D.
Here is my code:
- import processing.opengl.*;
- import damkjer.ocd.*;
- Camera cam;
- PFont fontA, fontB;
- int numTerms = 9;
- int bufferRate = 20;
- int z [] = new int [numTerms];
- int now;
- int end;
- int meshWidthX = 250;
- int meshShiftX = 500;
- int meshCols = 3;
- int meshSensY = 10;
- int meshShiftY = 800;
- int meshWidthZ = 100;
- int tweetBoxW = 100;
- int tweetBoxH = 100;
- String [] termsLabel = {
- "hello", "world", "helloworld", "processing", "arduino", "edinburgh","university","scotland","architecture"
- };
- String terms[] = {
- "hello", "world", "helloworld", "processing", "arduino", "edinburgh","university","scotland","architecture"
- };
- String gt [] = new String [numTerms];
- String gf [] = new String [numTerms];
- String gn [] = new String [numTerms];
- long lastId [] = new long [numTerms];
- void setup () {
- size (screen.width, screen.height, OPENGL);
- smooth ();
- cam = new Camera (this, 500, 500, 1000);
- fontA = loadFont ("TwCenMT-BoldItalic-150.vlw");
- fontB = loadFont ("TwCenMT-Regular-150.vlw");
- Twitter twitter = new Twitter ();
- for (int party = 0; party < numTerms; party++) {
- try {
- Query query = new Query (terms [party]);
- QueryResult result = (QueryResult) twitter.search (query);
- java.util.List tweets = result.getTweets ();
- println (tweets.size ());
- Tweet t = (Tweet) tweets.get (0);
- lastId [party] = t.getId ();
- println (lastId [party]);
- gt [party] = t.getText ();
- gf [party] = t.getFromUser ();
- gn [party] = t.getProfileImageUrl ();
- }
- catch (Exception e) {
- System.out.println (e);
- }
- }
- }
- void draw () {
- cam.feed ();
- now = second ();
- background (0);
- int maximum = max (z);
- int total = z [0] + z [1] + z [2] + z [3] + z [4] + z [5] + z [6] + z [7] + z [8];
- for (int i = 0; i < 2; i++) {
- int percent = 0;
- int percentNext = 0;
- if (total > 0) percent = int ( (100 * z [i]) / total);
- if (total > 0) percentNext = int ( (100 * z [i + 1]) / total);
- int colour = 0;
- if (percent > 0) colour = int (3 * percent);
- else colour = 0;
- stroke (colour);
- noFill ();
- line ( (i* meshWidthX) + meshShiftX, meshShiftY - (meshSensY * percent), 0 * meshWidthZ, ((i + 1) * meshWidthX) + meshShiftX, meshShiftY - (meshSensY * percentNext), 0 * meshWidthZ);
- }
- for (int i = 0; i < 3; i++) {
- int percent = 0;
- int percentNext = 0;
- if (total > 0) percent = int ( (100 * z [i]) / total);
- if (total > 0) percentNext = int ( (100 * z [i + 1]) / total);
- fill (255, 0, 0, 100);
- textFont (fontB, 10);
- text (gt [i] +
- " FROM: " + gf [i], (i * meshWidthX + meshShiftX), meshShiftY - (meshSensY * percent), tweetBoxW, tweetBoxH, 0 * meshWidthZ);
- fill (125 / 2, 100);
- textFont (fontA, 15);
- text (termsLabel[i] + " - " + percent + ".0%", (i * meshWidthX + meshShiftX), meshShiftY - (meshSensY * percent), 0 * meshWidthZ);
- }
- for (int i = 3; i < 5; i++) {
- int percent = 0;
- int percentNext = 0;
- if (total > 0) percent = int ( (100 * z [i]) / total);
- if (total > 0) percentNext = int ( (100 * z [i + 1]) / total);
- int colour = 0;
- if (percent > 0) colour = int (3 * percent);
- else colour = 0;
- stroke (colour);
- noFill ();
- line ( ((i - 3) * meshWidthX) + meshShiftX, meshShiftY - (meshSensY * percent), 1 * meshWidthZ, ((i - 2) * meshWidthX) + meshShiftX, meshShiftY - (meshSensY * percentNext), 1 * meshWidthZ);
- }
- for (int i = 3; i < 6; i++) {
- int percent = 0;
- int percentNext = 0;
- if (total > 0) percent = int ( (100 * z [i]) / total);
- if (total > 0) percentNext = int ( (100 * z [i + 1]) / total);
- fill (255, 0, 0, 100);
- textFont (fontB, 10);
- text (gt [i] +
- " FROM: " + gf [i], ((i - 3) * meshWidthX) + meshShiftX, meshShiftY - (meshSensY * percent), tweetBoxW, tweetBoxH, 1 * meshWidthZ);
- fill (255 / 2, 100);
- textFont (fontA, 15);
- text (termsLabel[i] + " - " + percent + ".0%", ((i - 3) * meshWidthX) + meshShiftX, meshShiftY - (meshSensY * percent), 1 * meshWidthZ);
- }
- for (int i = 6; i < 8; i++) {
- int percent = 0;
- int percentNext = 0;
- if (total > 0) percent = int ( (100 * z [i]) / total);
- if (total > 0) percentNext = int ( (100 * z [i + 1]) / total);
- int colour = 0;
- if (percent > 0) colour = int (3 * percent);
- else colour = 0;
- stroke (colour);
- noFill ();
- line ( ((i - 6) * meshWidthX) + meshShiftX, meshShiftY - (meshSensY * percent), 2 * meshWidthZ, ((i - 5) * meshWidthX) + meshShiftX, meshShiftY - (meshSensY * percentNext), 2 * meshWidthZ);
- }
- for (int i = 6; i < 8; i++) {
- int percent = 0;
- int percentNext = 0;
- if (total > 0) percent = int ( (100 * z [i]) / total);
- if (total > 0) percentNext = int ( (100 * z [i + 1]) / total);
- fill (255, 0, 0, 100);
- textFont (fontB, 10);
- text (gt [i] +
- " FROM: " + gf [i], ((i - 6) * meshWidthX) + meshShiftX, meshShiftY - (meshSensY * percent), tweetBoxW, tweetBoxH, 2 * meshWidthZ);
- fill (255 / 2, 100);
- textFont (fontA, 15);
- text (termsLabel[i] + " - " + percent + ".0%", ((i - 6) * meshWidthX) + meshShiftX, meshShiftY - (meshSensY * percent), 2 * meshWidthZ);
- }
- for (int i = 0; i < 3; i++) {
- int percent = 0;
- int percentNextSq = 0;
- if (total > 0) percent = int ( (100 * z [i]) / total);
- if (total > 0) percentNextSq = int ( (100 * z [i + 3]) / total);
- int colour = 0;
- if (percent > 0) colour = int (3 * percent);
- else colour = 0;
- stroke (colour);
- noFill ();
- line ( (i * meshWidthX) + meshShiftX, meshShiftY - (meshSensY * percent), 0 * meshWidthZ, ((i + 3) * meshWidthX) - 250, meshShiftY - (meshSensY * percentNextSq), 1 * meshWidthZ);
- }
- for (int i = 3; i < 6; i++) {
- int percent = 0;
- int percentNextSq = 0;
- if (total > 0) percent = int ( (100 * z [i]) / total);
- if (total > 0) percentNextSq = int ( (100 * z [i + 3]) / total);
- int colour = 0;
- if (percent > 0) colour = int (3 * percent);
- else colour = 0;
- stroke (colour);
- noFill ();
- line ( (i * meshWidthX) - 250, meshShiftY - (meshSensY * percent), 1 * meshWidthZ, ((i + 3) * meshWidthX) - 1000, meshShiftY - (meshSensY * percentNextSq), 2 * meshWidthZ);
- }
- if (now!= end && now%bufferRate == 0) {
- Twitter twitter = new Twitter();
- for (int party = 0; party < numTerms; party++) {
- try {
- Query query = new Query (terms [party]);
- query.setSinceId (lastId [party]);
- QueryResult result = (QueryResult) twitter.search (query);
- java.util.List tweets = result.getTweets ();
- if (tweets.size () > 0) {
- Tweet t = (Tweet) tweets.get (0);
- lastId [party] = t.getId ();
- gt [party] = t.getText ();
- gf [party] = t.getFromUser ();
- println (gt [party]);
- println (lastId [party]);
- z [party] = min (meshShiftY / numTerms * meshShiftY, z [party] + tweets.size ());
- }
- }
- catch (Exception e) {
- System.out.println (e);
- }
- }
- end = now;
- }
- }
- void mouseMoved () {
- cam.look (radians (mouseX - pmouseX) / 4, radians (mouseY - pmouseY) / 4);
- }
- void mouseDragged () {
- cam.truck ((mouseX - pmouseX) * 2);
- cam.dolly ((mouseY - pmouseY) * -2);
The code is running very slowly, and I think this is because so much of it is within draw. I am guessing I need to create a class, however I don't know where to start as the examples on the Processing site don't contain a variable i (apologies if I am using the wrong terminology!!!)
Any help is greatly appreciated!!!
1