javafx 8 - how to chage media soures by using sceentap gesture? -
i want change media source , restart mediaplayer in javafx using leap motion gesture.
- how decribe screentap gesture case in leaplistener restart mediaplayer?
- hot change media source using swipe gesture?
and there fix plz (i'm beginner in java & first time of programming)
here code
public class leaptest extends application{
private final leaplistener listener = new leaplistener(); private final controller leapcontroller = new controller(); private media media = new media("file:///c:/users/halim.shin/workspace/movie/flower.mp4"); private mediaplayer player = new mediaplayer(media); private mediaview view = new mediaview(player); @override public void start(stage stage) throws exception { leapcontroller.addlistener(listener); borderpane root = new borderpane(); int key_move_delta = 10; double size_delta = 1.05; root.setcenter(view); scene scene = new scene(root,500,500,color.black); scene.getstylesheets().add(getclass().getresource("application.css").toexternalform()); scene.setonkeypressed(new eventhandler<keyevent>(){ @override public void handle(keyevent event) { // todo auto-generated method stub switch(event.getcode()){ case : view.settranslatey(view.gettranslatey() - key_move_delta); break; case right : view.settranslatex(view.gettranslatex() + key_move_delta); break; case down : view.settranslatey(view.gettranslatey() + key_move_delta); break; case left : view.settranslatex(view.gettranslatex() - key_move_delta); break; case page_up : view.setscalex(view.getscalex()*size_delta); view.setscaley(view.getscaley()*size_delta); break; case page_down : view.setscalex(view.getscalex()/size_delta); view.setscaley(view.getscaley()/size_delta); break; case enter : player.pause(); break; case space : player.play(); break; } } }); player.setcyclecount(mediaplayer.indefinite); player.setmute(true); // player.play(); // stage.setfullscreen(false); stage.setscene(scene); stage.show(); } public void stop(){ leapcontroller.removelistener(listener); } public static void main(string[] args){ launch(args); }
}
public class leaplistener extends listener {
public void onconnect(controller controller){ controller.enablegesture(gesture.type.type_swipe); controller.enablegesture(gesture.type.type_circle); controller.enablegesture(gesture.type.type_screen_tap); controller.enablegesture(gesture.type.type_key_tap); } public void onframe(controller controller){ frame frame = controller.frame(); gesturelist gestures = frame.gestures(); for(int i=0; i<gestures.count(); i++){ gesture gesture = gestures.get(i); **switch (gesture.type()){ case type_screen_tap : ?????????????????? system.out.println("key"); break; default : break;** } } }
}
you have communicate 2 different threads: javafx thread , leap motion one.
for that, can add objectproperty<t>
objects leaplistener
class in order set desired values @ every frame given gestures. have implement related public observablevalue<t>
methods.
on javafx thread, anonimous changelistener<t>
class can added listen change in observablevalue
. note you'll have use platform.runlater()
.
starting leaplistener
class:
public class leaplistener extends listener { private final booleanproperty keytap= new simplebooleanproperty(false); public booleanproperty keytapproperty() { return keytap; } public enum direction { none,left,right,up,down } public objectproperty<direction> swipegesture=new simpleobjectproperty<>(); @override public void onconnect(controller controller){ controller.enablegesture(gesture.type.type_key_tap); controller.enablegesture(gesture.type.type_swipe); } @override public void onframe(controller controller){ frame frame = controller.frame(); // reset properties keytap.set(false); swipegesture.set(direction.none); gesturelist gestures = frame.gestures(); for(int i=0; i<gestures.count(); i++){ gesture gesture = gestures.get(i); switch (gesture.type()){ case type_screen_tap : keytap.set(true); break; case type_swipe: swipegesture swipe = new swipegesture(gesture); if(math.abs(swipe.direction().getx()) > math.abs(swipe.direction().gety())){ if(swipe.direction().getx() > 0){ swipegesture.set(direction.right); } else { swipegesture.set(direction.left); } } else { if(swipe.direction().gety() > 0){ swipegesture.set(direction.up); } else { swipegesture.set(direction.down); } } break; default : break; } } } }
while in javafx class:
@override public void start(stage stage) { ... leaplistener leaplistener=new leaplistener(); leaplistener.keytapproperty().addlistener( (observablevalue<? extends boolean> ov, boolean t, final boolean t1) -> { if(t1){ platform.runlater(() -> { // react tap gesture... player.play(); }); } }); leaplistener.swipegesture.addlistener( (observablevalue<? extends leaplistener.direction> observable, leaplistener.direction oldvalue, leaplistener.direction newvalue) -> { platform.runlater(()->{ // react swipe gesture ... switch(newvalue){ case : break; case right : break; case down : break; case left : break; } }); }); player.setcyclecount(mediaplayer.indefinite); player.setmute(true); // player.play(); // stage.setfullscreen(false); stage.setscene(scene); stage.show(); }
just note whenever tap/swipe gesture detected leap motion controller, properties updated, , listener triggered. in javafx thread can respond gestures.
Comments
Post a Comment