Herong's Tutorial Notes on Swing
Dr. Herong Yang, Version 3.05, 2006

Graphics Environment

Part:   1   2 

This chapter discusses:

  • Local graphics environment
  • Screen information from default toolkit
  • Screen resolution

Local Graphics Environment

java.awt.GraphicsEnvironment is an AWT class representing graphics environments 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 are the text fonts 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

Screen Information of Default Toolkit

java.awt.Toolkit is an AWT class acting as a base class for all implementations of AWT. This class also offers a static method, getDetaulToolkit(), to return a Toolkit object representing the default implementation of AWT.

You can use this default toolkit object to get information of the default graphics device, the local screen. For example, you can find out the size and resolution of the local screen.

(Continued on next part...)

Part:   1   2 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Swing - Graphics Environment