view src/main/java/alice/test/topology/aquarium/fx/Aquarium.java @ 626:0960dd991266 dispose

finalize
author nozomi
date Thu, 14 Sep 2017 17:04:53 +0900
parents 41a5977e754c
children
line wrap: on
line source

package alice.test.topology.aquarium.fx;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import alice.codesegment.OutputDataSegment;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
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.image.ImageView;
import javafx.scene.image.WritableImage;
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 {
        final 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.setFill(Color.color(0.2, 0.5, 1.0));
        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);
                    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);

        BufferedImage img = null;
        try {
            URL url = getClass().getClassLoader().getResource("images/IKAMUSUME.jpg");
            img = ImageIO.read(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        WritableImage wimg = SwingFXUtils.toFXImage(img, null);
        ImageView iview = new ImageView(wimg);
        iview.setFitWidth(11);
        iview.setFitHeight(11);
        iview.setTranslateX(-5.5);
        iview.setTranslateY(-5.5);
        iview.setTranslateZ(6.0);
        iview.setId("IKAMUSUME");
        root.getChildren().add(iview);

        // 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);
    }

}