import org.json.*; String apiKey = "6603a082e81f4de0e9ea1ca5bdc6f570:11:49920035"; String baseURL = "http://api.nytimes.com/svc/news/v2/all"; PFont font1; PFont font2; PFont font3; int counter = 0; int timer = 0; int height = 800; int width = 1200; boolean updateOn = true; Newswire_entry[] recentNews = new Newswire_entry[20]; Newswire_blob[] entryBlobs = new Newswire_blob[20]; void setup() { size(width,height); frameRate(60); background(0); smooth(); //Load the font font1 = loadFont("Verdana-48.vlw"); font2 = loadFont("SerifGothic-48.vlw"); font3 = loadFont("Meta-Normal-48.vlw"); //toPrint = getOJArticles(); //THE METHOD GETS CALLED HERE getArticles(recentNews); // for (int i = 0; i < recentNews.length; i++){ // println(recentNews[i].section); // println(""); // } createBlobs(recentNews, entryBlobs); } void draw() { //background(0); drawBackground(); textFont(font3, 15); for (int i = 0; i < entryBlobs.length; i++){ entryBlobs[i].draw(); fill(255, 255, 255); text(entryBlobs[i].entry.headline, entryBlobs[i].startX+20, entryBlobs[i].startY, 200); // println(entryBlobs[i].xVel); } if (updateOn){ update(); } } void update(){ for (int i = 0; i < entryBlobs.length; i++){ entryBlobs[i].update(); } } Newswire_entry[] getArticles(Newswire_entry[] wire_results) { //String request = baseURL+"/last24hours?api-key="+apiKey; String request = baseURL+"/recent?api-key="+apiKey; // println(request); // return request; // String result = join( loadStrings( request ), ""); // println( result ); // return result; try { JSONObject nytData = new JSONObject(join(loadStrings(request), "")); //println(nytData); JSONArray results = nytData.getJSONArray("results"); int total = nytData.getInt("num_results"); // String[] headlines_arr = new String[total]; for (int i = 0; i < total; i++){ JSONObject entry = results.getJSONObject(i); String headline = entry.getString("headline"); String summary = entry.getString("summary"); String byline = entry.getString("byline"); String created = entry.getString("created"); String section = entry.getString("section"); String type = entry.getString("type"); String people = entry.getString("people"); String locations = entry.getString("locations"); String indexing_terms = entry.getString("indexing_terms"); String organizations = entry.getString("organizations"); String terms = entry.getString("terms"); wire_results[i] = new Newswire_entry(headline, summary, byline, created, section, type, people, locations, indexing_terms, organizations, terms); // println(headline); //println(entry); // println(""); } println ("There were " + total + " new feeds."); String title = nytData.getString("title"); println(title); //return headlines_arr; } catch (JSONException e) { println ("There was an error parsing the JSONObject."); } return wire_results; } Newswire_blob[] createBlobs(Newswire_entry[] wire_results, Newswire_blob[] blobs) { for (int i = 0; i < wire_results.length; i++){ Newswire_entry entry = wire_results[i]; int divide = 100; String type = entry.type; String section = entry.section; // println(entry.headline); // println(section); // println(section); int xVel = 0; int yVel = 0; int startX = int(random(width/4, 3*width/4)); int startY = int(random(height/3, 2*height/3)); if ((section.equals("Arts")) || (section.equals("Books")) || (section.equals("Theater")) || (section.equals("Movies"))){ int endX = width/8; int endY = height/6; // xVel = (endX - startX)%(6*divide); //yVel = (endY - startY)%(4*divide); xVel = -1; yVel = -1; } if (section.equals("Autos")){ int endX = 3*width/8; int endY = height/6; xVel = (endX - startX)%divide; yVel = (endY - startY)%divide; } if (section.equals("Business")){ int endX = 5*width/8; int endY = height/6; xVel = (endX - startX)%divide; yVel = (endY - startY)%divide; } if ((section.equals("Fashion & Style")) || (section.equals("T:Style"))){ int endX = 7*width/8; int endY = height/6; xVel = (endX - startX)/divide; yVel = (endY - startY)/divide; } if (section.equals("Health")){ int endX = width/8; int endY = height/2; xVel = (endX - startX)%divide; yVel = (endY - startY)%divide; } if (section.equals("Opinion")){ int endX = 7*width/8; int endY = height/2; xVel = (endX - startX)%divide; yVel = (endY - startY)%divide; } if ((section.equals("N.Y. / Region")) || (section.equals("U.S."))){ int endX = width/8; int endY = 5*height/6; xVel = (endX - startX)%divide; yVel = (endY - startY)%divide; } if ((section.equals("Science")) || (section.equals("Technology")) || (section.equals("Multimedia"))){ int endX = 3*width/8; int endY = 5*height/6; xVel = (endX - startX)%divide; yVel = (endY - startY)%divide; } if (section.equals("Sports")){ // int endX = width/4; // int endY = height/3; xVel = (width/4 - startX)%(3*divide); yVel = (height/3 - startY)%(3*divide); // println((width/4-startX)%divide); // println((height/3-startY)%divide); // xVel = 0; // yVel = 1; } if (section.equals("World")){ int endX = 7*width/8; int endY = 5*height/6; xVel = 1; yVel = 1; } else { // int endX = width/8; // int endY = 5*height/6; // xVel = (endX - startX)%divide; // yVel = (endY - startY)%divide; } int radius = 20; int age = 0; blobs[i] = new Newswire_blob(entry, startX, startY, radius, xVel, yVel, age, type, section); println(entry.headline); println(entry.section); } return blobs; } void drawBackground(){ textFont(font3, 20); stroke(255); // art fill(#FF0080); rect(0, 0, width/4, height/3); fill(0); text("Arts, Books, Movies, & Theater", 10, 25); // auto fill(#72FF00); rect(width/4, 0, width/4, height/3); fill(0); text("Autos", width/4+10, 25); // business fill(#FAFF00); rect(2*width/4, 0, width/4, height/3); fill(0); text("Business", 2*width/4+10, 25); // fashion & style fill(#0070FF); rect(3*width/4, 0, width/4, height/3); fill(0); text("Fashion & Style", 3*width/4+10, 25); // health fill(#FF6200); rect(0, height/3, width/4, height/3); fill(0); text("Health", 10, height/3+25); // opinion fill(#C300FF); rect(3*width/4, height/3, width/4, height/3); fill(0); text("Opinion", 3*width/4+10, height/3+25); // Region fill(#FAFF00); rect(0, 2*height/3, width/4, height/3); fill(0); text("Region: US & NY", 10, 2*height/3+25); // Science fill(#0070FF); rect(width/4, 2*height/3, width/4, height/3); fill(0); text("Science, Tech, & Multimedia", width/4+10, 2*height/3+25); // Sports fill(#FF0080); rect(2*width/4, 2*height/3, width/4, height/3); fill(0); text("Sports", 2*width/4+10, 2*height/3+25); // world fill(#72FF00); rect(3*width/4, 2*height/3, width/4, height/3); fill(0); text("World", 3*width/4+10, 2*height/3+25); // center rect fill(0); rect(width/4, height/3, width/2, height/3); fill(0); } void keyPressed(){ if (key == 'p'){ updateOn = !updateOn; } if (key == 'c'){ for (int i = 0; i < 20; i++){ entryBlobs[i].showText = false; } } } void mouseClicked(){ for (int i = 0; i < 20; i++){ if (((mouseX > entryBlobs[i].startX-entryBlobs[i].radius) && (mouseX < entryBlobs[i].startX+entryBlobs[i].radius)) && ((mouseX > entryBlobs[i].startY-entryBlobs[i].radius) && mouseY < entryBlobs[i].startY+entryBlobs[i].radius)){ //updateOn = false; // println("TRUE!"); entryBlobs[i].showText = !entryBlobs[i].showText; } } }