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

Graphics Environment

Part:   1  2  

(Continued from previous part...)

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

/**
 * DefaultToolkit.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import javax.swing.*;
public class DefaultToolkit {
   public static void main(String[] a) {
      Toolkit t = Toolkit.getDefaultToolkit();
      Dimension d = t.getScreenSize();
      System.out.println("Screen size: "+d.width+", "+d.height);
      System.out.println("Screen resolution: "+t.getScreenResolution());
   }
}

Output:

Screen size: 1024, 768
Screen resolution: 96

Comparing the output of DefaultToolkit.java with LocalGraphicsEnvironment, the toolkit screen size doesn't match the environment window bounds. I don't know why. I also don't know how to read the screen resolution value. Is it 96 DPI (Dots Per Inch)?

Screen Resolution

To answer my question raised in the previous section, I wrote the following program and did some tests:

/**
 * ScreenResolution.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.awt.*;
import javax.swing.*;
public class ScreenResolution {
   static int dpi;
   static int width, height;
   public static void main(String[] a) {
      Toolkit t = Toolkit.getDefaultToolkit();
      dpi = t.getScreenResolution();
      width = t.getScreenSize().width;
      height = t.getScreenSize().height;
      System.out.println("Width = "+width);
      System.out.println("Height = "+height);
      System.out.println("DPI = "+dpi);
      JFrame f = new JFrame("Screen Size and Resolution");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setContentPane(new MyComponent());
      f.setExtendedState(Frame.MAXIMIZED_BOTH);
      f.setVisible(true);
   }
   static class MyComponent extends JComponent {
      public void paint(Graphics g) {
         g.drawString("100 pixel boxes",0,20);
         for (int i=0; i<width/100+1; i++) {
            g.drawRect(i*100,20,100,100);
         }
         g.drawString("One inch ("+dpi+" pixel) boxes",0,140);
         for (int i=0; i<width/dpi+1; i++) {
            g.drawRect(i*dpi,140,dpi,dpi);
         }
      }
   }
}

For the first test, I changed my Windows screen setting (Control Panel / Display / Settings / Screen Area) to 1024 x 786, and executed my program. It shows about 10.* 100-pixel boxes and 10.* one-inch boxes, and gave me the following in the console window:

Width = 1024
Height = 768
DPI = 96

For the second test, I changed my Windows screen setting (Control Panel / Display / Settings / Screen Area) to 800 x 600, and executed my program. It shows about 8 100-pixel boxes and 8.* one-inch boxes, and gave me the following in the console window:

Width = 800
Height = 600
DPI = 96

So the results tell me that the size of my one-inch boxes is not really one inch, if you measure them on the screen. The screen resolution I got from the default toolkit is not following the screen setting. In other words, I am not getting the real resolution of my screen. If anyone knows how to get the real resolution, please tell me.

Part:   1  2  

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