org.vaadin.scenejs.SceneJSApplication.java Source code

Java tutorial

Introduction

Here is the source code for org.vaadin.scenejs.SceneJSApplication.java

Source

/**
 * SceneJS Vaadin Addon
 * Copyright (C) 2012 Federico Russo <chiccorusso@gmail.com>
 * Licensed under the MIT license.
 *
 * Includes SceneJS WebGL Scene Graph Library for JavaScript http://scenejs.org/
 * Dual licensed under the MIT or GPL Version 2 licenses. http://scenejs.org/license
 * Copyright 2010, Lindsay Kay
 */
package org.vaadin.scenejs;

import com.vaadin.Application;
import com.vaadin.terminal.ParameterHandler;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import org.vaadin.scenejs.SceneJSCanvas.PickListener;

/**
 * Demo Vaadin application for the SceneJS integration.
 * 
 * The application contains a Window with a SceneJS canvas that is populated with different trees
 * according to parameters that are passed in the URL (see the {@code ParameterHandler}.
 *
 * @author Federico Russo
 */
public class SceneJSApplication extends Application {

    private static final Random RANDOM = new Random(System.currentTimeMillis());

    @Override
    public void init() {
        final Window window = new Window("SceneJS");
        window.addComponent(new Label("SceneJS 2 Vaadin Demo"));
        final SceneJSCanvas canvas = new SceneJSCanvas() {
            {
                setHeight("800px");
                setWidth("1000px");
            }
        };

        addWindow(window);
        window.addComponent(canvas);
        window.addParameterHandler(new ParameterHandler() {
            @Override
            public void handleParameters(Map<String, String[]> parameters) {
                if (parameters.containsKey("t")) {
                    teapots(canvas, window, Integer.valueOf(parameters.get("t")[0]));
                } else if (parameters.containsKey("p")) {
                    pipes(canvas, window, Integer.valueOf(parameters.get("p")[0]), selections);
                } else if (parameters.containsKey("s")) {
                    spheres(canvas, window, Integer.valueOf(parameters.get("s")[0]));
                } else if (parameters.containsKey("t1")) {
                    texts(canvas, window, Integer.valueOf(parameters.get("t1")[0]));
                } else if (parameters.containsKey("t2")) {
                    texts2(canvas, window, Integer.valueOf(parameters.get("t2")[0]));
                }
            }
        });
    }

    private final Collection<String> selections = new HashSet<String>();

    public static void pipes(final SceneJSCanvas canvas, final Window window, int howmany,
            final Collection<String> selections) {
        canvas.addNode(new Library(new Cylinder(1, 10, 12).core("y"), new Cube().core("c"),
                new Material(new Color(0.5, 0.5, 0.6), new Color(0.9, 0.9, 0.9), 0, 1, 70).core("m"),
                new Material(new Color(1, 0, 0), new Color(0.9, 0, 0), 1, 0, 0).setAlpha(0.5).core("s")));
        SceneJSNode root = new LookAt("lookAt", new Point(30, 0, 0));
        SceneJSNode sceneRoot = root
                .a(/*  */new DirectionalLight(new Color(1, 1, 1), new Point(-1, 0.5, -1), true, true),
                        /* */ new DirectionalLight(new Color(1, 1, 0.8), new Point(0, 0.5, -1), true, false),
                        /* */ new Camera("camera", 40, 1.25, 0.1, 300))
                .c(/*    */new Flags().setBackfaces(false), /*     */ new Material().ref("m"));
        for (int i = 0; i < howmany; i++) {
            final float r = fl(0.1f);
            final Name pipeRoot = sceneRoot.c(/**/new Translate(flm(10), flm(10), flm(10)),
                    /* */ new Rotate(fl(360), flm(0.5f), flm(0.5f), flm(0.5f)), /*   */ new Name("pipe" + i));
            pipeRoot.c(/*      */new Scale(r, r, r), /*       */ new Cylinder().ref("y"));
            pipeRoot.addPickListener(new PickListener() {
                @Override
                public void onPick(Name sender) {
                    window.showNotification(sender.getId());
                    selections.add(sender.getId());
                    pipeRoot.c(/**/new Scale(2 * r, 2 * r, 10 * r), /* */ new Translate(0, 0, 0.5),
                            /*   */ new Flags().setBackfaces(true).setTransparent(true),
                            /*     */ new Material().ref("s"), /*       */ new Cube().ref("c"));
                    canvas.requestRepaint();
                }
            });
        }

        canvas.a(root);
        canvas.addNothingPickedListener(new SceneJSCanvas.NothingPickedListener() {
            @Override
            public void nothingPicked() {
                selections.clear();
                window.showNotification("Nothing picked.");
            }
        });
        canvas.setMovementType(SceneJSCanvas.MovementType.CONSTRAINED);
    }

