We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello.
I have 2 sketches, the first of which grabs some text from a website. At present I just have a printIn command to check that it's grabbing the right bit (which it is). I want to take the text that this sketch grabs and get it to go into a rolling headline.
I have a second sketch taken from a rolling headline tutorial, and am trying to work out how to get the text from the printIn command to go into the rolling headline text (and from there for it to check and replace every once in a while if the text from the website changes).
Can someone point me in the right direction as to how to do this? Thanks
LP.
The http request code is pasted first:
import http.requests.*;
public void setup()
{
size(400,400);
smooth();
GetRequest get = new GetRequest("https://www.mi5.gov.uk/UKThreatLevel/UKThreatLevel.xml");
get.send();
//println("Reponse Content: " + get.getContent());
//println("Reponse Content-Length Header: " + get.getHeader("Content-Length"));
///return the contents of the HTTP request
String content = get.getContent();
//turn itinto HTML
XML rss = parseXML(content);
///get the main chunk of xml inside the parent rss
XML channel = rss.getChild("channel");
//inside this is a child element called 'title'
XML item = channel.getChild("item/title");
//you need what's inside this element i.e. ignore the tags, get the stuff inside
String itemContent = item.getContent();
//how do I get the text printed out into the second sketch?
println("", itemContent);
}
This is the rolling headline sketch code:
int blinkTime;
boolean blinkOn;
PImage bgImg; // Declare variable PImage bgImg related to the field
PGraphics img;
//For now some news headlines, it's here where I want to place the text retrieved from the first sketch
String[] headlines = {
"fact plus importance equals news",
"speed plus repetition equals fear",
"percentage minus context equals terror",
"it's here where I want to place the text retrieved from the first sketch",
};
PFont f; // Global font variable
float x; // Horizontal location
int index = 0;
PFont l; // STEP 1 Declare PFont variable
void setup() {
size(600, 399); // make the size same as photo
bgImg = loadImage("fieldnpole.jpg"); // load the image inside setup
f = createFont( "HelveticaNeue-Bold", 20);
// Initialize headline offscreen
x = width;
}
{
blinkTime = millis();
blinkOn = true;
}
void draw() {
background(bgImg);
fill(#FF0303);
noStroke();
if (blinkOn)
ellipse(550,30,20,20);
fill(#FF0303);
noStroke();
if (millis() - 500 > blinkTime) {
blinkTime = millis();
blinkOn = !blinkOn;
}
fill(255,0,0.50);
rect(0,340,600,80);
// Display headline at x location
textFont(f, 20);
textAlign (LEFT);
fill(255);
// A specific String from the array is displayed according to the value of the "index" variable.
text(headlines[index], x, height-20);
// Decrement x
x = x - 3;
// If x is less than the negative width, then it is off the screen
// textWidth() is used to calculate the width of the current String.
float w = textWidth(headlines[index]);
if (x < -w) {
x = width;
// index is incremented when the current String has left the screen in order to display a new String.
index = (index + 1) % headlines.length;
}
{
l = createFont("HelveticaNeue-Bold",10,true); // STEP 2 Create Font
textFont(l,10); // STEP 3 Specify font to be used
fill(0); // STEP 4 Specify font color
text("LIVE",510,35); // STEP 5 Display Text
}
}
Answers
make a third sketch as a copy of the first, then incorporate the 2nd in it
avoid duplicate functions like setup
itemContent in the first sketch is headlines in the 2nd
Thanks Chrisr. I am getting an ItemContent does not exist message but am unsure what I'm doing (I as going to add wrong but I think I'll leave it there). Code at present with error is...
try to join both setup() functions
at the end of setup() the itemContent is handled
Not tested ;-)
Many thanks for that.
And you need the draw from the 2nd sketch
I eventually realised that I'd need that. Seems to all work fine. Now I have some more
You can usw. ctrl–t to have auto–Indents
Look at the curly brackets {}
Some seem amiss
In pasting to the forum or in my scrawly initial code? (or both) !
Can you (or anyone else for that matter) help me get my head around what I need to look at to do the next bit of what I'm attempting? - I'm not sure whether forum-wise it would be better to ask a new question.
I now have the 1) a rolling headline sketch that serves up a status alert from the spooks. 2) I have a second sketch that is an oscillating flag that presently slides up and down a flagpole image with y-mouse constraints.
I want to get the flag to be in 1 of 5 possible positions that are dependant on the text that's encountered when making the http request. (So I need to correlate specific positions for the flag with 5 possible words served up).
What concepts do I need to get my head around? I've re posted the (hopefully tidier) code from the http sketch as well as the wavy flag code as a seperate sketch. (When I've tried to combine them I get a mixing active and static modes warning so far and haven't been able to work out why either). Thanks
LP
1st sketch...
2nd sketch...
oh and the graphic I'm using...
ctrl-t is used in processing not in the forum
Not solved!! Can anyone jump in here...?
Both sketches are not treated with ctrl-t
Do both sketches run? Because I see misplaced {}?
From line 77 onward in the 1st sketch it seems bit wrong. My setup() function was much better.
In principle you merged two sketches already and now you have to do the same with a 3rd one.
The cleanest would be to alter you 3rd into a function or several functions because you have a few functions already. The main function should get a word as a parameter the flag is reacting to.
Once you did that copy the functions without setup and draw into sketch 1.
call the main function then and pass it a word from headlines[] as a Parameter.
Thought I'd done ctrl-t. Both sketches run on my machine. Thanks for the advice, will attempt that over the next few days (I believe some reading will be involved as I am attempting to run before I can crawl.)
Thanks for your reply