Presentation about developing JavaFX for Intel Ultrabooks, taking advantage of the multi-touch support and also sensors.
By Intel Brazil Community Manager, Felipe Pedroso and Oracle Product Manager, Bruno Borges.
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Developing Rich Interfaces in JavaFX for Ultrabooks
1. Developing Rich Interfaces in
JavaFX for Ultrabooks
Felipe Pedroso
Community Manager – Intel Brazil
Bruno Borges
Principal Product Manager - Oracle LAD
2. 2
Program Agenda
The Future of PCs
JavaFX and Touch Support
Using JNI to Work with Sensors
9. 9
The Ultrabook™ Platform
Reshaping the PC Experience
9
Ambient
Light Sensor
Ambient
Light Sensor
GPS
GPS Compass
Compass
Near Field
Communicati
on
Near Field
Communicati
on
Gyroscope
Gyroscope
Ultrabook
™
Accelerometer
Accelerometer
Multi-Touch
Multi-Touch
Context Aware
Sensors
10. 10
OK, they have all those
features…
… but how can we implement them
using Java?
11. 11
To allow users to TOUCH my Java App...
• Should I use...
– AWT? Swing? SWT?
• Actually, you can use them but...
– They aren’t made for multi-touch (OK, there’s a way to do it)
– You must optimize your UI controls to be more ‘touch
friendly’
• So, how to do it in a simple and easy way?
14. 14
Actions supported
• Touch events: Down, Move and Up
– Tap (Down and Up) / Double tap
– Drag and Drop (Down, Move and Up)
– Hold
• Gestures
– Swipe: Up, Down, Left and Right
– Zoom: Pinch / Spread
– Rotate
15. 15
• Your components must extend the Node class or any of its
subclasses (StackPane, ImageView, etc)
• Set the proper EventHandler to handle the action.
15
What do I need to do to manipulate
components?
16. 16
• Apply a transform to the component (Translation,
Rotation and Scale):
• Let’s dive into some code!
What do I need to do to manipulate
components?
18. 18
Available sensors on Ultrabooks
• Accelerometer
– Proper acceleration in three axis (x, y, z)
• Gyrometer
– Device orientation
• Magnetometer
– Strenght and direction of magnetic fields
• GPS (Global Positioning System)
– Location and Time information
• NFC
– Near Field Communication
• Ambient Light Sensor
– Ambient Light Level
18
21. 21
Windows API – How to
• Get the default object of your sensor using the GetDefault method
• You can call the GetCurrentReading() to get the current value of the sensors or...
• ... work with the ReadingChanged event
– Set the attribute ReportInterval (please, respect the MinimumReportInterval to avoid
problems)
– Delegate a method to handle the event (something like setting a method as a listener)
– Handle the event!
• This procedure is valid only for sensors from Windows.Sensors namespace
23. 23
Using JNI to access the sensors from
Windows.Sensors
1. [Java] Create a native method to register the object that will
handle the events that come from the sensor
2. Generate the header file using javah
3. [C++] Create a DLL Project in Visual Studio with the DLLs and
namespaces of the Windows 8 APIS
4. [C++] Use a variable to keep a reference to the object and the ID
(GetMethodID) of the method that will handle the event
24. 24
Using JNI to access the sensors from
Windows.Sensors
5. [C++] Initialize the sensor and delegate a C++ method to
handle the event
6. [C++] Redirect the delegate method call to the Java method
using the function CallVoidMethod with the following parameters:
the Java Object, the method ID and it’s parameters
7. [Java] Handle the event!
A lot of text? Please, show me the code!
25. 25
JNI Callback from C++ to Java
public final class ShakenSensor {
static { System.loadLibrary("ShakenLib"); }
private ShakenListener listener;
public ShakenSensor (ShakenListener listener) {
this.listener = listener;
}
public native boolean registerObject();
public native boolean registerMethod();
private void shaken(long timestamp) {
listener.shaken(timestamp);
}
}
Load the
C++
Library
Define
native
methods
private method
to be called
from C++
26. 26
JNI Callback from C++ to Java
// Shaken.h
JNIEXPORT jboolean JNICALL Java_sample_ShakenSensor_registerObject
(JNIEnv *, jobject obj);
JNIEXPORT jboolean JNICALL Java_sample_ShakenSensor_registerMethod
(JNIEnv * env, jobject obj);
The header file with mapped
native JNI methods
27. 27
JNI Callback from C++ to Java
// Shaken.cpp
#include "Shaken.h"
#include "jni.h"
#include<iostream>
#using <Windows.winmd>
#using <Platform.winmd>
using namespace::Windows::Devices::Sensors;
JavaVM * currentJvm;
jobject shakenSensorJObject;
jmethodID shakenJMethod; // ShakenSensor.shaken(long)
cpp file including:
Windows Sensor Fusion API
Java Native Interface header
Shaken header
Keep references of
jobject, jmethodID,
currentJvm
28. 28
JNI Callback from C++ to Java
// Shaken.cpp
…_ShakenSensor_registerObject (JNIEnv * env, jobject obj)
{
shakenSensorJObject = env->NewGlobalRef(obj);
jclass shakenSensorClass = env->GetObjectClass(shakenSensorJObject);
shakenJMethod = env->GetMethodID(shakenSensorClass, "shaken", "(J)V");
env->GetJavaVM(¤tJvm);
…
}
Get a reference to ShakenSensor object
from JNIEnv, then the jclass,
then the shaken Java method
29. 29
JNI Callback from C++ to Java
// Shaken.cpp
void Shaken(Accelerometer^ sender, AccelerometerShakenEventArgs^ args){
invokeShakenOnJava(args->Timestamp.UniversalTime / 10000);
}
…_ShakenSensor_registerMethod (JNIEnv * env, jobject obj)
{
accelerometer->Shaken += ref new
Windows::Foundation::TypedEventHandler<Accelerometer^,AccelerometerShakenEventArgs^>(&Shaken);
…
}
registerMethod activates the Sensor and
register events to Shaken C++ method
Shaken method forwards
data to Java
30. 30
JNI Callback from C++ to Java
// Shaken.cpp
void invokeShakenOnJava(jlong timest){
JNIEnv * jniEnvironment;
int envStt = currentJvm->GetEnv((void **)&jniEnvironment, JNI_VERSION_1_6);
// check the envStat == JNI_OK
//and call currentJvm->AttachCurrentThread if necessary
…
// call the Java method on the ShakenSensor Java Object
jniEnvironment->CallVoidMethod(shakenSensorJObject, shakenJMethod,timest);
// always detach current thread
currentJvm->DetachCurrentThread();
}
Make sure JVM is attached
to thread, then call the
method shakenJMethod
31. 31
What about Linux?
Touch works fine, but there aren’t clear APIs to read
sensors. If you know how to do it, let’s work together!
32. 32
Want to know more about Intel Software?
http://software.intel.com/en-us/