Finding Out About Device Space

In the vast majority of cases, the programmer doesn't need to worry about device space. However, when you do need to know some details about device spaces and the characteristics of the available devices, the following three classes are invaluable:

  • GraphicsEnvironment

  • GraphicsDevice

  • GraphicsConfiguration

Objects generated by the GraphicsEnvironment return a list of all devices on a platform. This list includes the expected printers and video displays, but also lists memory buffers and fonts. Java supports a multimonitor environment that can be important for some imaging and virtual reality applications. More information on this topic is given in Chapter 13, “Working with Input and Output Devices.”

Objects instantiated from the GraphicsDevice and GraphicsConfiguration classes refer to individual devices and configurations, respectively. Note that a single device might have multiple configurations associated with it. Listing 2.1 can be used to query the graphics environment (stored in the examples under GraphicsQuery.java).

Listing 2.1 GraphicsQuery
import java.awt.*;
import javax.swing.*;
import  java.awt.event.*;
import java.awt.image.*;
import java.applet.Applet;

public class GraphicsQuery extends JApplet {

    public GraphicsQuery() {

        BufferedImage big =
new BufferedImage(200, 200,BufferedImage.TYPE_INT_ARGB);

       GraphicsEnvironment ge =
            GraphicsEnvironment.getLocalGraphicsEnvironment();

       //list all fonts font families on the platform

       System.out.println("****START LISTING FONTS****");

       String[] fonts = ge.getAvailableFontFamilyNames();
       for (int i=0; i < fonts.length; i++) {
           System.out.println("AVAILABLE FONTS; i: " + i +
                              " FONT NAME: " + fonts[i]);
       }

       System.out.println("****STOP LISTING FONTS****");

       GraphicsDevice dscreen = ge.getDefaultScreenDevice();

  System.out.println("DEFAULT SCREEN ID: " +
 dscreen.getIDstring() +  " DEVICE TYPE: " +
 dscreen.getType());

       //the following gets an array of screen devices;
  //the number is usually one but sometimes many

       GraphicsDevice[] gs = ge.getScreenDevices();
        for (int i = 0; i < gs.length; i++) {
          GraphicsDevice gd = gs[i];
          GraphicsConfiguration[] gc = gd.getConfigurations();

          for (int j=0; j < gc.length; j++) {
            Rectangle gcBounds = gc[j].getBounds();
            System.out.println("SCREEN DEVICE #: " + j +
                               " TYPE: " + gd.getType() +
                               " x bounds: " + gcBounds.x +
                               " y bounds: " + gcBounds.y);
          }
        }
  }

   public static void main(String arg[]) {
        GraphicsQuery gq = new GraphicsQuery();
    }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset