Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Structuring the code

$
0
0

Sorry to post it here, but I'm having a huge problem structuring my code. I have just started working with processing, so I'm more of a beginner. Would really appreciate if anyone could help me with this.

The idea behind my project is to be able to control the amount of negative and positive information we want to see. I tried to combine rss reader code of two different websites and make a working slider that would change the background colour. Headlines of the bad news would be displayed in black and the good news would be in white. Depending on type of information we want to see, the background would change from black to white revealing only headlines we want to read.

This is a v simple idea, been trying to structure it out so the headlines would work in colour too but I don't think I've got enough knowledge to do so.

rss code:

String[] titles;
String[] titles2;


PFont font;
//color textColor = 255;
void setup() {
  size(1450,860);
  background(0);
  noStroke();
  smooth();

  // Setup font
  font = loadFont("din.vlw");
  textFont(font, 18);

  // Load RSS feed

  String url = "https://feedity.com/reddit-com/W1FRU1ZV.rss";
  XML rss = loadXML(url);
  //println(rss.listChildren());

  // Get title of each element

  XML[] titleXMLElements = rss.getChildren("channel/item/title");
  titles = new String[titleXMLElements.length];
  for (int i = 0; i < titleXMLElements.length; i++) {
    String title = titleXMLElements[i].getContent();


    titles[i] = title;
  }

    String url2 = "https://feedity.com/reddit-com/W1FRU1ZW.rss";
  XML rss2 = loadXML(url2);
  // Get title of each element
  //println(rss2.listChildren());

  XML[] title2XMLElements = rss2.getChildren("channel/item/title");
  titles2 = new String[title2XMLElements.length];
  for (int i = 0; i < title2XMLElements.length; i++) {
    String title2 = title2XMLElements[i].getContent();
    // Store title in array for later use
    titles2[i] = title2; }

}

void draw() {
background(255);

  for (int i = 0; i < titles.length; i++) {
    float y = (i+1) * 15;

    fill(0, 220);
    text(titles[i], 100, y*6, 500, 200);

  }

  for (int i = 0; i < titles2.length; i++) {
    float y = (i+1) * 15;

    fill(0);
    text(titles2[i], 800, y*6, 500, 100);
}
}

Slider ( got this one from http://www.sojamo.de/ and been trying to use some parts of it)

PGraphics letters;   //news headlines
PGraphics sliders;   //control sliders/background colour

import controlP5.*;

ControlP5 cp5;

int myColor = color(255);
int c1,c2;
float n,n1;


void setup() {
  size(400,600);
  noStroke();
  cp5 = new ControlP5(this);

  letters = createGraphics(width, height);
  sliders = createGraphics(width, height);



  // create a new button with name 'buttonA'
  cp5.addButton("colorA")
     .setValue(0)
     .setPosition(100,100)
     .setSize(200,19)
     ;

  // and add another 2 buttons
  cp5.addButton("colorB")
     .setValue(100)
     .setPosition(100,120)
     .setSize(200,19)
     ;



}

void draw() {
  background(myColor);
  myColor = lerpColor(c1,c2,n);
  n += (1-n)* 0.1;
}

public void controlEvent(ControlEvent theEvent) {
  println(theEvent.getController().getName());
  n = 0;
}

// function colorA will receive changes from
// controller with name colorA
public void colorA(int theValue) {
  println("a button event from colorA: "+theValue);
  c1 = c2;
  c2 = color(0,0,0);
}

// function colorB will receive changes from
// controller with name colorB
public void colorB(int theValue) {
  println("a button event from colorB: "+theValue);
  c1 = c2;
  c2 = color(255);
}


public void play(int theValue) {
  println("a button event from buttonB: "+theValue);
  c1 = c2;
  c2 = color(0,0,0);
}

I have been trying to use PGraphics but again, I cannot think what's the best way to structure my code. I'd really appreciate any help,

Thank you!!


Viewing all articles
Browse latest Browse all 2896

Trending Articles