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