// Ruqian Zhou // Section A // ruqianz@andrew.cmu.edu // Copyright Ruqian Zhou, October 2010, Pittsburgh, PA 15213, All Rights Reserved PFont f1; PImage worldMap; int boxWidth, boxHeight; int diameter; int index; String [] country = { "Canada", "United States", "Mexico", "Costa Rica", "Colombia", "Chile", "Iceland", "United Kingdom", "France", "Morocco", "Ghana", "Congo", "Malawi", "South Africa", "Yemen", "Iraq", "Turkey", "Kazakhstan", "Russia", "India", "China", "Japan", "Thailand", "Singapore", "Australia" }; float [] hpi = { 39.4, 30.7, 55.6, 76.1, 66.1, 49.7, 38.1, 43.3, 43.9, 56.8, 37.1, 32.4, 34.5, 29.7, 48.1, 42.6, 41.7, 38.5, 34.5, 50.3, 57.1, 43.3, 50.9, 48.2, 36.6 }; int [] rank = { 89, 114, 23, 1, 6, 46, 94, 74, 71, 21, 100, 112, 107, 118, 50, 79, 83, 91, 108, 35, 20, 75, 41, 49, 102 }; int [] latitude = { -60, -38, -23, -10, -4, 30, -65, -54, -46, -32, -8, 1, 13, 29, -15, -33, -39, -48, -60, -20, -35, -36, -15, -1, 27 }; int [] longitude = { -95, -97, -102, -84, -72, -71, -18, -2, 2, -5, -2, 15, 34, 24, 48, 44, 35, 68, 100, 77, 105, 138, 100, 103, 133 }; void setup( ) { size( 889, 600 ); smooth( ); f1 = loadFont( "f1.vlw" ); worldMap = loadImage( "worldmap.png" ); boxWidth = 130; boxHeight = 60; index = -1; } void draw( ) { image( worldMap, 0, 0 ); drawTitle(); translate( width/2, height/2.1 ); drawCountryLocation( ); showData(); } void keyPressed( ) { if ( key == CODED ) { if ( keyCode == LEFT) { index--; if (index < 0 ) { index = hpi.length - 1; } } else if ( keyCode == RIGHT ) { index++; if ( index == hpi.length ) { index = 0; } } } } void showData( ) { if ( index >= 0 && index < hpi.length ) { fill( 0, 80 ); float x = longitude[index]*2.18; float y = latitude[index]*2.88; rect( x, y, boxWidth, boxHeight ); textFont( f1, 16 ); fill( 255, 250, 222 ); text( country[index], x+10, y+15 ); textFont( f1, 14 ); text( "HPI: " + hpi[index], x+20, y+40 ); text( "Rank: " + rank[index], x+20, y+55 ); println(index); } } void drawCountryLocation( ) { for ( int i = 0; i < hpi.length; i++ ) { diameter = int( hpi[i] ) / 4; fill( 240, 75, 75 ); noStroke( ); ellipse ( longitude[i]*2.18, latitude[i]*2.88, diameter, diameter ); } } void drawTitle() { fill( 0, 80 ); noStroke( ); rect( 15, height-height/5, width-30, height/5-15 ); textFont( f1, 32 ); fill( 255, 250, 222 ); text( "The (un)Happy Planet Index 2.0", 30, height/1.13 ); textFont( f1, 18 ); text( "HPI = Happy Life Years / Ecological Footprint", 30, height/1.08 ); textFont( f1, 15 ); text( "Press the [ left ] or [ right ] arrow keys", width/1.5, height/1.12 ); text( "to check each country's HPI and rank.", width/1.5, height/1.08 ); }