    private static float fl() {
        return RANDOM.nextFloat();
    }

    private static float flm(float max) {
        return fl(max * 2) - max;
    }

    private static float fl(float max) {
        return RANDOM.nextFloat() * max;
    }

    protected void teapots(SceneJSCanvas canvas, final Window window, int howmany) {
        canvas.addNode(new Library(new Sphere(1).core("coreSphere"), new Sphere2(1).core("coreSphere2"),
                new Teapot().core("coreTeapot"), new Cylinder(1, 10, 24).core("coreCylinder")));
        SceneJSNode material = canvas.a(/**/new LookAt(new Point(0, 10, 15), new Point(), new Point(0, 1, 0)))
                .a(/*  */new Camera("camera", 25, 1.25f, 0.1f, 300))
                .ap(/*  */new DirectionalLight(new Color(1, 1, 1), new Point(1, -0.5, -1), true, true))
                .ap(/*  */new DirectionalLight(new Color(1, 1, 0.8), new Point(0, -0.5, -1), true, false))
                .a(/*  */new Rotate("pitch", 0, 1, 0, 0)).a(/*    */new Rotate("yaw", 0, 0, 1, 0))
                .a(/*      */new Flags().setBackfaces(false))
                .a(/*        */new Material(new Color(0.5, 0.5, 0.6), new Color(0.9, 0.9, 0.9), 0, 1, 70));
        for (int i = 0; i < howmany; i++) {
            float r = fl(0.1f);
            material.a(/**/new Name("teapot" + i, new PickListener() {
                @Override
                public void onPick(Name sender) {
                    window.showNotification(sender.getId());
                }
            })).a(/*  */new Rotate(fl(360), fl() - 0.5f, fl() - 0.5f, fl() - 0.5f))
                    .a(/*    */new Translate(fl(10), fl(10), fl(10))).a(/*      */new Scale(r, r, r))
                    .a(/*        */new Teapot().ref("coreTeapot"));
        }

        canvas.setMovementType(SceneJSCanvas.MovementType.MODEL);
    }

    protected void spheres(SceneJSCanvas canvas, final Window window, int howmany) {
        canvas.addNode(new Library(new Sphere(1).core("coreSphere"), new Sphere2(1).core("coreSphere2"),
                new Teapot().core("coreTeapot"), new Cylinder(1, 10, 24).core("coreCylinder")));
        SceneJSNode material = canvas.a(/**/new LookAt(new Point(0, 10, 15), new Point(), new Point(0, 1, 0)))
                .a(/*  */new Camera("camera", 25, 1.25, 0.1, 300))
                .ap(/*  */new DirectionalLight(new Color(1, 1, 1), new Point(1, -0.5, -1), true, true))
                .ap(/*  */new DirectionalLight(new Color(1, 1, 0.8), new Point(0, -0.5, -1), true, false))
                .a(/*  */new Rotate("pitch", 0, 1, 0, 0)).a(/*    */new Rotate("yaw", 0, 0, 1, 0))
                .a(/*      */new Flags().setBackfaces(false))
                .a(/*        */new Material(new Color(0.5, 0.5, 0.6), new Color(0.9, 0.9, 0.9), 0, 1, 70));
        for (int i = 0; i < howmany; i++) {
            float r = fl(0.1f);
            material.a(/**/new Name("pipe" + i, new PickListener() {
                @Override
                public void onPick(Name sender) {
                    window.showNotification(sender.getId());
                }
            })).a(/*  */new Rotate(fl(360), fl() - 0.5f, fl() - 0.5f, fl() - 0.5f))
                    .a(/*    */new Translate(fl(10), fl(10), fl(10))).a(/*      */new Scale(r, r, r))
                    .a(/*        */new Sphere2().ref("coreSphere2"));
        }

        canvas.setMovementType(SceneJSCanvas.MovementType.MODEL);
    }

