Code
//The Weather Machine
//This program parses the accuweather website and displays
//the information (Overall Conditions, RealFeel Temperature, Visibility, Wind Speed and Humidity) visually.
//Each element affects the overall visual representation, as below
//Background - Overall Conditions
//Flower Color - RealFeel Temperature
//No of Flowers - Visibility
//Flower Trail Length - Humidity
//Speed of Flower ACROSS screen - Wind Speed
//Users can enter any American zipcode, to see a visual representation of the weather in that area
//I have not covered all the possible 'Overall Conditions' that the site generates - so you might on occasion, see the default background color of white
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
public class Weather extends BApplet
{
HTMLReader html;
String displayedText = new String();
boolean flCond, flTemp, flHumid, flWind, flVis = false; //for handling the values taken from site
String cond; //overall conditions
float wind,temp,vis,humid; //wind,temperature,, visibility and humidity
int r1,g1,b1; // oreground color - on temperature
int r2,g2,b2; //background color - on overall conditions
int len; // this is dependent on the humidity value
boolean enterflag = false;
Vector flowernumbers = new Vector();
void setup()
{
size(600,650);
framerate(12);
smooth();
startParse("http://wwwa.accuweather.com/adcbin/public/curcon_local.asp?partner=accuweather&zipcode=30313&metric=0");
len=(int) (humid*2/3); //the length of trail dependent on humidity value - see function drawFlower in class Flower
strokeTemp(temp); //sets stroke dependent on temperature
bgCond(cond); //background dependent on overall conditions
Flower f;
for(int i=0;i<vis;i++)
{
f=new Flower();
flowernumbers.add(f);
}
}
void loop()
{
background(r1,g1,b1); //the bg function in setup sets the color, while here am just calling the set color
Flower f=new Flower();
BFont m = loadFont("Futura-Light.vlw.gz");
textFont (m,24);
fill(0);
text("Enter any zipcode to see current weather representation",20,30);
int len = displayedText.length();
for(int i = 0; i< len; i++)
{
text(displayedText,20,60); //The position of answers
}
for(int i=0;i<flowernumbers.size();i++)
{
//println("flowerloop = "+i);
f=(Flower)(flowernumbers.get(i));
f.drawFlower();
}
if(enterflag)
{
String url = "http://wwwa.accuweather.com/adcbin/public/curcon_local.asp?partner=accuweather&zipcode="+displayedText+"&metric=0";
println(url);
startParse(url);
len=(int) (humid*2/3); //the length of trail dependent on humidity value - see function drawFlower in class Flower
strokeTemp(temp); //sets stroke dependent on temperature
bgCond(cond); //background dependent on overall conditions
flowernumbers = new Vector();
//println("flowernumbers size = "+flowernumbers.size());
//println("Visibility = "+vis);
Flower f1;
for(int i=0;i<vis;i++)
{
f1=new Flower();
flowernumbers.add(f1);
}
enterflag = false;
}
fill(0);
rect(0,530,650,120);
text("COLOR CODE", 10,527);
textFont(m,18);
fill(255);
text("Haze,Fog or Light Fog",25,560);
fill(197,198,217);
rect(10,550,10,10);
fill(255);
text("Sunny or Partly Sunny",25,580);
fill(251,244,118);
rect(10,570,10,10);
fill(255);
text("Overcast, Rain or Light Rain",185,560);
fill(16,50,114);
rect(170,550,10,10);
fill(255);
text("Cloudy or Mostly Cloudy",185,580);
fill(164,182,198);
rect(170,570,10,10);
fill(255);
text("Flower Temperature from 100 to 0",370,560);
BImage b;
b = loadImage("grad.jpg");
image(b, 370, 570);
fill(255);
text("No. of flowers is visibility in miles and their movement across screen is an approx. of windspeed in miles",10,610);
text("Flower trail length determined by humidity in air",10,630);
}
void startParse(String url)
{
html = new HTMLReader();
TextParserCallbacks cb = new TextParserCallbacks();
html.parse(url, cb);
}
class TextParserCallbacks extends HTMLEditorKit.ParserCallback
{
public void handleText(char[] tex, int pos) //function finds the first word, then resets state and gets the second word - finds humidity & gets 87
{
String word = new String(tex);
boolean pass = false; //this is changed when each keyword is found
if(word.indexOf("units") != -1)
{
flCond = true;
pass = true;
}
if(word.indexOf("Humidity") != -1)
{
flHumid = true;
pass = true;
}
if(word.indexOf("Visibility") != -1)
{
flVis = true;
pass = true;
}
if(word.indexOf("Temp") != -1)
{
flTemp = true;
pass = true;
}
if(word.indexOf("Wind Speed") != -1)
{
flWind = true;
pass = true;
}
if((flCond)&(!(pass))) //once the second word is got, boolean resets
{
flCond = false;
cond = word;
println("Overall Conditions "+cond);
}
if((flTemp)&(!(pass)))
{
flTemp = false;
int k = word.length();
word = word.substring(0,k-2); //to convert string to float, first need to remove the characters at end
temp = Float.parseFloat(word); //converts string to float
println("Temperature "+temp);
}
if((flWind)&(!(pass)))
{
flWind = false;
int k = word.length();
word = word.substring(0,k-3); //same as temp
wind = Float.parseFloat(word);
println("Wind "+wind);
}
if((flVis)&(!(pass)))
{
flVis = false;
int k = word.length();
word = word.substring(0,k-2); //same as temp
vis = Float.parseFloat(word);
println("Visibility "+vis);
}
if((flHumid)&(!(pass)))
{
flHumid = false;
humid = Float.parseFloat(word);
println("Humidity "+humid);
}
}
}
void strokeTemp(float temp) //Sets the stroke according to temperature
{
float w1,w2,w3;
w1= 1/sqrt(1+pow(temp/30,4));
w2= 1/sqrt(1+pow((temp-50)/10,4));
w3= 1/sqrt(1+pow((temp-100)/30,4));
r2= (int)(255*w3);
g2= (int)(255*w2);
b2= (int)(255*w1);
}
void bgCond(String cond) // Sets background according to Overall Conditions
{
if((cond.equals("HAZE")) || (cond.equals("LIGHT FOG")) || (cond.equals("FOG")) || (cond.equals("LGT.SNOW/FOG")))
{
r1 = 197;
g1 = 198;
b1 = 217;
background(r1,g1,b1);
}
else if((cond.equals("PARTLY SUNNY")) || (cond.equals("SUNNY")))
{
r1 = 251;
g1 = 244;
b1 = 130;
background(r1,g1,b1);
}
else if((cond.equals("OVERCAST")) || (cond.equals("RAIN")) || (cond.equals("LIGHT RAIN")))
{
r1 = 16;
g1 = 50;
b1 = 114;
background(r1,g1,b1);
}
else if((cond.equals("CLOUDY")) || (cond.equals("MOSTLY CLOUDY")))
{
r1 = 164;
g1 = 182;
b1 = 198;
background(r1,g1,b1);
}
else
{
r1 = 255;
g1 = 255;
b1 = 255;
background(r1,g1,b1);
}
}
class Flower
{
int xpos,ypos;
int petals;
Flower()
{
xpos = (int)random(599);
ypos = (int)random(399);
petals = (int)random(2,5); //if removing this, change the value in the x and y fields back to 4
}
void drawFlower()
{
for(int h=len-1;h>=0;h-=3)
{
float theta;
int x,y;
int r3,g3,b3;
r3 = r2+(r1-r2)*h/len;
g3 = g2+(g1-g2)*h/len;
b3 = b2+(b1-b2)*h/len;
stroke(r3,g3,b3);
if(h < 3) //if im dumping this, remember to put back strokeweight as 1 in the loop
{
strokeWeight(3);
}
else
{
strokeWeight(1);
}
for(int k=0;k<=300;k++) //for odd petals, use same no in sin | for even use half the no
{
theta=(2*PI*k)/300;
x=xpos+(int)(20*sin(petals*(theta-2*PI*ypos/50))*cos(theta)); //math formulae to draw 'rhodonea'(flowerette in polar coordinates)
y=ypos-h+(int)(20*sin(petals*(theta-2*PI*ypos/50))*sin(theta));
point(x,y);
}
}
ypos+=5+(int)(0.02*ypos); //randomize position of figure - using the wind as an input to guide the path. the .02 is acceleration due to gravity :D
xpos+=wind;
if((ypos > 599)||(xpos > 599))
{
ypos = (int)random(399);
xpos = (int)random(599);
}
}
}
void keyPressed() //Action Listener
{
char typedKey = (char)key;
if(typedKey == 10)
{
enterflag = true;
}
else if(typedKey == 8)
{
String add = "";
for(int k = 0 ;k < displayedText.length()-1 ; k++)
{
add += displayedText.charAt(k);
}
displayedText = add;
}
else
{
displayedText = displayedText+typedKey;
}
}
}
Project 4: Create an applet that dynamically does something to one or more web pages.
Statement:Hypertext was conceived as a computer-aided form of reading and writing whose structure matches that of the human mind (a tangled web of association), thus enabling humans to make sense of the exponential growth of knowledge experienced in the 20th century. The World-Wide Web, while a rather anemic implementation of hypertext, makes up for these deficiencies by providing us a sneak peak at what it might be like to have a truly global repository of knowledge. But making sense of the world is not just a matter of structure, but of process, of the dynamic construction of meaning. And as we've been discovering together, computation is fundamentally a process medium. What would you do to the web? Create an applet that dynamically does something to one or more web pages (e.g. collage, systematic distortion, re-layout, ironic superposition, etc.).
Project 04 : The Weather Machine