X

Vaibhav's Blog Space

Image of the Day - NASA + JavaFX

Guest Author

In last blog entry, we talked to have some blogs on Web Service to show how things go. Here is one of the simplest.

JavaFX is moving towards it's next release and work is ON. One of my friends came to my desk and he saw some animation of weather going here and there. He asked me " Is it some scientific data, data from NASA or something like that, if not what is this animation all about". Meantime, I checked the RSS feeds/WebService by NASA and I use one here to get something called "Image of the Day".

Its a big size image and so decide to showcase fullscreen example as well. Here goes the small code :

//Main.fx

package nasaimage;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

/\*\*
 \* @author Vaibhav
 \*/

var s: Stage;
public var vText: Boolean = true;
var ly: LocationY = LocationY{};
var im = ImageView {
    fitHeight: bind s.height
    fitWidth: bind s.width
    image: bind Image {
        url: ly.urlImage
    }
}

var rect = Rectangle {
   blocksMouse: true
   x: bind s.width - 25
   y: 20
   arcHeight: 4
   arcWidth: 4
   width: 20,
   height: 20
   stroke: Color.GRAY
   strokeWidth: 2
   onMouseClicked: function( e: MouseEvent ):Void {
       FX.exit();
   }
}
/\* close button 'x' in title bar \*/
var close = Text {
   fill: Color.WHITE
   font: Font {
       name: "Arial Bold"
       size: 18
   }
   x: bind s.width - 20
   y: 35
   content: "x"
}

var loadingText = Text {
   visible: bind vText
   fill: Color.BLACK
   font: Font {
       name: "Arial Bold"
       size: 20
   }
   x: 20
   y: 20
   content: "Loading Image from NASA WebService..."
}

function run() {
s = Stage {
    title: "Nasa Image"
    width: 300
    height: 300
    fullScreen: true   
    scene: Scene {
        content: [
            loadingText,im, rect, close
        ]
    }
}
}

//LocationY.fx

package nasaimage;

import java.lang.Exception;
import javafx.data.pull.PullParser;
import javafx.data.xml.QName;
import javafx.io.http.HttpRequest;

/\*\*
 \* @author Vaibhav
 \*/

public class LocationY {
    public var urlImage:String;
    var url = bind "http://www.nasa.gov/rss/lg_image_of_the_day.rss";
    var p: PullParser;
    var h: HttpRequest;
    init {
        if (url.length() > 0) {
            h = HttpRequest {
                location: url
                onException: function(exception: Exception) {
                    print("Please check the internet connectivity or Data Input");
                }
                onInput: function(input) {
                    p = PullParser {
                        input: input
                        onEvent: function(event) {
                            if ((event.type == PullParser.END_ELEMENT)) /\* and (event.qname.name == "current_conditions")) \*/{
                                if (event.qname.name == "enclosure") {
                                    urlImage = event.getAttributeValue(QName{name:"url"});
                                 }
                            }
                        }
                    };
                    p.parse();
                    p.input.close();
                }
                onDone: function() {
                Main.vText = false;
                }
            };
            h.start();
        }
    }
}

Don't ask me why I am using LocationY.fx as filename. Sometime back, I made my first webservice with name LocationY and from that time, I am copying the same file.

Last day Photo was more awesome than this. I guess it was something related with stars :). As talked in the prev. blog about the use of OnDone() method, here it is. Before loading of image, you will see a text   'Loading Image from NASA WebService...'. This text vanishes when image get loaded because we have called its visible: false in OnDone() method of HTTP.

I hope you love this short code and start of webservices. Thanks to NASA website to provide this RSS feed :).

Join the discussion

Comments ( 13 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha