nashorn/samples/colorfulcircles.js
author lana
Thu, 18 Jun 2015 01:43:08 -0700
changeset 31187 fdd3914e481e
parent 29995 ead3020640fc
permissions -rw-r--r--
Added tag jdk9-b69 for changeset 20f60dc00878
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29995
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
     1
/*
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
     3
 *
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
     6
 * are met:
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
     7
 *
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    10
 *
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    14
 *
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    18
 *
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    30
 */
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    31
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    32
// Nashorn port of ColorfulCircles.java JavaFX animation example at
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    33
// https://docs.oracle.com/javafx/2/get_started/ColorfulCircles.java.html
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    34
// ColorfulCircles.java is under the following license terms:
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    35
 
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    36
/*
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    37
* Copyright (c) 2011, 2012 Oracle and/or its affiliates.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    38
* All rights reserved. Use is subject to license terms.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    39
*
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    40
* This file is available and licensed under the following license:
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    41
*
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    42
* Redistribution and use in source and binary forms, with or without
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    43
* modification, are permitted provided that the following conditions
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    44
* are met:
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    45
*
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    46
* - Redistributions of source code must retain the above copyright
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    47
* notice, this list of conditions and the following disclaimer.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    48
* - Redistributions in binary form must reproduce the above copyright
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    49
* notice, this list of conditions and the following disclaimer in
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    50
* the documentation and/or other materials provided with the distribution.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    51
* - Neither the name of Oracle nor the names of its
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    52
* contributors may be used to endorse or promote products derived
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    53
* from this software without specific prior written permission.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    54
*
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    55
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    56
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    57
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    58
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    59
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    60
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    61
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    62
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    63
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    64
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    65
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    66
*/
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    67
 
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    68
// Usage: jjs -fx colorfulcircles.fx
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    69
 
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    70
// Porting note: No imports - just load these fx scripts!
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    71
load("fx:controls.js");
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    72
load("fx:graphics.js");
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    73
 
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    74
// Porting note: whatever is inside
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    75
// public void start(Stage primaryStage)
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    76
// goes into "start" function
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    77
 
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    78
function start(primaryStage) {
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    79
    // Porting note: Replace types with 'var'. "Group root" becomes "var root".
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    80
    // and so on..
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    81
 
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    82
    var root = new Group();
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    83
    var scene = new Scene(root, 800, 600, Color.BLACK);
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    84
    primaryStage.setScene(scene);
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    85
    var circles = new Group();
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    86
    // Porting note: for (int i = 0....) becomes for (var i = 0...)
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    87
 
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    88
    for (var i = 0; i < 30; i++) {
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    89
        var circle = new Circle(150, Color.web("white", 0.05));
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    90
        circle.setStrokeType(StrokeType.OUTSIDE);
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    91
        circle.setStroke(Color.web("white", 0.16));
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    92
        circle.setStrokeWidth(4);
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    93
        circles.getChildren().add(circle);
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    94
    }
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    95
 
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    96
    // Porting note: There is no "f" suffix for float literals in JS.
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    97
    // LinearGradient(0f, 1f, 1f, 0f,..) becomes just
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    98
    // LinearGradient(0, 1, 1, 0,..)
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
    99
 
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   100
    // Porting note: LinearGradient's constructor is a varargs method
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   101
    // No need to create Stop[] just pass more Stop objects at the end!
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   102
    var colors = new Rectangle(scene.getWidth(), scene.getHeight(),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   103
       new LinearGradient(0, 1, 1, 0, true, CycleMethod.NO_CYCLE,
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   104
       new Stop(0, Color.web("#f8bd55")),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   105
       new Stop(0.14, Color.web("#c0fe56")),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   106
       new Stop(0.28, Color.web("#5dfbc1")),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   107
       new Stop(0.43, Color.web("#64c2f8")),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   108
       new Stop(0.57, Color.web("#be4af7")),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   109
       new Stop(0.71, Color.web("#ed5fc2")),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   110
       new Stop(0.85, Color.web("#ef504c")),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   111
       new Stop(1, Color.web("#f2660f"))));
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   112
    colors.widthProperty().bind(scene.widthProperty());
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   113
    colors.heightProperty().bind(scene.heightProperty());
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   114
    var blendModeGroup =
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   115
       new Group(new Group(new Rectangle(scene.getWidth(), scene.getHeight(),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   116
         Color.BLACK), circles), colors);
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   117
    colors.setBlendMode(BlendMode.OVERLAY);
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   118
    root.getChildren().add(blendModeGroup);
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   119
    circles.setEffect(new BoxBlur(10, 10, 3));
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   120
 
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   121
    // Porting note: Java code uses static import of
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   122
    // java.lang.Math.random. Just use JS Math.random here
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   123
    var random = Math.random;
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   124
 
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   125
    var timeline = new Timeline();
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   126
    // Porting note: Java enhanced for loop
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   127
    // for (Node circle : circles.getChildren())
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   128
    // becomes
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   129
    // for each (var circle: circles.getChildren())
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   130
  
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   131
    for each (var circle in circles.getChildren()) {
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   132
        timeline.getKeyFrames().addAll(
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   133
            new KeyFrame(Duration.ZERO, // set start position at 0
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   134
            new KeyValue(circle.translateXProperty(), random() * 800),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   135
            new KeyValue(circle.translateYProperty(), random() * 600)),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   136
            new KeyFrame(new Duration(40000), // set end position at 40s
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   137
            new KeyValue(circle.translateXProperty(), random() * 800),
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   138
            new KeyValue(circle.translateYProperty(), random() * 600)));
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   139
    }
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   140
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   141
    // play 40s of animation
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   142
    timeline.play();
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   143
    primaryStage.show();
ead3020640fc 8078174: Add few FX and parser API samples for nashorn
sundar
parents:
diff changeset
   144
}