Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

java.awt.GraphicsEnvironment - Graphics Environment Class

This section describes java.awt.GraphicsEnvironment - Graphics Environment Class. You can use this class to to get a lot of information about your local graphics device, which is really your computer screen.

java.awt.GraphicsEnvironment is an AWT class representing graphics environment that are accessible by the local operating system. This class also offers a static method, getLocalGraphicsEnvironment(), to return an object representing the local default graphics environment.

You can use this local graphics environment object to get a lot of information about your local system. For example, you can find out what text fonts are available, and what is maximum size of the graphical drawing area on the screen.

To show you how to use the getLocalGraphicsEnvironment() method, I wrote the following sample program:

/**
 * LocalGraphicsEnvironment.java
 * Copyright (c) 2004 by Dr. Herong Yang
 */
import java.awt.*;
public class LocalGraphicsEnvironment {
   public static void main(String[] a) {
      GraphicsEnvironment e 
         = GraphicsEnvironment.getLocalGraphicsEnvironment();
      String n[] = e.getAvailableFontFamilyNames();
      System.out.println("Font families:");
      for (int i=0; i<n.length; i++) {
         System.out.println("   "+n[i]);
      }
      Point p = e.getCenterPoint();
      System.out.println("Window center point: "+p.x+", "+p.y);
      Rectangle r = e.getMaximumWindowBounds();
      System.out.println("Maximum window bounds: "+r.x+", "+r.y
         +", "+r.width+", "+r.height);
      GraphicsDevice g = e.getDefaultScreenDevice();
      System.out.println("Device ID: "+g.getIDstring());
   }
}

Output:

Font families:
   Albertus Extra Bold
   Albertus Medium
   Antique Olive
   Arial
   Arial Black
   Arial Narrow
   ......
Window center point: 512, 370
Maximum window bounds: 0, 0, 1024, 740
Device ID: \Display0

Sample programs listed in this section have been tested with JDK 1.3.1, 1.4.1, 1.5.0 and 1.6.0.

Last update: 2009.

Sections in This Chapter

java.awt.GraphicsEnvironment - Graphics Environment Class

java.awt.Toolkit - AWT Base Class

Testing Screen Resolution

Dr. Herong Yang, updated in 2009
java.awt.GraphicsEnvironment - Graphics Environment Class