Animating a Red Ball to Show Acceleration Force Changes
The AccelerometerTest sample is a simple example to show how JavaFX can use the accelerometer on your mobile phone. An accelerometer is an electromechanical device that measures acceleration forces. These forces could be caused by moving or vibrating the accelerometer. JSR 256 provides the Mobile Sensor API.
The following two figures show the sample from the emulator and the External Events Generator window, which can be opened from the emulator menu. The red ball rolls as you change the axisX and axisY values, which simulate the acceleration forces of the x and y axes.
AccelerometerTest

External Events Generator Window

Understanding the Code
This sample uses JSR 256 Mobile Sensor API in the javax.microedition.sensor.* package. Hence, the JSR 256 jar file is included in the sample NetBeans project library tab. The first step is to find the "acceleration" sensor and open the channel to obtain acceleration data when needed. The circle node translation is bound to the script variables, axisX and axisY, where their values are refreshed every 100ms by obtaining data from the opened channel.
import ... import javax.microedition.sensor.*; import javax.microedition.io.Connector; import javax.microedition.sensor.control.*; var axisX = 0.0; var axisY = 0.0; var axisZ = 0.0; /* The first step is to find the "acceleration" sensor and open the channel to get acceleration data when needed. */ var sensors = SensorManager.findSensors("acceleration", null); SensorUtil.dumpInfo(); var accel = sensors[0]; var infosLen = accel.getChannelInfos().length; var accelInfo = accel.getChannelInfos()[0]; var accelUrl = accel.getUrl(); var accelConn: SensorConnection = Connector.open(accelUrl) as SensorConnection; /* The function to get acceleration data. The function is called by the Timeline every 100ms. and update the script variables, axisX/Y/Z. */ function getData():Void { if(accelInfo.getDataType() == ChannelInfo.TYPE_DOUBLE) { var data: Data[] = accelConn.getData(1); axisX = data[0].getDoubleValues()[0]; axisY = data[1].getDoubleValues()[0]; axisZ = data[2].getDoubleValues()[0]; } } Timeline { repeatCount: Timeline.INDEFINITE keyFrames: KeyFrame { time: 100ms action: getData }; }.play(); /* * The circle will translate as the script variables, axisX/Y/Z are refreshed. */ var scene:Scene = Scene { fill: Color.DARKRED content: Group { content: Circle { centerX: bind scene.width/2 * (1 + axisX) centerY: bind scene.height/2 * (1 + axisY) fill: Color.RED radius: 20 } } } Stage { scene: scene width: 320 height: 240 }
How to Set up, Compile and Run the Code
AccelerometerTest sample requires the following configuration changes manually.
- Open the file at [FX-SDK]\emulator\toolkit-lib\devices\DefaultFxPhone\conf\device.properties
- Edit the file to have the following lines. Override the values.
- runtime.application.security.policyfile: _policy.txt.FULL
- runtime.application.security.messagefile: _function_groups.txt.FULL
- Open the the file at [FX-SDK]\emulator\toolkit-lib\devices\DefaultFxPhone\conf\modules
- Edit the file to add the following line.
- modules/emulator-ui-window-external-events/jsr256
After the configuration is complete, build and run the sample from NetBeans IDE. Use the following steps to experiment with the accelerometer feature.
- Choose View > External Events Generator from the emulator window to generate a separate window for simulating accelerations.
- In the External Events Generator window, click the Sensors tab.
- Drag the slides to change the axisX and axisY values.
The red ball rolls as you change the axisX and axisY values.
Josh MarinacciStaff Engineer,
Sun Microsystems
