Random color
in
Programming Questions
•
1 years ago
Here is the code that I'm working with:
//Build an ArrayList to hold all of the words that we get from the imported tweets
ArrayList<String> words = new ArrayList();
ConfigurationBuilder cb;
Twitter twitter;
Query query;
//These two variables are used to tweak the size and position of the names
int nameSizeMultiplier = 20;
int namePositionAdjustment = 150;
//The number of times each word comes up in the Twitter search
int cainWords = 0;
int perryWords = 0;
int bachmannWords = 0;
int paulWords = 0;
int romneyWords = 0;
int americanWords = 0;
int jobsWords = 0;
int economyWords = 0;
//Get random x & y values for each word
float cainX = random(namePositionAdjustment, width - namePositionAdjustment);
float perryX = random(namePositionAdjustment, width - namePositionAdjustment);
float bachmannX = random(namePositionAdjustment, width - namePositionAdjustment);
float paulX = random(namePositionAdjustment, width - namePositionAdjustment);
float romneyX = random(namePositionAdjustment, width - namePositionAdjustment);
float americanX = random(namePositionAdjustment, width - namePositionAdjustment);
float jobsX = random(namePositionAdjustment, width - namePositionAdjustment);
float economyX = random(namePositionAdjustment, width - namePositionAdjustment);
float cainY = random(namePositionAdjustment, height - namePositionAdjustment);
float perryY = random(namePositionAdjustment, height - namePositionAdjustment);
float bachmannY = random(namePositionAdjustment, height - namePositionAdjustment);
float paulY = random(namePositionAdjustment, height - namePositionAdjustment);
float romneyY = random(namePositionAdjustment, height - namePositionAdjustment);
float americanY = random(namePositionAdjustment, height - namePositionAdjustment);
float jobsY = random(namePositionAdjustment, height - namePositionAdjustment);
float economyY = random(namePositionAdjustment, height - namePositionAdjustment);
void setup() {
//Set the size of the stage, and the background to black.
size(800,600);
background(0);
smooth();
cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("4gi93D8y2nkadut37JcfA");
cb.setOAuthConsumerSecret("pAURDlB571afctTySuIcEczwoXCLxyN48panfpDeSQ");
cb.setOAuthAccessToken("1532481-B4KJUH0uSjdaQE0jVgi3B3BoIk5XbyxqzuNhSTstx8");
cb.setOAuthAccessTokenSecret("NQAANu41p6p2MYHL3rCej7QGk7KTAr8UiGDs7UoMKo");
//Make the twitter object and prepare the query
twitter = new TwitterFactory(cb.build()).getInstance();
query = new Query("#GOP");
query.setRpp(100);
updateTwitter();
}
void draw() {
//If 30 seconds have passed, run it again:
if (frameCount % 900 == 0)
{
updateTwitter(); //This funcion runs the search again and updates the variables
// See below for the function definition
}
background(0);
//Draw a faint black rectangle over what is currently on the stage so it fades over time.
fill(0,1);
rect(0,0,width,height);
//Draw the words at the appropriate relative size (based on the number of times they occur)
fill(255,random(50,150));
textSize(nameSizeMultiplier*cainWords);
text("Cain", cainX, cainY);
fill(random(255));
textSize(nameSizeMultiplier*perryWords);
text("Perry", perryX, perryY);
textSize(nameSizeMultiplier*bachmannWords);
text("Bachmann", bachmannX, bachmannY);
textSize(nameSizeMultiplier*paulWords);
text("Paul", paulX, paulY);
textSize(nameSizeMultiplier*romneyWords);
text("Romney", romneyX, romneyY);
textSize(nameSizeMultiplier*americanWords);
text("American", americanX, americanY);
textSize(nameSizeMultiplier*jobsWords);
text("Jobs", jobsX, jobsY);
textSize(nameSizeMultiplier*economyWords);
text("Economy", economyX, economyY);
}
void updateTwitter()
{
//Try making the query request.
try {
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
String user = t.getFromUser();
String msg = t.getText();
Date d = t.getCreatedAt();
println("Tweet by " + user + " at " + d + ": " + msg);
//Break the tweet into words
String[] input = msg.split(" ");
for (int j = 0; j < input.length; j++) {
//Put each word into the words ArrayList
words.add(input[j]);
}
};
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
//Setup the new locations:
cainWords = 0;
perryWords = 0;
bachmannWords = 0;
paulWords = 0;
romneyWords = 0;
americanWords = 0;
jobsWords = 0;
economyWords = 0;
cainX = random(namePositionAdjustment, width - namePositionAdjustment);
perryX = random(namePositionAdjustment, width - namePositionAdjustment);
bachmannX = random(namePositionAdjustment, width - namePositionAdjustment);
paulX = random(namePositionAdjustment, width - namePositionAdjustment);
romneyX = random(namePositionAdjustment, width - namePositionAdjustment);
americanX = random(namePositionAdjustment, width - namePositionAdjustment);
jobsX = random(namePositionAdjustment, width - namePositionAdjustment);
economyX = random(namePositionAdjustment, width - namePositionAdjustment);
cainY = random(namePositionAdjustment, height - namePositionAdjustment);
perryY = random(namePositionAdjustment, height - namePositionAdjustment);
bachmannY = random(namePositionAdjustment, height - namePositionAdjustment);
paulY = random(namePositionAdjustment, height - namePositionAdjustment);
romneyY = random(namePositionAdjustment, height - namePositionAdjustment);
americanY = random(namePositionAdjustment, height - namePositionAdjustment);
jobsY = random(namePositionAdjustment, height - namePositionAdjustment);
economyY = random(namePositionAdjustment, height - namePositionAdjustment);
for (int j = 0; j < words.size(); j++)
{
String word1 = words.get(j);
if (word1.contains("cain") || word1.contains("Cain"))
{
cainWords++;
}
if (word1.contains("perry") || word1.contains("Perry"))
{
perryWords++;
}
if (word1.contains("bachmann") || word1.contains("Bachmann"))
{
bachmannWords++;
}
if (word1.contains("paul") || word1.contains("Paul"))
{
paulWords++;
}
if (word1.contains("romney") || word1.contains("Romney"))
{
romneyWords++;
}
if (word1.contains("american") || word1.contains("American"))
{
americanWords++;
}
if (word1.contains("jobs") || word1.contains("Jobs"))
jobsWords++;
}
println("Number of Cain Words is " + cainWords);
println("Number of Perry Words is " + perryWords);
println("Number of Bachmann Words is " + bachmannWords);
println("Number of Paul Words is " + paulWords);
println("Number of Romney Words is " + romneyWords);
println("Number of American Words is " + americanWords);
println("Number of Jobs Words is " + jobsWords);
println("Number of Economy Words is " + economyWords);
}
______________________________________________________________________________________
Essentially what I'm trying to do is assign a random color for the names that pop up, but I'm unsure how to do that. Any help would be appreciated!
Also taking cool ideas how to make it more visually appealing =)
//Build an ArrayList to hold all of the words that we get from the imported tweets
ArrayList<String> words = new ArrayList();
ConfigurationBuilder cb;
Twitter twitter;
Query query;
//These two variables are used to tweak the size and position of the names
int nameSizeMultiplier = 20;
int namePositionAdjustment = 150;
//The number of times each word comes up in the Twitter search
int cainWords = 0;
int perryWords = 0;
int bachmannWords = 0;
int paulWords = 0;
int romneyWords = 0;
int americanWords = 0;
int jobsWords = 0;
int economyWords = 0;
//Get random x & y values for each word
float cainX = random(namePositionAdjustment, width - namePositionAdjustment);
float perryX = random(namePositionAdjustment, width - namePositionAdjustment);
float bachmannX = random(namePositionAdjustment, width - namePositionAdjustment);
float paulX = random(namePositionAdjustment, width - namePositionAdjustment);
float romneyX = random(namePositionAdjustment, width - namePositionAdjustment);
float americanX = random(namePositionAdjustment, width - namePositionAdjustment);
float jobsX = random(namePositionAdjustment, width - namePositionAdjustment);
float economyX = random(namePositionAdjustment, width - namePositionAdjustment);
float cainY = random(namePositionAdjustment, height - namePositionAdjustment);
float perryY = random(namePositionAdjustment, height - namePositionAdjustment);
float bachmannY = random(namePositionAdjustment, height - namePositionAdjustment);
float paulY = random(namePositionAdjustment, height - namePositionAdjustment);
float romneyY = random(namePositionAdjustment, height - namePositionAdjustment);
float americanY = random(namePositionAdjustment, height - namePositionAdjustment);
float jobsY = random(namePositionAdjustment, height - namePositionAdjustment);
float economyY = random(namePositionAdjustment, height - namePositionAdjustment);
void setup() {
//Set the size of the stage, and the background to black.
size(800,600);
background(0);
smooth();
cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("4gi93D8y2nkadut37JcfA");
cb.setOAuthConsumerSecret("pAURDlB571afctTySuIcEczwoXCLxyN48panfpDeSQ");
cb.setOAuthAccessToken("1532481-B4KJUH0uSjdaQE0jVgi3B3BoIk5XbyxqzuNhSTstx8");
cb.setOAuthAccessTokenSecret("NQAANu41p6p2MYHL3rCej7QGk7KTAr8UiGDs7UoMKo");
//Make the twitter object and prepare the query
twitter = new TwitterFactory(cb.build()).getInstance();
query = new Query("#GOP");
query.setRpp(100);
updateTwitter();
}
void draw() {
//If 30 seconds have passed, run it again:
if (frameCount % 900 == 0)
{
updateTwitter(); //This funcion runs the search again and updates the variables
// See below for the function definition
}
background(0);
//Draw a faint black rectangle over what is currently on the stage so it fades over time.
fill(0,1);
rect(0,0,width,height);
//Draw the words at the appropriate relative size (based on the number of times they occur)
fill(255,random(50,150));
textSize(nameSizeMultiplier*cainWords);
text("Cain", cainX, cainY);
fill(random(255));
textSize(nameSizeMultiplier*perryWords);
text("Perry", perryX, perryY);
textSize(nameSizeMultiplier*bachmannWords);
text("Bachmann", bachmannX, bachmannY);
textSize(nameSizeMultiplier*paulWords);
text("Paul", paulX, paulY);
textSize(nameSizeMultiplier*romneyWords);
text("Romney", romneyX, romneyY);
textSize(nameSizeMultiplier*americanWords);
text("American", americanX, americanY);
textSize(nameSizeMultiplier*jobsWords);
text("Jobs", jobsX, jobsY);
textSize(nameSizeMultiplier*economyWords);
text("Economy", economyX, economyY);
}
void updateTwitter()
{
//Try making the query request.
try {
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
String user = t.getFromUser();
String msg = t.getText();
Date d = t.getCreatedAt();
println("Tweet by " + user + " at " + d + ": " + msg);
//Break the tweet into words
String[] input = msg.split(" ");
for (int j = 0; j < input.length; j++) {
//Put each word into the words ArrayList
words.add(input[j]);
}
};
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
//Setup the new locations:
cainWords = 0;
perryWords = 0;
bachmannWords = 0;
paulWords = 0;
romneyWords = 0;
americanWords = 0;
jobsWords = 0;
economyWords = 0;
cainX = random(namePositionAdjustment, width - namePositionAdjustment);
perryX = random(namePositionAdjustment, width - namePositionAdjustment);
bachmannX = random(namePositionAdjustment, width - namePositionAdjustment);
paulX = random(namePositionAdjustment, width - namePositionAdjustment);
romneyX = random(namePositionAdjustment, width - namePositionAdjustment);
americanX = random(namePositionAdjustment, width - namePositionAdjustment);
jobsX = random(namePositionAdjustment, width - namePositionAdjustment);
economyX = random(namePositionAdjustment, width - namePositionAdjustment);
cainY = random(namePositionAdjustment, height - namePositionAdjustment);
perryY = random(namePositionAdjustment, height - namePositionAdjustment);
bachmannY = random(namePositionAdjustment, height - namePositionAdjustment);
paulY = random(namePositionAdjustment, height - namePositionAdjustment);
romneyY = random(namePositionAdjustment, height - namePositionAdjustment);
americanY = random(namePositionAdjustment, height - namePositionAdjustment);
jobsY = random(namePositionAdjustment, height - namePositionAdjustment);
economyY = random(namePositionAdjustment, height - namePositionAdjustment);
for (int j = 0; j < words.size(); j++)
{
String word1 = words.get(j);
if (word1.contains("cain") || word1.contains("Cain"))
{
cainWords++;
}
if (word1.contains("perry") || word1.contains("Perry"))
{
perryWords++;
}
if (word1.contains("bachmann") || word1.contains("Bachmann"))
{
bachmannWords++;
}
if (word1.contains("paul") || word1.contains("Paul"))
{
paulWords++;
}
if (word1.contains("romney") || word1.contains("Romney"))
{
romneyWords++;
}
if (word1.contains("american") || word1.contains("American"))
{
americanWords++;
}
if (word1.contains("jobs") || word1.contains("Jobs"))
jobsWords++;
}
println("Number of Cain Words is " + cainWords);
println("Number of Perry Words is " + perryWords);
println("Number of Bachmann Words is " + bachmannWords);
println("Number of Paul Words is " + paulWords);
println("Number of Romney Words is " + romneyWords);
println("Number of American Words is " + americanWords);
println("Number of Jobs Words is " + jobsWords);
println("Number of Economy Words is " + economyWords);
}
______________________________________________________________________________________
Essentially what I'm trying to do is assign a random color for the names that pop up, but I'm unsure how to do that. Any help would be appreciated!
Also taking cool ideas how to make it more visually appealing =)
1