/*
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt;
import java.awt.image.BufferedImage;
import java.security.AccessController;
import java.util.Locale;
import sun.font.FontManager;
import sun.font.FontManagerFactory;
import sun.java2d.HeadlessGraphicsEnvironment;
import sun.java2d.SunGraphicsEnvironment;
import sun.security.action.GetPropertyAction;
/**
*
* The GraphicsEnvironment class describes the collection
* of {@link GraphicsDevice} objects and {@link java.awt.Font} objects
* available to a Java(tm) application on a particular platform.
* The resources in this GraphicsEnvironment might be local
* or on a remote machine. GraphicsDevice objects can be
* screens, printers or image buffers and are the destination of
* {@link Graphics2D} drawing methods. Each GraphicsDevice
* has a number of {@link GraphicsConfiguration} objects associated with
* it. These objects specify the different configurations in which the
* GraphicsDevice can be used.
* @see GraphicsDevice
* @see GraphicsConfiguration
*/
public abstract class GraphicsEnvironment {
private static GraphicsEnvironment localEnv;
/**
* The headless state of the Toolkit and GraphicsEnvironment
*/
private static Boolean headless;
/**
* The headless state assumed by default
*/
private static Boolean defaultHeadless;
/**
* This is an abstract class and cannot be instantiated directly.
* Instances must be obtained from a suitable factory or query method.
*/
protected GraphicsEnvironment() {
}
/**
* Returns the local GraphicsEnvironment.
* @return the local GraphicsEnvironment
*/
public static synchronized GraphicsEnvironment getLocalGraphicsEnvironment() {
if (localEnv == null) {
localEnv = createGE();
}
return localEnv;
}
/**
* Creates and returns the GraphicsEnvironment, according to the
* system property 'java.awt.graphicsenv'.
*
* @return the graphics environment
*/
private static GraphicsEnvironment createGE() {
GraphicsEnvironment ge;
String nm = AccessController.doPrivileged(new GetPropertyAction("java.awt.graphicsenv", null));
try {
// long t0 = System.currentTimeMillis();
Class> geCls;
try {
// First we try if the bootclassloader finds the requested
// class. This way we can avoid to run in a privileged block.
geCls = Class.forName(nm);
} catch (ClassNotFoundException ex) {
// If the bootclassloader fails, we try again with the
// application classloader.
ClassLoader cl = ClassLoader.getSystemClassLoader();
geCls = Class.forName(nm, true, cl);
}
ge = (GraphicsEnvironment)geCls.newInstance();
// long t1 = System.currentTimeMillis();
// System.out.println("GE creation took " + (t1-t0)+ "ms.");
if (isHeadless()) {
ge = new HeadlessGraphicsEnvironment(ge);
}
} catch (ClassNotFoundException e) {
throw new Error("Could not find class: "+nm);
} catch (InstantiationException e) {
throw new Error("Could not instantiate Graphics Environment: "
+ nm);
} catch (IllegalAccessException e) {
throw new Error ("Could not access Graphics Environment: "
+ nm);
}
return ge;
}
/**
* Tests whether or not a display, keyboard, and mouse can be
* supported in this environment. If this method returns true,
* a HeadlessException is thrown from areas of the Toolkit
* and GraphicsEnvironment that are dependent on a display,
* keyboard, or mouse.
* @return true if this environment cannot support
* a display, keyboard, and mouse; false
* otherwise
* @see java.awt.HeadlessException
* @since 1.4
*/
public static boolean isHeadless() {
return getHeadlessProperty();
}
/**
* @return warning message if headless state is assumed by default;
* null otherwise
* @since 1.5
*/
static String getHeadlessMessage() {
if (headless == null) {
getHeadlessProperty(); // initialize the values
}
return defaultHeadless != Boolean.TRUE ? null :
"\nNo X11 DISPLAY variable was set, " +
"but this program performed an operation which requires it.";
}
/**
* @return the value of the property "java.awt.headless"
* @since 1.4
*/
private static boolean getHeadlessProperty() {
if (headless == null) {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction