Sunday, 29 July 2012

Fetching Weather Data From Yahoo With JavaFX

 Do you remember the Hello World, JavaFX style application? It prints the text "Hello World!" to the output window when you click a button. I changed it to print the temperature in Bucharest instead:


First version:

package helloworld;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.stage.Stage;

import org.w3c.dom.*;

/**
 *
 * @author Cristina
 */
public class HelloWorld extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
   
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
       
        final WebEngine webEngine = new WebEngine("http://weather.yahooapis.com/forecastrss?w=868274&u=c");
      
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler() {

            @Override
            public void handle(ActionEvent event) {
                NodeList meteo = webEngine.getDocument().getElementsByTagNameNS("http://xml.weather.yahoo.com/ns/rss/1.0", "condition");
                System.out.println(meteo
                .item(0)
                .getAttributes()
                .getNamedItem("temp")
                .getNodeValue()
                .toString());

            }
        });
       
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }   
}


Second version:

public class HelloWorld extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
   
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Fetching Weather Data From Yahoo");
        Group root = new Group();
        Scene scene = new Scene(root, 400, 100);
               
        final WebEngine webEngine = new WebEngine("http://weather.yahooapis.com/forecastrss?w=868274&u=c");
      
        Button btn = new Button();
        btn.setText("Get temperature");
       
        final Text temperatura = new Text(10,50,"Temperatura in Bucuresti");
       
        btn.setOnAction(new EventHandler() {

            @Override
            public void handle(ActionEvent event) {
                NodeList meteo = webEngine.getDocument().getElementsByTagNameNS("http://xml.weather.yahoo.com/ns/rss/1.0", "condition");
                temperatura.setText("Hi! The temperature in Bucharest is: " + meteo
                .item(0)
                .getAttributes()
                .getNamedItem("temp")
                .getNodeValue()
                .toString() + " degrees Celsius.");
            }
        });
       
        root.getChildren().add(btn);
        root.getChildren().add(temperatura);
       
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
 


Bibliography

  1. http://developer.yahoo.com/weather/#request
  2. JavaFX 2.0: Introduction by Example, by Carl Dea


Tuesday, 12 June 2012

Advanced Calculator

And this is how my first meaningful JavaFX GUI looked like:


Then I made the stage transparent and added a close image:


I  got the idea from Zoran Pavlović blog. Thanks! :)

I'm already aware of several ways in which this application can be improved. Just in case you'd like to join me in learning JavaFX, I uploaded the code  here.

An excellent tutorial to get you started with TortoiseHG versioning control system  can be found here.

Monday, 11 June 2012

Simple Calculator, JavaFX Style

package calculatorsimplu;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

/**
 *
 * @author Cristina
 */
public class CalculatorSimplu extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
   
    @Override
    public void start(Stage scena) {
               
        Label et1 = new Label("Primul numar:");
        TextField nr1 = new TextField();
        Label et2 = new Label("Al doilea numar:");
        TextField nr2 = new TextField();
        Label et3 = new Label("Rezultat:");
        TextField rezultat = new TextField();
        Button calculeaza = new Button();
        calculeaza.setText("Aduna");
       
        scena.setTitle("Primul meu calculator");
        FlowPane root = new FlowPane();
        root.getChildren().add(et1);
        root.getChildren().add(nr1);
        root.getChildren().add(et2);
        root.getChildren().add(nr2);
        root.getChildren().add(et3);
        root.getChildren().add(rezultat);
        root.getChildren().add(calculeaza);
        scena.setScene(new Scene(root, 400, 100));
        scena.show();
    }
}

Bibliography


Saturday, 9 June 2012

JavaFX pe romaneste

Modelul interfetei  de programare JavaFX este cel al unei scene de teatru pe care actorii interpreteaza o piesa. Notiunile fundamentale sunt denumite in engleza Stage, Scene si Node. Pe romaneste, as spune

  • Scena

  • Decor

  • Actori


