nashorn/test/script/jfx/flyingimage.js
changeset 21443 85671274e5fb
child 21690 ffbb4611d1f4
equal deleted inserted replaced
21442:7dd27a5942b1 21443:85671274e5fb
       
     1 /*
       
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  * 
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  * 
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  * 
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  * 
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * Testing JavaFX canvas run by Nashorn.
       
    26  *
       
    27  * @test/nocompare
       
    28  * @run
       
    29  * @fork
       
    30  */
       
    31 
       
    32 TESTNAME = "flyingimage";
       
    33 
       
    34 var Image                = Java.type("javafx.scene.image.Image");
       
    35 var Color                = Java.type("javafx.scene.paint.Color");
       
    36 var Canvas               = Java.type("javafx.scene.canvas.Canvas");
       
    37 var BorderPane           = Java.type("javafx.scene.layout.BorderPane");
       
    38 var StackPane            = Java.type("javafx.scene.layout.StackPane");
       
    39 var Font                 = Java.type("javafx.scene.text.Font");
       
    40 var FontSmoothingType    = Java.type("javafx.scene.text.FontSmoothingType");
       
    41 var Text                 = Java.type("javafx.scene.text.Text");
       
    42 
       
    43 var WIDTH = 800;
       
    44 var HEIGHT = 600;
       
    45 var canvas = new Canvas(WIDTH, HEIGHT);
       
    46 function fileToURL(file) {
       
    47     return new File(file).toURI().toURL().toExternalForm();
       
    48 }
       
    49 var imageUrl = fileToURL(__DIR__ + "flyingimage/flyingimage.png");
       
    50 var img = new Image(imageUrl);
       
    51 var font = new Font("Arial", 16);
       
    52 var t = 0;
       
    53 var isFrameRendered = false;
       
    54 function renderFrame() {
       
    55     var gc = canvas.graphicsContext2D;
       
    56     gc.setFill(Color.web("#cccccc"));
       
    57     gc.fillRect(0, 0, WIDTH, HEIGHT);
       
    58     gc.setStroke(Color.web("#000000"));
       
    59     gc.setLineWidth(1);
       
    60     gc.strokeRect(5, 5, WIDTH - 10, HEIGHT - 10);
       
    61     var c = 200;
       
    62     var msc= 0.5 * HEIGHT / img.height;
       
    63     var sp0 = 0.003;
       
    64     for (var h = 0; h < c; h++, t++) {
       
    65         gc.setTransform(1, 0, 0, 1, 0, 0);
       
    66         var yh = h / (c - 1);
       
    67         gc.translate((0.5 + Math.sin(t * sp0 + h * 0.1) / 3) * WIDTH, 25 + (HEIGHT * 3 / 4 - 40) * (yh * yh));
       
    68         var sc = 30 / img.height + msc * yh * yh;
       
    69         gc.rotate(90 * Math.sin(t * sp0 + h * 0.1 + Math.PI));
       
    70         gc.scale(sc, sc);
       
    71         gc.drawImage(img, -img.width / 2, -img.height / 2);
       
    72      }
       
    73     gc.setTransform(1, 0, 0, 1, 0, 0);
       
    74     isFrameRendered = true;
       
    75 }
       
    76 var stack = new StackPane();
       
    77 var pane = new BorderPane();
       
    78 
       
    79 pane.setCenter(canvas);
       
    80 stack.getChildren().add(pane);
       
    81 $STAGE.scene = new Scene(stack);
       
    82 renderFrame();
       
    83 checkImageAndExit();