import java.io.File; import java.io.*; import java.awt.*; import java.awt.image.*; import java.awt.font.*; import javax.imageio.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Text; import org.w3c.dom.Document; import org.w3c.dom.*; public class WeatherBug { static String tag[] = { "temp_f", "heat_index_f", "windchill_f", "wind_dir", "wind_mph", "relative_humidity", "icon_url_name", "weather", "observation_time" }; static String location[] = { "Albert Lea, MN 56007", "St. Paul, MN 55101", "Minneapolis, MN 55401", "Bemidji, MN 56601", "Duluth, MN 55802", "Fergus Falls, MN", "Mankato, MN 56001", "Marshall, MN 56258", "Rochester, MN 55904", "St. Cloud, MN 56301", "Winona, MN 55987" }; static String station[] = { "KAEL", "KSTP", "KMSP", "KBJI", "KDLH", "KFFM", "KMKT", "KMML", "KRST", "KSTC", "KONA" }; public static void main( String[] args ) { try { // // Pick off the first argument and check if that file exists // String xmlname = "KMSP"; if ( args.length > 0 && args[ 0 ].length() > 1 ) { xmlname = args[ 0 ]; } File xmlfile = new File( xmlname + ".xml"); if ( ! xmlfile.exists() ) { System.out.println( "XML Error...File = " + xmlname + ".xml Not Found" ); System.exit( 0 ); } // // Open the XML File and read using DOM parser // DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(xmlfile); doc.getDocumentElement().normalize(); // // Locate various elements in XML, prepare text for graphic // String[] data = new String[ tag.length ]; for ( int i = 0; i < tag.length; i++ ) { NodeList taglist = doc.getElementsByTagName( tag[ i ] ); int len = taglist.getLength(); if ( len < 1 ) { System.out.println( "XML Error...Tag = " + tag + " Not Found" ); System.exit( 0 ); } else { Element tagelem = (Element) taglist.item( 0 ); data[ i ] = new String( tagelem.getTextContent() ); } } // // Build the graphic using data from XML // int width = 166; int height = 90; // // Set up image buffer, paint the background light grey // BufferedImage bi = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB ); Graphics2D g2 = bi.createGraphics(); g2.setColor( Color.lightGray ); g2.fillRect( 0, 0, width, height ); // // Set up fonts for high resolution, define two text fonts // g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); Font fB = new Font( "Times", Font.BOLD, 14 ); Font fN = new Font( "arial", Font.PLAIN, 12 ); Font fX = new Font( "Times", Font.PLAIN, 55 ); FontRenderContext frc = g2.getFontRenderContext(); // // Draw the 6 text strings // // City, state, zip -- look up in list or allow override // -- first check if it is a command line arg (second arg) // -- if not, look it up in the arrays above // -- if not, set string to be an error message & continue String citystr = "Configuration Error"; Boolean found = false; if ( args.length > 1 && args[ 1 ].length() > 1 ) { citystr = args[ 1 ]; found = true; } if ( ! found ) { for ( int i = 0; i < station.length; i++ ) { if ( station[ i ].equals( xmlname ) ) { found = true; citystr = location[ i ]; } } } TextLayout t = new TextLayout( citystr, fB, frc ); g2.setColor( Color.black ); t.draw( g2, 3, 14 ); // Current weather conditions t = new TextLayout( data[ 7 ], fN, frc ); g2.setColor( Color.black ); t.draw( g2, 62, 29 ); // Temp with windchill String temp = data[ 0 ]; String t_type = " Chill Fact"; String chill = data[ 1 ]; if ( Integer.parseInt( temp ) >= 32 ) { t_type = " Heat Fact"; chill = data[ 2 ]; } if ( chill.equals("NA") ) { chill = temp; } t = new TextLayout( temp + "F (" + chill + t_type + ")", fN, frc ); g2.setColor( Color.black ); t.draw( g2, 62, 43 ); // Wind direction and speed dir = 3, speed = 4 if ( data[ 3 ].equals("NA") ) { data[ 3 ] = "Calm"; } if ( data[ 3 ].equals("Northeast") ) { data[ 3 ] = "NE"; } if ( data[ 3 ].equals("Northwest") ) { data[ 3 ] = "NW"; } if ( data[ 3 ].equals("Southeast") ) { data[ 3 ] = "SE"; } if ( data[ 3 ].equals("Southwest") ) { data[ 3 ] = "SW"; } String mph = data[ 4 ]; if ( mph.equals("NA") ) { mph = "0"; } int loc = mph.indexOf( "." ); if ( loc > 0 ) { mph = new String( mph.substring( 0, loc ) ); } t = new TextLayout( data[ 3 ] + " At " + mph + " MPH", fN, frc ); g2.setColor( Color.black ); t.draw( g2, 62, 57 ); // Relative humidity t = new TextLayout( data[ 5 ] + "% Humidity", fN, frc ); g2.setColor( Color.black ); t.draw( g2, 62, 71 ); // Date and time of last update String obstime = data[ 8 ].substring( 15 ); String ampm = obstime.substring( obstime.length()-6, obstime.length()-4 ); obstime = obstime.substring( 1, obstime.length() - 7 ); String ampm_str; ampm_str = " AM"; if ( ampm.indexOf( "am" ) < 0 ) { ampm_str = " PM"; } t = new TextLayout( "Updated: " + obstime + ampm_str, fN, frc ); g2.setColor( Color.black ); t.draw( g2, 3, 85 ); // // Read the weather icon and copy it into the image buffer // - if the icon file doesn't exist, paint a big red X // File icn = new File( "icons/" + data[ 6 ] ); if ( icn.exists() ) { BufferedImage icnFile = ImageIO.read( icn ); g2.drawImage( icnFile, 4, 16, 59, 74, 0, 0, 55, 58, null ); } else { t = new TextLayout( "X", fX, frc ); g2.setColor( Color.red ); t.draw( g2, 11, 64 ); } // // Write out the completed image as a PNG file // File bugfile = new File( xmlname + ".png" ); ImageIO.write( bi, "png", bugfile ); } catch (Exception e) { e.printStackTrace(); } } }