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 01:31] – [A digital thermometer] mithat | processing:thermometer_example [2013/08/04 03:29] – [Adding a graphic meter] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
{{: | {{: | ||
Line 10: | Line 10: | ||
* How to invoke an action periodically. | * How to invoke an action periodically. | ||
- | You'll not 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 info 60%> | <WRAP center round info 60%> | ||
- | The code here was written for Processing | + | Because applet support has been removed in Processing 2 and Processing.js doesn' |
</ | </ | ||
- | <WRAP center round important 60%> | + | ===== Getting files from the Internet ===== |
- | Because [[http://wiki.processing.org/ | + | {{: |
- | </ | + | |
- | ===== Getting files from the Internet ===== | ||
The built-in function '' | The built-in function '' | ||
Line 97: | 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 163: | 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 287: | Line 288: | ||
/** | /** | ||
- | * Render the temperature at a given canvas location. | + | * Render the temperature |
*/ | */ | ||
void drawDigitalTemp(int temperature, | void drawDigitalTemp(int temperature, | ||
Line 302: | Line 303: | ||
===== Adding a graphic meter ===== | ===== Adding a graphic meter ===== | ||
- | Representing quantities graphically. | + | {{: |
+ | |||
+ | Representing quantities graphically. Note also the use of Javadoc-style documentation comments for the functions. | ||
<file java thermometer_meter.pde> | <file java thermometer_meter.pde> | ||
Line 309: | Line 312: | ||
* 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; | ||
+ | // Functions: | ||
void setup() { | void setup() { | ||
size(200, 100); | size(200, 100); | ||
Line 329: | Line 341: | ||
try { | try { | ||
- | | + | |
- | | + | |
- | | + | |
+ | | ||
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 351: | Line 366: | ||
} | } | ||
+ | /** | ||
+ | * Render a temperature numerically at a given canvas location. | ||
+ | * | ||
+ | * @param temperature the temperature to be drawn (int) | ||
+ | * @param units the units of measure used for temperatre (String) | ||
+ | * @param x the horizontal location of the display (int) | ||
+ | * @param y the vertical location of the display (int) | ||
+ | * | ||
+ | * @return void | ||
+ | */ | ||
void drawDigitalTemp(int temperature, | 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) | + | /** |
- | /* val: the value to be drawn | + | * Render a value on a vertical meter. |
- | * minimum: val's value that corresponds to no deflection. | + | * |
- | * minimum: val's value that corresponds to maximum deflection. | + | * @param |
- | * w: the width of the meter | + | |
- | * h: the height of the meter | + | |
- | * x, y: the location of the meter | + | |
+ | | ||
+ | | ||
+ | * @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 | ||
float barHeight=map(val, | float barHeight=map(val, | ||
- | println(" | + | |
// draw the indicator | // draw the indicator | ||
strokeWeight(1); | strokeWeight(1); | ||
Line 378: | Line 409: | ||
fill(# | fill(# | ||
rect(x, y+(h-barHeight), | rect(x, y+(h-barHeight), | ||
+ | |||
// draw the frame | // draw the frame | ||
noFill(); | noFill(); | ||
Line 384: | 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 392: | 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 427: | 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 451: | 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