processing:thermometer_example
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
processing:thermometer_example [2013/08/04 02:06] – [Thermometer example] mithat | processing:thermometer_example [2013/08/04 03:29] – [Adding a graphic meter] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
{{: | {{: | ||
Line 12: | Line 12: | ||
You won't find much description below. I may add some later, but my intent here is just to do a structured code dump. | You won't find much description below. I may add some later, but my intent here is just to do a structured code dump. | ||
- | <WRAP center round important | + | <WRAP center round info 60%> |
Because applet support has been removed in Processing 2 and Processing.js doesn' | Because applet support has been removed in Processing 2 and Processing.js doesn' | ||
</ | </ | ||
===== Getting files from the Internet ===== | ===== Getting files from the Internet ===== | ||
+ | {{: | ||
+ | |||
The built-in function '' | The built-in function '' | ||
Line 93: | Line 95: | ||
===== Scraping XML for data ===== | ===== Scraping XML for data ===== | ||
+ | {{: | ||
- | Grabbing selected data from an XML source is easy with the '' | + | Grabbing selected data from an XML source is easy with Processing' |
<file java get_xml_page.pde> | <file java get_xml_page.pde> | ||
Line 159: | Line 162: | ||
===== A digital thermometer ===== | ===== A digital thermometer ===== | ||
+ | |||
+ | {{: | ||
Fonts are graphics in Processing. To get the following to work, you'll need to create the *.vlw file using the //Tools > Create Font...// menu item. | Fonts are graphics in Processing. To get the following to work, you'll need to create the *.vlw file using the //Tools > Create Font...// menu item. | ||
Line 297: | Line 302: | ||
===== Adding a graphic meter ===== | ===== Adding a graphic meter ===== | ||
+ | |||
+ | {{: | ||
Representing quantities graphically. Note also the use of Javadoc-style documentation comments for the functions. | Representing quantities graphically. Note also the use of Javadoc-style documentation comments for the functions. | ||
Line 339: | Line 346: | ||
| | ||
background(# | background(# | ||
- | | + | |
- | units.getString(ATTRIBUTE_UNIT_TEMPERATURE), | + | int temperature = condition.getInt(ATTRIBUTE_CONDITION_TEMPERATURE); |
- | drawMeter(70, -40, 120, 10, height-2*BORDER, | + | |
+ | | ||
+ | drawMeter(temperature, -40, 120, 10, height-2*BORDER, | ||
} | } | ||
catch (Exception e) { | catch (Exception e) { | ||
Line 358: | Line 367: | ||
/** | /** | ||
- | * Render | + | * Render |
* | * | ||
* @param temperature the temperature to be drawn (int) | * @param temperature the temperature to be drawn (int) | ||
Line 378: | Line 387: | ||
/** | /** | ||
- | * Render | + | * Render |
* | * | ||
* @param val the value to be drawn (float) | * @param val the value to be drawn (float) | ||
Line 394: | Line 403: | ||
// calculate height of indicator in pixels | // calculate height of indicator in pixels | ||
float barHeight=map(val, | float barHeight=map(val, | ||
- | println(" | + | |
// draw the indicator | // draw the indicator | ||
strokeWeight(1); | strokeWeight(1); | ||
Line 400: | Line 409: | ||
fill(# | fill(# | ||
rect(x, y+(h-barHeight), | rect(x, y+(h-barHeight), | ||
+ | |||
// draw the frame | // draw the frame | ||
noFill(); | noFill(); | ||
Line 406: | Line 416: | ||
} | } | ||
</ | </ | ||
+ | |||
===== Updating the data ===== | ===== Updating the data ===== | ||
- | Don't sleep. Poll instead. | + | Don't sleep. Poll instead. The following updates the temperature display every minute. |
<file java thermometer_update.pde> | <file java thermometer_update.pde> | ||
Line 414: | Line 425: | ||
* as well as numerically; | * as well as numerically; | ||
| | ||
- | * Copyright (C) 2012 Mithat Konar | + | * Copyright (C) 2012-2013 Mithat Konar |
*/ | */ | ||
- | // Change | + | // Constants: |
- | // See http:// | + | // change |
+ | // (see http:// | ||
final String URL = " | final String URL = " | ||
- | // These should only need changing if Yahoo! changes the RSS feed structure. | + | // path in XML data to current conditions: |
- | final String | + | final String |
+ | |||
+ | // attribute name in element above for current temperature: | ||
final String ATTRIBUTE_CONDITION_TEMPERATURE = " | final String ATTRIBUTE_CONDITION_TEMPERATURE = " | ||
- | final String | + | |
+ | // path in XML data to element that holds units of measurement: | ||
+ | final String | ||
+ | |||
+ | // attribute name in element above that holds units for temperature: | ||
final String ATTRIBUTE_UNIT_TEMPERATURE = " | final String ATTRIBUTE_UNIT_TEMPERATURE = " | ||
+ | // misc. | ||
final int BORDER=10; | final int BORDER=10; | ||
- | int oldMinute; | + | // Global variables: |
+ | int oldMinute; | ||
+ | // Functions: | ||
void setup() { | void setup() { | ||
size(200, 100); | size(200, 100); | ||
smooth(); | smooth(); | ||
+ | background(# | ||
oldMinute = minute(); | oldMinute = minute(); | ||
- | background(# | ||
updateTemp(); | updateTemp(); | ||
} | } | ||
void draw() { | void draw() { | ||
- | // updateTemp every minute. | + | // updateTemp every minute |
int currentMinute = minute(); | int currentMinute = minute(); | ||
- | if (oldMinute != currentMinute) | + | if (oldMinute != currentMinute) { |
- | | + | |
updateTemp(); | updateTemp(); | ||
oldMinute = currentMinute; | oldMinute = currentMinute; | ||
Line 449: | Line 469: | ||
} | } | ||
+ | /** | ||
+ | * Update the display of the current temperature. | ||
+ | * | ||
+ | * @return void | ||
+ | */ | ||
void updateTemp() { | void updateTemp() { | ||
try { | try { | ||
- | | + | |
- | background(# | + | |
- | XMLElement | + | |
- | | + | |
- | | + | |
background(# | background(# | ||
drawDigitalTemp(condition.getInt(ATTRIBUTE_CONDITION_TEMPERATURE), | drawDigitalTemp(condition.getInt(ATTRIBUTE_CONDITION_TEMPERATURE), | ||
- | | + | |
drawMeter(70, | drawMeter(70, | ||
+ | |||
+ | // uncomment println below for debugging. | ||
+ | // println(" | ||
+ | // " | ||
} | } | ||
catch (Exception e) { | catch (Exception e) { | ||
println(" | println(" | ||
println(e); | println(e); | ||
- | | + | |
// Turn the canvas red with a big ' | // Turn the canvas red with a big ' | ||
background(# | background(# | ||
Line 473: | Line 501: | ||
} | } | ||
- | void drawDigitalTemp(int | + | /** |
- | /* | + | * Render the temperature |
- | | + | * |
+ | | ||
+ | * @param units the units of measure used for temperatre | ||
+ | * @param | ||
+ | * @param | ||
+ | * | ||
+ | * @return void | ||
*/ | */ | ||
+ | void drawDigitalTemp(int temperature, | ||
{ | { | ||
PFont font; | PFont font; | ||
font = loadFont(" | font = loadFont(" | ||
- | fill(# | ||
textFont(font, | textFont(font, | ||
textAlign(CENTER, | textAlign(CENTER, | ||
+ | fill(# | ||
text(temperature + " | text(temperature + " | ||
} | } | ||
- | void drawMeter(float val, float minimum, float maximum, int w, int h, int x, int y) | + | /** |
- | /* | + | |
- | | + | * |
- | | + | * @param |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
+ | * @param | ||
+ | * | ||
+ | * @return void | ||
*/ | */ | ||
+ | void drawMeter(float val, float minimum, float maximum, int w, int h, int x, int y) | ||
{ | { | ||
// calculate height of indicator in pixels | // calculate height of indicator in pixels |
processing/thermometer_example.txt · Last modified: 2013/08/04 03:55 by mithat