    protected void texts(SceneJSCanvas canvas, final Window window, int howmany) {
        SceneJSNode material = canvas
                .addNode(new LookAt(new Point(0, 10, 15), new Point(0, 0, 0), new Point(0, 1, 0))).
                /**/
                addNode(new Camera("camera", 25, 1.25f, 0.1f, 300)).
                /* */
                addNodeP(new DirectionalLight(new Color(1, 1, 1), new Point(1, -0.5f, -1), true, true)).
                /* */
                addNodeP(new DirectionalLight(new Color(1, 1, 0.8f), new Point(0, -0.5f, -1), true, false)).
                /* */
                addNode(new Rotate("pitch", 0, 1, 0, 0)).
                /*   */addNode(new Rotate("yaw", 0, 0, 1, 0)).
                /*     */
                addNode(new Material(new Color(0.5f, 0.5f, 0.6f), new Color(0.9f, 0.9f, 0.9f), 0, 1, 70));
        for (int i = 0; i < howmany; i++) {
            float r = RANDOM.nextFloat() / 10;
            material.addNode(new Name("pipe" + i, new PickListener() {
                @Override
                public void onPick(Name sender) {
                    window.showNotification(sender.getId());
                }
            })).addNode(new Rotate(RANDOM.nextFloat() * 360, RANDOM.nextFloat() - 0.5f, RANDOM.nextFloat() - 0.5f,
                    RANDOM.nextFloat() - 0.5f))
                    .addNode(new Translate(RANDOM.nextFloat() * 10, RANDOM.nextFloat() * 10,
                            RANDOM.nextFloat() * 10))
                    .addNode(new Scale(r, r, r)).addNode(new Text("text " + i, 10, 1));
        }

        canvas.setMovementType(SceneJSCanvas.MovementType.MODEL);
    }

    protected void texts2(SceneJSCanvas canvas, final Window window, int howmany) {
        SceneJSNode material = canvas
                .addNode(new LookAt(new Point(0, 10, 15), new Point(0, 0, 0), new Point(0, 1, 0))).
                /**/
                addNode(new Camera("camera", 25, 1.25f, 0.1f, 300)).
                /* */
                addNodeP(new DirectionalLight(new Color(1, 1, 1), new Point(1, -0.5f, -1), true, true)).
                /* */
                addNodeP(new DirectionalLight(new Color(1, 1, 0.8f), new Point(0, -0.5f, -1), true, false)).
                /* */
                addNode(new Rotate("pitch", 0, 1, 0, 0)).
                /*   */addNode(new Rotate("yaw", 0, 0, 1, 0)).
                /*     */
                addNode(new Material(new Color(0.5f, 0.5f, 0.6f), new Color(0.9f, 0.9f, 0.9f), 0, 1, 70));
        for (int i = 0; i < howmany; i++) {
            float r = RANDOM.nextFloat() / 10;
            material.addNode(new Name("pipe" + i, new PickListener() {
                @Override
                public void onPick(Name sender) {
                    window.showNotification(sender.getId());
                }
            })).addNode(new Rotate(RANDOM.nextFloat() * 360, RANDOM.nextFloat() - 0.5f, RANDOM.nextFloat() - 0.5f,
                    RANDOM.nextFloat() - 0.5f))
                    .addNode(new Translate(RANDOM.nextFloat() * 10, RANDOM.nextFloat() * 10,
                            RANDOM.nextFloat() * 10))
                    .addNode(new Scale(r, r, r)).addNode(new Text2("text2 " + i, 10, 1));
        }

        canvas.setMovementType(SceneJSCanvas.MovementType.MODEL);
    }
}