Get All-Weather Forecasts With Yahoo APIs

By Vaibhav Choudhary, May 15, 2009

With the JavaFX technology it is easy to build your own customized Weather forecasting tool by using Yahoo Webservices.

Understanding the Code

The code shown in Figure 1 creates Weather forecasting, which enables the user to see three-Day weather information from Yahoo API's. Forecast conditions will be animated in JavaFX with FXZ files.

Source Code

public class Weather extends CustomNode {
   public var city:String;
   public var tempToday: Integer=0;
   public var codeToday: String;
   public var cityID: Integer = 0;
   var url = "http://weather.yahooapis.com/forecastrss?p={city}";
   var p: PullParser;
   var h: HttpRequest;
   public var tempforecast_next_high:Integer[];
   public var tempforecast_next_low:Integer[];
   public var tempforecast_code:String[];
   public var tempforecast_day: String[];
   public var tempforecast_condition: String[];
   public var tempforecast_today: Integer;
   public var tempforecast_today_condition: String;

Figure 1: Weather.fx Class

Changing data member information such as a city name is easy in Main.fx

This sample enables the user to view weather data in a 3-D bar chart. This weather data is taken at runtime from Yahoo Weather APIs. The animation of the conditions is created in FXZ file format. FXZ files have layers of image data from which you can easily animate any condition, such as Thunder.fx.

Source Code

public class Thunder extends CustomNode {
    var fxdContent = FXDLoader.loadContent(
    "{__DIR__}thunders.fxz");

    var im1 = fxdContent.getNode("th1");
    var im2 = fxdContent.getNode("th2");
    var gp = Group {
        translateY: 10
        opacity: bind op1
        content: [im2]
    }

    var im3 = fxdContent.getNode("im3");
    var gp1 = Group {
        translateY: 10
        opacity: bind op2
        content: [im3]
    }
    var op1 = 0.0;
    var op2 = 1.0;
    var t = Timeline {
        repeatCount: Timeline.INDEFINITE
        keyFrames: [
            KeyFrame {
                time: 1s
                canSkip: true
                values: [
                    op1 => 1.0 tween Interpolator.LINEAR,
                    op2 => 0.0 tween Interpolator.LINEAR
                ]
            }
        ]
    }
    init{
        t.playFromStart();
    }
    public override function create(): Node {
        return Group {
            content: [im1, gp, gp1]
        };
    }
}