Testing Screen Resolution

This section provides a tutorial example on how to test the graphics resolution of the local screen using the default Toolkit object returned by the Toolkit.getDefaultToolkit() static method.

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

/* ScreenResolution.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
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.setContentPane(new MyComponent());
   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.drawString("One inch ("+dpi+" pixel) boxes",0,140);
         for (int i=0; i<width/dpi+1; i++) {

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:

Screen Resolution
Screen Resolution

The example program also gives me the following numbers 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 result tells 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.

Table of Contents

 About This Book

 JDK (Java Development Kit)

 Introduction of Java Swing Package

Graphics Environment of the Local System

 java.awt.GraphicsEnvironment - Graphics Environment Class

 java.awt.Toolkit - AWT Base Class

Testing Screen Resolution

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

 JComboBox - Swing Combo Box Class

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

 AWT (Abstract Windows Toolkit)

 Integration with Desktop System

 Outdated Tutorials


 Full Version in PDF/EPUB