Guest User

Untitled

a guest
Oct 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. /**
  2. * デバイスを追跡するクラス
  3. */
  4. public class DeviceTracker {
  5.  
  6. private Device device;
  7.  
  8. public DeviceTracker(){
  9. device = new TestDevice();
  10. }
  11.  
  12. /**
  13. * データストリーム
  14. */
  15. public Flux<Location> getDataStream(){
  16. return Flux
  17. .interval(Duration.ofSeconds(1L))//1秒ごとにデータを生成する
  18. .map(this::getDeviceLocation);//デバイスから位置情報を取得する
  19. }
  20.  
  21. private Location getDeviceLocation(Long time){
  22. //デバイスから位置情報を取得する
  23. return device.getCurrentLocation();
  24. }
  25.  
  26. /**
  27. * 簡易な位置情報(実際はここまで単純ではない)
  28. */
  29. public static class Location{
  30.  
  31. public Location(Double x,Double y){
  32. modify(x,y);
  33. }
  34.  
  35. private Double x;
  36.  
  37. public Double getX(){
  38. return x;
  39. }
  40.  
  41. private Double y;
  42.  
  43. public Double getY(){
  44. return y;
  45. }
  46.  
  47. public void modify(Double x,Double y){
  48. this.x = x;
  49. this.y = y;
  50. }
  51.  
  52. public String toString(){
  53. return "[x="+x+",y="+y+"]";
  54. }
  55. }
  56.  
  57. /**
  58. * 1秒毎に四方のいずれかへランダムに動くデバイス
  59. */
  60. public static class TestDevice extends Device{
  61. private Location currentLocation = new Location(400.0,200.0);
  62. private Random random = new Random();
  63.  
  64. public TestDevice(){
  65. new Thread(){
  66. @Override
  67. public void run(){
  68. while(true){
  69. try {
  70. move();
  71. Thread.sleep(1000L);
  72. } catch (InterruptedException e) {
  73. e.printStackTrace();
  74. return;
  75. }
  76. }
  77. }
  78. }.start();
  79. }
  80.  
  81. private void move(){
  82. int order = random.nextInt()%4;
  83. switch(order){
  84. case 0:
  85. currentLocation.modify(currentLocation.x+=10.0,currentLocation.y);
  86. break;
  87. case 1:
  88. currentLocation.modify(currentLocation.x-=10.0,currentLocation.y);
  89. break;
  90. case 2:
  91. currentLocation.modify(currentLocation.x,currentLocation.y+=10.0);
  92. break;
  93. case 3:
  94. currentLocation.modify(currentLocation.x,currentLocation.y-=10.0);
  95. break;
  96. }
  97. }
  98.  
  99. @Override
  100. public Location getCurrentLocation() {
  101. return currentLocation;
  102. }
  103.  
  104. }
  105.  
  106. }
  107.  
  108. /**
  109. * JavaFXコントローラクラス
  110. */
  111. public class TrackingController implements Initializable{
  112.  
  113. @FXML
  114. private Canvas map;
  115.  
  116. private DeviceTracker tracker;
  117.  
  118. private Disposable subscriber;
  119.  
  120. /**
  121. * 画面押したらここが走る。データの購読を開始するか解除するかのみ。
  122. */
  123. public void startOrStop(MouseEvent event){
  124. if(Objects.isNull(subscriber)||subscriber.isDisposed()){
  125. subscriber = tracker.getDataStream()
  126. .subscribe(this::modifyMap);
  127. }else{
  128. subscriber.dispose();
  129. }
  130. }
  131.  
  132. /**
  133. *データストリームからデータが流れるたびにこれが走る。画面を更新する。
  134. */
  135. private void modifyMap(Location location){
  136. GraphicsContext context = map.getGraphicsContext2D();
  137. context.clearRect(0, 0, map.getWidth(), map.getHeight());
  138. context.strokeOval(location.getX()-5, location.getY()-5, 10, 10);
  139. }
  140.  
  141. /**
  142. * コンストラクタ代わり
  143. */
  144. @Override
  145. public void initialize(URL location, ResourceBundle resources) {
  146. tracker = new DeviceTracker();
  147. map.setOnMouseClicked(this::startOrStop);//画面押下時の処理を登録
  148. }
  149. }
  150.  
  151. /**
  152. * アプリケーション(メイン)クラス
  153. */
  154. public class TrackingApplication extends Application{
  155. @Override
  156. public void start(Stage stage) throws Exception {
  157. Parent root = FXMLLoader.load(getClass().getResource("/tracking.fxml"));
  158. Scene scene = new Scene(root);
  159. stage.setScene(scene);
  160. stage.show();
  161. }
  162.  
  163. public static void main(String[] args){
  164. launch(args);
  165. }
  166. }
Add Comment
Please, Sign In to add comment