Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * デバイスを追跡するクラス
- */
- public class DeviceTracker {
- private Device device;
- public DeviceTracker(){
- device = new TestDevice();
- }
- /**
- * データストリーム
- */
- public Flux<Location> getDataStream(){
- return Flux
- .interval(Duration.ofSeconds(1L))//1秒ごとにデータを生成する
- .map(this::getDeviceLocation);//デバイスから位置情報を取得する
- }
- private Location getDeviceLocation(Long time){
- //デバイスから位置情報を取得する
- return device.getCurrentLocation();
- }
- /**
- * 簡易な位置情報(実際はここまで単純ではない)
- */
- public static class Location{
- public Location(Double x,Double y){
- modify(x,y);
- }
- private Double x;
- public Double getX(){
- return x;
- }
- private Double y;
- public Double getY(){
- return y;
- }
- public void modify(Double x,Double y){
- this.x = x;
- this.y = y;
- }
- public String toString(){
- return "[x="+x+",y="+y+"]";
- }
- }
- /**
- * 1秒毎に四方のいずれかへランダムに動くデバイス
- */
- public static class TestDevice extends Device{
- private Location currentLocation = new Location(400.0,200.0);
- private Random random = new Random();
- public TestDevice(){
- new Thread(){
- @Override
- public void run(){
- while(true){
- try {
- move();
- Thread.sleep(1000L);
- } catch (InterruptedException e) {
- e.printStackTrace();
- return;
- }
- }
- }
- }.start();
- }
- private void move(){
- int order = random.nextInt()%4;
- switch(order){
- case 0:
- currentLocation.modify(currentLocation.x+=10.0,currentLocation.y);
- break;
- case 1:
- currentLocation.modify(currentLocation.x-=10.0,currentLocation.y);
- break;
- case 2:
- currentLocation.modify(currentLocation.x,currentLocation.y+=10.0);
- break;
- case 3:
- currentLocation.modify(currentLocation.x,currentLocation.y-=10.0);
- break;
- }
- }
- @Override
- public Location getCurrentLocation() {
- return currentLocation;
- }
- }
- }
- /**
- * JavaFXコントローラクラス
- */
- public class TrackingController implements Initializable{
- @FXML
- private Canvas map;
- private DeviceTracker tracker;
- private Disposable subscriber;
- /**
- * 画面押したらここが走る。データの購読を開始するか解除するかのみ。
- */
- public void startOrStop(MouseEvent event){
- if(Objects.isNull(subscriber)||subscriber.isDisposed()){
- subscriber = tracker.getDataStream()
- .subscribe(this::modifyMap);
- }else{
- subscriber.dispose();
- }
- }
- /**
- *データストリームからデータが流れるたびにこれが走る。画面を更新する。
- */
- private void modifyMap(Location location){
- GraphicsContext context = map.getGraphicsContext2D();
- context.clearRect(0, 0, map.getWidth(), map.getHeight());
- context.strokeOval(location.getX()-5, location.getY()-5, 10, 10);
- }
- /**
- * コンストラクタ代わり
- */
- @Override
- public void initialize(URL location, ResourceBundle resources) {
- tracker = new DeviceTracker();
- map.setOnMouseClicked(this::startOrStop);//画面押下時の処理を登録
- }
- }
- /**
- * アプリケーション(メイン)クラス
- */
- public class TrackingApplication extends Application{
- @Override
- public void start(Stage stage) throws Exception {
- Parent root = FXMLLoader.load(getClass().getResource("/tracking.fxml"));
- Scene scene = new Scene(root);
- stage.setScene(scene);
- stage.show();
- }
- public static void main(String[] args){
- launch(args);
- }
- }
Add Comment
Please, Sign In to add comment