Friday May 22, 2009

Yahoo Map Navigation...JavaFX

Extremely sorry for not responding to the question in few of the last blog posts. Here is the Yahoo Map navigation. I started liking Yahoo Web Service, it is so cool.

Please use your App-ID, if you are putting this code somewhere else, because this code uses default Yahoo Key.  Now, any web service work need these API use :

PullParser - A parser for structured Data. JDK6 can also do parsing in same way.

HttpRequest - Can use for Async HTTP or RestFul web services.

This code is so easy and monotonous that you will start copying it after 2-3 go :). Here it goes for Yahoo Map + Navigation code I have added addition here for both key and mouse.

JNLP for the same : 


Main.fx :
package yahoomapnavigation;

import java.lang.Math;
import javafx.scene.Cursor;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import yahoomapnavigation.MapLocate;


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

var ml: MapLocate = MapLocate{zip:"10001"}; // 10001 - Newyork, i guess
var x1: Number = 0;
var y1: Number = 0;
var im: Image = bind Image {
    url: ml.location
}
/\* Image of the map from Yahoo WS \*/
var imview = ImageView {
    cache: true
    translateX: bind x1
    translateY: bind y1
    image: bind im
    cursor: Cursor.MOVE
    /\* Navigation from Mouse event\*/
    onMouseDragged: function( e: MouseEvent ):Void {
        if(e.dragX < 0 and x1 < 0) {
            x1 = x1 + 5;
        }
        if(e.dragX > 0 and Math.abs(x1  -  240) < im.width) {
            x1 = x1 - 5;
        }
        if(e.dragY < 0 and y1 < 0 ) {
            y1 = y1 + 5;
        }
        if(e.dragY > 0 and Math.abs(y1  -  320) < im.height) {
            y1 = y1 - 5;
        }
    }
    /\* Navigation from key event \*/
    onKeyPressed: function( e: KeyEvent ):Void {
        if(e.code == KeyCode.VK_LEFT)
                    {
            if(x1 < 0) {
                x1+=10;
            }
        }
        if(
                    e.code == KeyCode.VK_RIGHT)
                    {
            if(Math.abs(x1  -  240) < im.width) {
                x1-=10;
            }
        }
        if(
                    e.code == KeyCode.VK_DOWN)
                    {
            if(Math.abs(y1  -  320) < im.height) {
                y1-=10;
            }
        }
        if(e.code == KeyCode.VK_UP)
                    {
            if(y1 < 0) {
                y1+=10;
            }
        }
    }
};
Stage {
    title: "Yahoo Map Navigation"
    width: 240
    height: 320
    scene: Scene {
        content: [
            imview
        ]
    }
}
MapLocate.fx :
package yahoomapnavigation;

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

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

/\* Class to show Map from Yahoo Web service, input zip code \*/
public class MapLocate {
    public var location: String;
    public var zip: String;
    public var isguid: Boolean = false;
    var url = bind "http://local.yahooapis.com/MapsService/V1/mapImage?appid=P6pToNnV34GfP9zgTALgVW3CUTL5qHTnKjz5bLXPikqNjcMZTkF6h1xsnhm.P1WIs3U-&zip={zip}";
    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/Data Input");
                }

                onInput: function(input) {

                    p = PullParser {
                        input: input
                        onEvent: function(event) {

                            if ((event.type == PullParser.END_ELEMENT)) {
                                if (event.qname.name == "Result") {
                                    isguid = true;
                                    location = event.text;
                                }
                            }
                        }
                    };
                    p.parse();
                    p.input.close();
                }
                onDone: function() {
                  
                }
            };
            h.enqueue();
        }
    }
}
</pre>

Please let me know if there is any issue, with JNLP or code :). Have fun !

Monday May 11, 2009

Image of the Day - NASA + JavaFX

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 :).

About

Hi, I am Vaibhav Choudhary working in Sun. This blog is all about simple concept of Java and JavaFX.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today