I am working on a program that will search through news stories of an XML feed, and display a red overlay on stories that have negative-connotation words, blue on positive-connotation, purple on both, and grey if a string contains neither positive or negative words.
I was wondering the best way to implement searching through strings, I was a assuming an "if" statement would work best but I have gotten stuck in my programming. I'm somewhat of a newbie to processing, but I love to learn more!
I would appreciate any input, thanks!
- ///load
- float r;
- float g;
- float b;
- // Array to store titles
- String[] titles;
- PFont font;
- void setup() {
- size(700, 700);
- background(0);
- noStroke();
- // Setup font
- font = loadFont("MarketDeco-14.vlw");
- textFont(font, 14);
- // Load RSS feed
- String url = "http://feeds.bbci.co.uk/news/rss.xml";
- XMLElement rss = new XMLElement(this, url);
- // Get title of each element
- XMLElement[] titleXMLElements = rss.getChildren("channel/item/title");
- titles = new String[titleXMLElements.length];
- for (int i = 0; i < titleXMLElements.length; i++) {
- String title = titleXMLElements[i].getContent();
- // Store title in array for later use
- titles[i] = title;
- println(titles[i]);
- //change fill color
- if(titles[i].contains("deaths")){
- r = 255;
- g = 0;
- b = 0;
- }
- else{
- r = 255;
- g = 0;
- b = 0;
- }
- }
- }
- void draw() {
- background(255);
- // Draw all titles with bars and colors depending on its length
- for (int i = 0; i < titles.length; i++) {
- float y = (i+1) * 16;
- //square icon
- fill(r, g, b);
- rect(8, y - 11, 14, 14);
- //overlay over text
- fill(r, g, b, 90);
- rect(23, y - 11, titles[i].length() * 8.4, 14);
- //text
- fill(0, 220);
- text(titles[i], 26, y);
- }
- }
1