view src/main/java/alice/test/topology/aquarium/fx/Aquarium.java @ 467:6e304a7a60e7 dispose

remove white space
author sugi
date Sat, 22 Nov 2014 12:08:24 +0900
parents aefbe41fcf12
children b8fe79f0c764
line wrap: on
line source

package alice.test.topology.aquarium.fx;

import java.io.IOException;

import alice.codesegment.OutputDataSegment;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class Aquarium extends Application {

    private OutputDataSegment ods = new OutputDataSegment();

    @Override
    public void start(Stage primaryStage) throws IOException {
        String myName = getParameters().getRaw().get(0); // name
        primaryStage.setTitle("Aquarium "+ myName);
        primaryStage.setResizable(false);
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>(){
            @Override
            public void handle(WindowEvent event) {
                // should send finish DataSegment
                System.exit(0);
            }
        });

        Scene scene = new Scene(createContent());
        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent t) {
                FishInfo info = null;
                switch (t.getCode()) {
                case RIGHT:
                    info = new FishInfo(0.1,0,0);
                    info.rolY = -1;
                    info.rotate = 90;
                    break;
                case LEFT:
                    info = new FishInfo(-0.1,0,0);
                    info.rolY = 1;
                    info.rotate = 90;
                    break;
                case UP:
                    info = new FishInfo(0,-0.1,0);
                    info.rolX = -1;
                    info.rotate = 90;
                    break;
                case DOWN:
                    info = new FishInfo(0,0.1,0);
                    info.rolX = 1;
                    info.rotate = 90;
                    break;
                case N:
                    info = new FishInfo(0,0,0.1);
                    info.rolX = -1;
                    break;
                case M:
                    info = new FishInfo(0,0,-0.1);
                    info.rotate = 180;
                    break;
                default: // do nothing
                    // reset. send median position. after implement
                    info = new FishInfo(0,0,0);
                    break;
                }
                ods.update(myName+"FishdiffP", info);
            }
        });
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Parent createContent(){
        Group root = new Group();
        ods.put("root", root);

        // Create and position camera
        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.getTransforms().addAll(
                new Rotate(0, Rotate.Y_AXIS),
                new Rotate(0, Rotate.X_AXIS),
                new Translate(0, 0, -15));
        camera.setId("camera");

        root.getChildren().add(camera);

        // Use a SubScene
        SubScene subScene = new SubScene(root, 800, 700, true, SceneAntialiasing.BALANCED);
        subScene.setFill(Color.TRANSPARENT);
        subScene.setCamera(camera);
        Group parent = new Group(subScene);

        return parent;
    }


    public static void main(String[] args) {
        launch(args);
    }

}