Vaibhav's Blog Space

Understanding JavaFX - Small Navigation Code

Friday Jul 18, 2008

So finally I am able to write a small code with the new Java FX API and Builder provided in NB 6.1. I have also seen one bug got fixed (maybe initially it was handled on a different way). Initially when we make any FX project in Netbeans, it basically store the *.fx code into classes folder as well. There is no way one can find the .class file of the .fx file, which is not a problem now.

I have written one small navigation code of map from key control. Which moves the map left, right, up and down from the corresponding key. And the most part of the code line is to handle the boundary condition like the image should not move left when it is already in left most region and so on. Thanks to Vikram for helping me out in writing boundary condition, this is always confusing for me :-D. Here is the small code:

import javafx.application.Frame;
import javafx.application.Stage;
import javafx.scene.paint.Color;
import javafx.input.KeyEvent;
import javafx.input.KeyCode;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.input.MouseEvent;
import javafx.scene.transform.Translate;
import java.lang.*;
import javafx.scene.geometry.Line;

var x1 : Number = 0;
var y1 : Number = 0;
//var myImage = Image { url: "{__DIR__}/./earth-map-big.jpg" };
var myImage = Image { url: "http://arstechnica.com/reviews/4q00/macosx-pb1/images/earth-map-big.jpg" };
var line: Line;

Frame {
    title: "MyApplication"
    width: 500
    height: 500
    resizable: false
 
    closeAction: function() { 
        java.lang.System.exit( 0 ); 
    }
    visible: true
    stage: Stage {
        fill:Color.BLACK
        content: [
            ImageView {
                image : myImage
                transform : [ 
                    Translate { x : bind x1, y : bind y1 }
                ]
                onKeyPressed: function( e: KeyEvent ):Void {
                    System.out.println(x1 + " " + y1);
                   if(
                    e.getKeyText() == "Left")
                    {
                        if(x1 < 0) {
                            System.out.println(x1);
                            x1+=50;
                        }
                    }
                    if(
                    e.getKeyText() == "Right")
                    {
                        if(Math.abs(x1  -  500) < myImage.width) {
                            System.out.println(x1);
                            x1-=50;
                        }
                    }
                    if(
                    e.getKeyText() == "Down")
                    {
                        if(Math.abs(y1  -  500) < myImage.height) {
                            System.out.println(y1);
                            y1-=50;
                        }
                    }
                    if(e.getKeyText() == "Up")
                    {
                        if(y1 < 0) {
                            System.out.println(y1);
                            y1+=50;
                        }
                    }
                }
                opacity:1  
            }
        ]
   }
} 

I am loading the image from URL itself, so it will take sometime(because Image size is 3200 X 1600). Rest all is mathematics :-). Still lot more fancy job to do !

[9] Comments
Like this post? del.icio.us | furl | slashdot | technorati | digg
Comments:

Nice stuff

Posted by Angad Singh on July 20, 2008 at 01:32 PM IST #

Thanks ! Learning :)

Posted by Vaibhav on July 20, 2008 at 11:36 PM IST #

I am glad to talk with you and you give me great help! Thanks for that, I am wonderring if i can contact you via email when i meet problems?

Posted by wow account on August 27, 2009 at 11:35 AM IST #

http://www.jsjyyb.cn

Posted by 涡街流量计 on September 28, 2009 at 11:11 AM IST #

http://www.shanghaimassages.net

Posted by shanghai massage on September 28, 2009 at 11:12 AM IST #

It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.

Posted by Christian Louboutin Boots on October 29, 2009 at 06:34 AM IST #

christian louboutin discount thanks

Posted by christian louboutin discount on October 30, 2009 at 08:10 AM IST #

I have received several similar emails like this one.

Posted by link of london on November 07, 2009 at 06:33 AM IST #

Post a Comment:
  • HTML Syntax: NOT allowed