Saturday Dec 06, 2008

Mobile + Desktop JavaFX !

Just want to put the attention on one important point regarding the coding in JavaFX. Everyone who is using JavaFX has to understand that this technology is supported on Mobile, Desktop and TV(tomorrow it will be). So, its important as a developer to write Generic Applications which run across the platform. In such cases you need to know how to adjust some silly things in the code, like so desktop it can be a big font whereas you need to reduce font size for mobile or same case with images. 

Here is one sample code, which can show you how to do :

package sample11;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;

var mobile = FX.getProperty("javafx.me.profiles");
var isMobile = (mobile != null);
var s = 24;
var content = "Default";
if(isMobile == true) {
    s = 10;
    content = "Content in Mobile"
}
else { 
    s = 20;
    content = "Content on Desktop"
}

Stage {
    title: "Checking the Font"
    width: 350
    height: 280
    scene: Scene {
        content: Text {
            font : bind Font {
                size : s
            }
            x: 10, y: 30
            content: bind content
        }

    }
}

Run it on Desktop and on Mobile and see the different output.

(How to run on Mobile ? ---> Netbeans6.5 -> YourProject -> Right Click ->Properties -> Run -> Run as Mobile Emulator)

This feature has been nicely used in this sample :  InterestingPhotos.

Monday Aug 04, 2008

Java API + Flickr API

I decided to play with Flickr API's for Java FX coding. But in between I found myself in hell and I started with Java :-). As all of you know Flickr Support hell lot of API to view Image, to search image, to search comment, Image translation, Image upload and many more. Check out here for detailed API. Now using these API's are not at all tough, because its all a game of XML.

Here I have written a small code, which do this :

1. It search one image(it can work for more than one image) from search API of Flickr.
2. It writes the search data on a XML, which I am copying at D:\\Hello1.xml.
3. And finally the code is using XML parsing techniques to get the information required for image view.
4. Then I use JDK6 feature of Desktop and open the default browser with the parsed URL. And
Congratulations, you can see the image.

Code, can look little big because of bad coding and writing lot of repetitive things :D.  

package flickrapp;

import java.awt.Desktop;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.\*;
import java.net.\*;
import java.util.\*;

public class Main {

    public static void main(String args[]) throws Exception {
        URLConnection uc = new URL("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=e3471e67d4ac10c64055420d9b211b4f&per_page=1&text=Bangalore").openConnection();
        DataInputStream dis = new DataInputStream(uc.getInputStream());
        FileWriter fw = new FileWriter(new File("D:\\\\Hello1.xml"));
        String nextline;
        String[] servers = new String[10];
        String[] ids = new String[10];
        String[] secrets = new String[10];
        while ((nextline = dis.readLine()) != null) {
            fw.append(nextline);
        }
        dis.close();
        fw.close();
        String filename = "D:\\\\Hello1.xml";
        XMLInputFactory factory = XMLInputFactory.newInstance();
        System.out.println("FACTORY: " + factory);

        XMLEventReader r = factory.createXMLEventReader(filename, new FileInputStream(filename));
        int i = -1;
        while (r.hasNext()) {

            XMLEvent event = r.nextEvent();
            if (event.isStartElement()) {
                StartElement element = (StartElement) event;
                String elementName = element.getName().toString();
                if (elementName.equals("photo")) {
                    i++;
                    Iterator iterator = element.getAttributes();

                    while (iterator.hasNext()) {

                        Attribute attribute = (Attribute) iterator.next();
                        QName name = attribute.getName();
                        String value = attribute.getValue();
                        System.out.println("Attribute name/value: " + name + "/" + value);
                        if ((name.toString()).equals("server")) {
                            servers[i] = value;
                            System.out.println("Server Value" + servers[0]);
                        }
                        if ((name.toString()).equals("id")) {
                            ids[i] = value;
                        }
                        if ((name.toString()).equals("secret")) {
                            secrets[i] = value;
                        }
                    }
                }
            }
        }
        System.out.println(i);
        String flickrurl = "http://static.flickr.com/" + servers[i] + "/" + ids[i] + "_" + secrets[i] + ".jpg";
        try {
            URI uri = new URI(flickrurl);
            Desktop desktop = null;
            if (Desktop.isDesktopSupported()) {
                desktop = Desktop.getDesktop();
            }

            if (desktop != null) {
                desktop.browse(uri);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (URISyntaxException use) {
            use.printStackTrace();
        }
    }
}

Now see this line :

URLConnection uc = new URL("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=e3471e67d4ac10c64055420d9b211b4f&per_page=1&text=Bangalore").openConnection();

Here some important thing to see :

http://api.flickr.com/services/rest/?method=flickr.photos.search    -> this is the way to write Flickr API.

api_key=e3471e67d4ac10c64055420d9b211b4f  -> required for service. This is my api_key, you can have your own. Just go to flickr service and generate your API_key

per_page=1  -> Here is what I meant one image, if you can change this to 10. It will gather information of top 10 images in XML file.

text=Bangalore  -> Sorry, I have hard coded it for now. This is the search string.

Now, look at the XML file get generated in D:\\Hello1.xml. You can see one entry with tag photo inside photos. So, we need to take some data from this XML file and add in proper style to get the correct URL and that is here:

String flickrurl = "http://static.flickr.com/" + servers[i] + "/" + ids[i] + "_" + secrets[i] + ".jpg";

Again, lot of things are hard coded(which I will correct in next post). Since only one image (i=0). I am assuming its a jpg image :D. Now calling Desktop API, you can load this image on default browser.

Now, this is still a live question, for certain keyword search, it gives the same result like when I search for keyword "Vaibhav", code and search box of Flickr provided the same result(which is not my photo :-( )  whereas if I search on things like "Bangalore", result is not similar for many cases. I don't know how Flickr handles it internally.

Probably next I will try to upload image or translate image but in Java FX :-)

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