Thursday Jul 09, 2009

JavaFX - Storage


JavaFX has Storage API which provides functionality similar to read and write from flat file. The API is part of common profile and hence will work across various platforms/devices. The application is not required to be signed to use this API. Also the access of saved data is restricted with in the codebase of the application.

<script src="http://dl.javafx.com/1.2/dtfx.js"></script> <script src="/rakeshmenonp/resource/Draw/Draw.js"></script>

For Applet mode, click on above image

For standalone mode

In above sample use PathTransition API to move the car along a path (road). User can create roads by dragging the mouse over the terrain. The path will be saved using storage API, so that it can be retrieved later. Try creating a new path, close the application and reload the application later, the path will be re-created.

The path elements can be represented in xml format as shown below.

<path>
    <element type="MoveTo" x="73.0" y="46.0" />
    <element type="LineTo" x="74.0" y="45.0" />
    <element type="LineTo" x="75.0" y="45.0" />
    <element type="LineTo" x="82.0" y="45.0" />
    <element type="ClosePath" />
</path>

Next we will create a Storage instance and open an OutputStream to this as shown below. The API is similar to FileOutputStream. The boolean in openOutputStream is to specify overwrite (opposite of append in FileOutputStream). Size of resource can be specified using resource.maxLength attribute.

var entry = Storage {
    source: "TerrainPath.dat"
};

var out = entry.resource.openOutputStream(true);

This way we obtain the OutputStream to which we can write the data.

We can retrieve the data using openInputStream which returns an instance of InputStream instance. The data can be read from this stream, similar to FileInputStream.

var in = entry.resource.openInputStream();

Once the InputStream is obtained we can use PullParser to parse the xml data and construct the path.

var pullParser = PullParser {

    // InputStream obtained from Storage
    input: in

    onEvent: function(e) {

        if(e.type == PullParser.START_ELEMENT) {
            var type = e.getAttributeValue("type");
            if(type.equals("MoveTo")) {
                var x = e.getAttributeValue("x");
                var y = e.getAttributeValue("y");
                insert MoveTo 
                    x: Number.parseFloat(x
                    y: Number.parseFloat(y
                into elements;
            else if(type.equals("LineTo")) {
                var x = e.getAttributeValue("x");
                var y = e.getAttributeValue("y");
                insert LineTo {
                    x: Number.parseFloat(x
                    y: Number.parseFloat(y
                into elements;
            else if(type.equals("ClosePath")) {
                insert ClosePath { } into elements;
            }
        }
    }
};

pullParser.parse();

After re-creating the path we can use AnimationPath.createFromPath( <path> ) and PathTransition to move the car along the path. Please refer to previous post for more information on Animation.

Update:
I didn't mention about storage.properties, wasn't sure whether its a documented feature. Anyway users are trying to find the secrets! Well.. there is no secret as such, you can de-compile the class files

Configuration file - storage.properties - is under ~/.javafx/deployment/ in Unix and %HOME%/Sun/JavaFX/Deployment/ on Windows. User can enable/disable the storage using "storage.enabled" attribute by setting it to true/false. The limit can be configured using "storage.limit.domain". This file will be empty by default.

Try it out and let me know feedback

Source:

<script type="text/javascript">var dzone_style = '2';</script> <script language="javascript" src="/rakeshmenonp/resource/Draw/Draw_zoneit.js"></script>

About


The views expressed on this blog are my own and do not necessarily reflect that of my organization

Search

Categories
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