view src/main/java/alice/test/topology/aquarium/fx/Aquarium.java @ 381:6adfb60766f7 multicast

add window close event
author sugi
date Tue, 10 Jun 2014 13:41:11 +0900
parents 29e75b92c358
children 878d397904da
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);
		primaryStage.setTitle("Aquarium "+ myName); // name
		primaryStage.setResizable(false);
		primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>(){
			@Override
			public void handle(WindowEvent event) {
				System.exit(0);
			}	
		});
		
		Scene scene = new Scene(createContent());
		scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
		    public void handle(KeyEvent t) {
		    	System.out.println(t.getCode());
		    	switch (t.getCode()) {
		    	case RIGHT:
		    		ods.update(myName+"FishPosition", new FishInfo(0.1,0,0));
		    		break;
		    	case LEFT:
		    		ods.update(myName+"FishPosition", new FishInfo(-0.1,0,0));
		    		break;
		    	case UP:
		    		ods.update(myName+"FishPosition", new FishInfo(0,-0.1,0));
		    		break;
		    	case DOWN:
		    		ods.update(myName+"FishPosition", new FishInfo(0,0.1,0));
		    		break;
				default: // do nothing
					break;
		    	}
		    }
		});
		
		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(-20, Rotate.Y_AXIS),
				new Rotate(-20, 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);
	}

}