1/*
2 * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import java.awt.DisplayMode;
25import java.awt.EventQueue;
26import java.awt.Frame;
27import java.awt.GraphicsDevice;
28import java.awt.GraphicsEnvironment;
29import java.lang.reflect.InvocationTargetException;
30
31/**
32 * Used by the UninitializedDisplayModeChangeTest to change the
33 * display mode.
34 */
35public class DisplayModeChanger {
36
37    public static void main(String[] args)
38        throws InterruptedException, InvocationTargetException
39    {
40        final GraphicsDevice gd =
41            GraphicsEnvironment.getLocalGraphicsEnvironment().
42                getDefaultScreenDevice();
43
44        EventQueue.invokeAndWait(new Runnable() {
45            public void run() {
46                Frame f = null;
47                if (gd.isFullScreenSupported()) {
48                    try {
49                        f = new Frame("DisplayChanger Frame");
50                        gd.setFullScreenWindow(f);
51                        if (gd.isDisplayChangeSupported()) {
52                            DisplayMode dm = findDisplayMode(gd);
53                            if (gd != null) {
54                                gd.setDisplayMode(dm);
55                            }
56                        }
57                        try {
58                            Thread.sleep(1000);
59                        } catch (InterruptedException ex) {
60                            ex.printStackTrace();
61                        }
62                        gd.setFullScreenWindow(null);
63                    } finally {
64                        if (f != null) {
65                            f.dispose();
66                        }
67                    }
68                }
69            }
70        });
71    }
72
73    /**
74     * Finds a display mode that is different from the current display
75     * mode and is likely to cause a display change event.
76     */
77    private static DisplayMode findDisplayMode(GraphicsDevice gd) {
78        DisplayMode dms[] = gd.getDisplayModes();
79        DisplayMode currentDM = gd.getDisplayMode();
80        for (DisplayMode dm : dms) {
81            if (!dm.equals(currentDM) &&
82                 dm.getRefreshRate() == currentDM.getRefreshRate())
83            {
84                // different from the current dm and refresh rate is the same
85                // means that something else is different => more likely to
86                // cause a DM change event
87                return dm;
88            }
89        }
90        return null;
91    }
92
93}
94