Friday Jul 18, 2008

Understanding JavaFX - Small Navigation Code

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 !

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