src/java.desktop/share/classes/java/awt/GraphicsEnvironment.java

Print this page




  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 
  27 package java.awt;
  28 
  29 import java.awt.image.BufferedImage;
  30 import java.security.AccessController;

  31 import java.util.Locale;
  32 
  33 import sun.font.FontManager;
  34 import sun.font.FontManagerFactory;
  35 import sun.java2d.HeadlessGraphicsEnvironment;
  36 import sun.java2d.SunGraphicsEnvironment;
  37 import sun.security.action.GetPropertyAction;
  38 
  39 /**
  40  *
  41  * The <code>GraphicsEnvironment</code> class describes the collection
  42  * of {@link GraphicsDevice} objects and {@link java.awt.Font} objects
  43  * available to a Java(tm) application on a particular platform.
  44  * The resources in this <code>GraphicsEnvironment</code> might be local
  45  * or on a remote machine.  <code>GraphicsDevice</code> objects can be
  46  * screens, printers or image buffers and are the destination of
  47  * {@link Graphics2D} drawing methods.  Each <code>GraphicsDevice</code>
  48  * has a number of {@link GraphicsConfiguration} objects associated with
  49  * it.  These objects specify the different configurations in which the
  50  * <code>GraphicsDevice</code> can be used.


 144     /**
 145      * @return warning message if headless state is assumed by default;
 146      * null otherwise
 147      * @since 1.5
 148      */
 149     static String getHeadlessMessage() {
 150         if (headless == null) {
 151             getHeadlessProperty(); // initialize the values
 152         }
 153         return defaultHeadless != Boolean.TRUE ? null :
 154             "\nNo X11 DISPLAY variable was set, " +
 155             "but this program performed an operation which requires it.";
 156     }
 157 
 158     /**
 159      * @return the value of the property "java.awt.headless"
 160      * @since 1.4
 161      */
 162     private static boolean getHeadlessProperty() {
 163         if (headless == null) {
 164             java.security.AccessController.doPrivileged(
 165             new java.security.PrivilegedAction<Object>() {
 166                 public Object run() {
 167                     String nm = System.getProperty("java.awt.headless");
 168 
 169                     if (nm == null) {
 170                         /* No need to ask for DISPLAY when run in a browser */
 171                         if (System.getProperty("javaplugin.version") != null) {
 172                             headless = defaultHeadless = Boolean.FALSE;
 173                         } else {
 174                             String osName = System.getProperty("os.name");
 175                             if (osName.contains("OS X") && "sun.awt.HToolkit".equals(
 176                                     System.getProperty("awt.toolkit")))
 177                             {
 178                                 headless = defaultHeadless = Boolean.TRUE;
 179                             } else {

 180                                 headless = defaultHeadless =
 181                                     Boolean.valueOf(("Linux".equals(osName) ||
 182                                                      "SunOS".equals(osName) ||
 183                                                      "FreeBSD".equals(osName) ||
 184                                                      "NetBSD".equals(osName) ||
 185                                                      "OpenBSD".equals(osName) ||
 186                                                      "AIX".equals(osName)) &&
 187                                                      (System.getenv("DISPLAY") == null));
 188                             }
 189                         }
 190                     } else if (nm.equals("true")) {
 191                         headless = Boolean.TRUE;
 192                     } else {
 193                         headless = Boolean.FALSE;
 194                     }
 195                     return null;

 196                 }
 197                 }
 198             );
 199         }
 200         return headless.booleanValue();
 201     }
 202 
 203     /**
 204      * Check for headless state and throw HeadlessException if headless
 205      * @since 1.4
 206      */
 207     static void checkHeadless() throws HeadlessException {
 208         if (isHeadless()) {
 209             throw new HeadlessException();
 210         }
 211     }
 212 
 213     /**
 214      * Returns whether or not a display, keyboard, and mouse can be
 215      * supported in this graphics environment.  If this returns true,
 216      * <code>HeadlessException</code> will be thrown from areas of the
 217      * graphics environment that are dependent on a display, keyboard, or
 218      * mouse.
 219      * @return <code>true</code> if a display, keyboard, and mouse
 220      * can be supported in this environment; <code>false</code>




  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 
  27 package java.awt;
  28 
  29 import java.awt.image.BufferedImage;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.util.Locale;
  33 
  34 import sun.font.FontManager;
  35 import sun.font.FontManagerFactory;
  36 import sun.java2d.HeadlessGraphicsEnvironment;
  37 import sun.java2d.SunGraphicsEnvironment;
  38 import sun.security.action.GetPropertyAction;
  39 
  40 /**
  41  *
  42  * The <code>GraphicsEnvironment</code> class describes the collection
  43  * of {@link GraphicsDevice} objects and {@link java.awt.Font} objects
  44  * available to a Java(tm) application on a particular platform.
  45  * The resources in this <code>GraphicsEnvironment</code> might be local
  46  * or on a remote machine.  <code>GraphicsDevice</code> objects can be
  47  * screens, printers or image buffers and are the destination of
  48  * {@link Graphics2D} drawing methods.  Each <code>GraphicsDevice</code>
  49  * has a number of {@link GraphicsConfiguration} objects associated with
  50  * it.  These objects specify the different configurations in which the
  51  * <code>GraphicsDevice</code> can be used.


 145     /**
 146      * @return warning message if headless state is assumed by default;
 147      * null otherwise
 148      * @since 1.5
 149      */
 150     static String getHeadlessMessage() {
 151         if (headless == null) {
 152             getHeadlessProperty(); // initialize the values
 153         }
 154         return defaultHeadless != Boolean.TRUE ? null :
 155             "\nNo X11 DISPLAY variable was set, " +
 156             "but this program performed an operation which requires it.";
 157     }
 158 
 159     /**
 160      * @return the value of the property "java.awt.headless"
 161      * @since 1.4
 162      */
 163     private static boolean getHeadlessProperty() {
 164         if (headless == null) {
 165             AccessController.doPrivileged((PrivilegedAction<Object>) () -> {


 166                 String nm = System.getProperty("java.awt.headless");
 167 
 168                 if (nm == null) {
 169                     /* No need to ask for DISPLAY when run in a browser */
 170                     if (System.getProperty("javaplugin.version") != null) {
 171                         headless = defaultHeadless = Boolean.FALSE;
 172                     } else {
 173                         String osName = System.getProperty("os.name");
 174                         if (osName.contains("OS X") && "sun.awt.HToolkit".equals(
 175                                 System.getProperty("awt.toolkit")))
 176                         {
 177                             headless = defaultHeadless = Boolean.TRUE;
 178                         } else {
 179                             final String display = System.getenv("DISPLAY");
 180                             headless = defaultHeadless =
 181                                 ("Linux".equals(osName) ||
 182                                  "SunOS".equals(osName) ||
 183                                  "FreeBSD".equals(osName) ||
 184                                  "NetBSD".equals(osName) ||
 185                                  "OpenBSD".equals(osName) ||
 186                                  "AIX".equals(osName)) &&
 187                                  (display == null || display.isEmpty());
 188                         }
 189                     }


 190                 } else {
 191                     headless = "true".equals(nm);
 192                 }
 193                 return null;
 194             });
 195         }
 196         return headless;



 197     }
 198 
 199     /**
 200      * Check for headless state and throw HeadlessException if headless
 201      * @since 1.4
 202      */
 203     static void checkHeadless() throws HeadlessException {
 204         if (isHeadless()) {
 205             throw new HeadlessException();
 206         }
 207     }
 208 
 209     /**
 210      * Returns whether or not a display, keyboard, and mouse can be
 211      * supported in this graphics environment.  If this returns true,
 212      * <code>HeadlessException</code> will be thrown from areas of the
 213      * graphics environment that are dependent on a display, keyboard, or
 214      * mouse.
 215      * @return <code>true</code> if a display, keyboard, and mouse
 216      * can be supported in this environment; <code>false</code>