12.2. The GraphicsDevice Class

Individual devices are represented by instances of java.awt.GraphicsDevice. Currently this class supports three types of devices, represented by integer constants:

public static final int TYPE_RASTER_SCREEN

This constant represents a monitor.

public static final int TYPE_PRINTER

This constant represents any kind of printer.

public static final int TYPE_IMAGE_BUFFER

This constant represents an image. Remember that you can render into an image using a Graphics or Graphics2D object. In this sense, the image is a kind of "virtual device."

To find out the type of a particular device, call the getType() method:

public abstract int getType()

This method returns the device type, which should be one of the constants TYPE_RASTER_SCREEN, TYPE_PRINTER, or TYPE_IMAGE_BUFFER.

Devices also have a name, called an identification string :

public abstract String getIDstring()

This method returns the name of the device. Note the nonstandard capitalization of this method name.

The following program prints out the ID string of every screen device on your system:

import java.awt.*;

public class ShowDevices {
  public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] screenDevices = ge.getScreenDevices();
    for (int i = 0; i < screenDevices.length; i++)
      System.out.println(screenDevices[i].getIDstring());
    System.exit(0);
  }
}

On my machine, I only have one screen device:

C:\>java ShowDevices :0.0 ...

Get Java 2D Graphics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.