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