1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sensorstest;
  26 
  27 import javafx.geometry.Rectangle2D;
  28 import javafx.scene.Group;
  29 import javafx.stage.Screen;
  30 
  31 import javafx.scene.shape.Path;
  32 import javafx.scene.shape.MoveTo;
  33 import javafx.scene.shape.LineTo;
  34 import javafx.scene.shape.ClosePath;
  35 
  36 import javafx.scene.shape.Rectangle;
  37 
  38 import javafx.scene.paint.Color;
  39 
  40 
  41 public class AttitudeIndicator extends Group {
  42 
  43     public AttitudeIndicator() {
  44         super();
  45 
  46         final Rectangle2D dims = Screen.getPrimary().getVisualBounds();
  47         final int width = (int) dims.getWidth();
  48         final int height = (int) dims.getHeight();
  49 
  50         final int cx = width/2;
  51         final int cy = height/2;
  52 
  53         final Path mask = new Path();
  54         mask.getElements().addAll(
  55             new MoveTo(-1,0),
  56             new LineTo(-1, height),
  57             new LineTo(width,height),
  58             new LineTo(width,0),
  59             new LineTo(-1,0),
  60             new LineTo(width/4,   height/4),
  61             new LineTo(width*3/4, height/4),
  62             new LineTo(width*3/4, height*3/4),
  63             new LineTo(width/4,   height*3/4),
  64             new LineTo(width/4,   height/4),
  65             new ClosePath()
  66         );
  67         mask.setStrokeWidth(0);
  68         mask.setStroke(null);
  69         mask.setFill(Color.BLACK);
  70 
  71         final Rectangle rSky = new Rectangle(cx - 500, cy - 1000, 1000, 1000);
  72         rSky.setFill(Color.NAVY);
  73 
  74         final Rectangle rGround = new Rectangle(cx - 500, cy, 1000, 1000);
  75         rGround.setFill(Color.BROWN);
  76 
  77         final Rectangle bar = new Rectangle(width/4, cy-4, width/2, 8);
  78         bar.setFill(Color.GRAY);
  79 
  80         final Group g = new Group(rSky, rGround);
  81 
  82         getChildren().addAll(g, mask, bar);
  83 
  84         com.sun.javafx.ext.device.ios.sensors.IOSMotionManager.addAccelerationListener(
  85                 new com.sun.javafx.ext.device.ios.sensors.IOSMotionManager.Listener() {
  86             public void handleMotion(float x, float y, float z) {
  87                 final double roll = Math.atan(-y/x);
  88                 final double pitch = Math.atan(-y/z);
  89 
  90                 g.setRotate(Math.toDegrees(Math.atan(-y/x)));
  91                 g.setTranslateY(z * cy);
  92             }
  93         });
  94     }
  95 }