1 /*
   2  * Copyright (c) 2010, 2014, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package hello;
  27 
  28 import javafx.application.Application;
  29 import javafx.scene.Group;
  30 import javafx.scene.Scene;
  31 import javafx.scene.paint.Color;
  32 import javafx.scene.shape.Rectangle;
  33 import javafx.stage.Stage;
  34 
  35 public class HelloLaunchOnNewThread extends Application {
  36 
  37     static long startTime;
  38 
  39     public HelloLaunchOnNewThread() {
  40         long endTime = System.nanoTime();
  41         long elapsedMsec = (endTime - startTime + 500000) / 1000000;
  42         System.err.println("DONE: elapsed time = " + elapsedMsec + " msec");
  43         System.err.println("Constructor: currentThread="
  44                 + Thread.currentThread().getName());
  45     }
  46 
  47     @Override public void init() {
  48         System.err.println("init: currentThread="
  49                 + Thread.currentThread().getName());
  50     }
  51 
  52     @Override public void start(Stage stage) {
  53         System.err.println("start: currentThread="
  54                 + Thread.currentThread().getName());
  55 
  56         stage.setTitle("Launch from New Thread");
  57 
  58         Group root = new Group();
  59         Scene scene = new Scene(root, 600, 450);
  60         scene.setFill(Color.LIGHTGREEN);
  61 
  62         Rectangle rect = new Rectangle();
  63         rect.setX(25);
  64         rect.setY(40);
  65         rect.setWidth(100);
  66         rect.setHeight(50);
  67         rect.setFill(Color.RED);
  68 
  69         root.getChildren().add(rect);
  70         stage.setScene(scene);
  71         stage.show();
  72         System.err.println("You should now see the 'HelloWorld' rectangle in the window");
  73     }
  74 
  75     @Override public void stop() {
  76         System.err.println("stop: currentThread="
  77                 + Thread.currentThread().getName());
  78     }
  79 
  80     /**
  81      * @param args the command line arguments
  82      */
  83     public static void main(final String[] args) {
  84         System.err.println("main: currentThread="
  85                 + Thread.currentThread().getName());
  86         new Thread(() -> {
  87             // Sleep for a very short time to ensure main thread exits,
  88             // since that will provoke RT-9824
  89             try {
  90                 Thread.sleep(100);
  91             } catch (InterruptedException ex) {}
  92             System.err.println("Calling Application.launch from currentThread="
  93                     + Thread.currentThread().getName());
  94             System.err.print("LAUNCHING...");
  95             System.err.flush();
  96             startTime = System.nanoTime();
  97             Application.launch(HelloLaunchOnNewThread.class, args);
  98             System.err.println("Application.launch returns");
  99         }).start();
 100         System.err.println("Main thread exiting: currentThread="
 101                 + Thread.currentThread().getName());
 102     }
 103 }