Primul pas in definirea unei interfete grafice in JavaFX este crearea unei scene. Scena este obiectul javafx.stage.Stage, aflat la dispozitia programatorului odata cu invocarea metodei start().

Un decor poate fi imaginat ca fiind una dintre ferestrele aplicatiei, in care actorii(subclase ale javafx.scene.Node, de exemplu text, imagini, forme geometrice, componente de tip UI) interactioneaza atat intre ei cat si cu spectatorii(utilizatorii aplicatiei).

Ar putea reprezenta o sursa de confuzie faptul ca "Scene" din engleza devine in romana "Decor", in timp ce "Scena" din romana este traducerea cuvantului "Stage". Cea mai buna modalitate de a intelege aceste concepte este programarea cu JavaFX a clasicei aplicatii Hello World.

Saturday, 17 March 2012

A PhD Is About Getting Things Done

My PhD advisor once told me that only a tiny fraction of accepted PhD theses have some scientific value. What a doctoral degree really proves is that the owner knows how to sell something (the thesis).

A PhD is about getting things done. And about choices. Here's my real-life story.

When I was a PhD student, one of the conditions to get your thesis approved was to have a number of papers accepted at prestigious international conferences. I needed one more, and finally I received a notification that my submission to SEKE'98 had been included in the program. A happy coincidence made it that I had a friend I was really fond of in San Francisco, and he told me that there was enough room for me to share the apartment he was living in. Everything seemed perfect and I started to make preparations.

And then I realized that I would not have been able to deliver my PhD in term, if I went to San Francisco. So I decided to stay in Japan and submit my thesis, despite the fact that one of the professors in the evaluation committee had advised me to postpone for 6 more months. I could not afford to postpone because my scholarship had ended a year before and although I was teaching English to cover some of my expenses, I was still spending more than I was earning and my savings were over.

Sometimes, I still wonder if I was wrong. I don't know. What I'm sure of is that, if I had to make the decision again, I would choose the same road. And maybe in a couple of years, or in five, or in ten, we'll meet in San Francisco. At JavaOne.

Tuesday, 7 February 2012

Simplest RSS Feed Reader with ROME and JDOM

Recently I have been pondering on a research topic regarding software for blogging and so I have started to study XML applications in Java. Below is the code of a very simple program that gets in the command line the address of a RSS feed, then prints the feed type and the feed title.


Compile using the following command:
C:\ProgrameJava>javac -classpath .\rome-1.0.jar;.\jdom-1.1.2.jar  RSSReader.java

Execute using, for example, the next line:
C:\ProgrameJava>java -classpath .;.\rome-1.0.jar;.\jdom-1.1.2.jar  RSSReader  http://feeds.feedburner.com/Edragonu-TheChoiceOfAPersonalPath

References

To be continued :)

Wednesday, 25 January 2012

My First Video Production

I got the idea from this blog post: How to Assess, Decide, Do with Pen, Paper and Magnets . I read the article about a year after it was written, when I first heard about the author, Dragos Roua, who was providing a productivity workshop at Excelez, a personal development conference in Bucharest. I couldn't make it to the event, but started to read Brilliantly Better, Dragos' blog on "living a better life, screwing up and everything in between", on a regular basis. Up to date, this is my favourite source of inspiration and motivation.

I am still interested in Oracle Java Certification. If I was younger, I would have definitely enjoyed the challenge, which is like a marathon, stretching your physical resources more than your intelligence. I have experienced this type of exam, made up from hundreds of similar questions that you have to answer quickly and continuously for about three hours, when I was applying for admission in an American university. About 20 years ago, that is, but that memory is still unpleasant. Maybe because it was a meaningless and useless effort.

Having said that, I am not sure that I will actually sit in the certification exams myself, but I enjoy coding in Java and I know something about practicing for exams. So I challenge those of you who are interested to provide a better implementation of a whiteboard with magnets. As you could hear in the presentation videos from above in this post, the best way to learn a programming language is to write code.



Video created using Camtasia Studio 7.1, of which I heard here.