001: /*
002: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: */
032:
033: package com.db.utils.fullscreen;
034:
035: import java.awt.Component;
036: import java.awt.DisplayMode;
037: import java.awt.event.ActionEvent;
038: import java.awt.event.ActionListener;
039: import java.awt.event.WindowAdapter;
040: import java.awt.event.WindowEvent;
041: import java.awt.event.WindowListener;
042: import java.awt.GraphicsDevice;
043: import java.awt.GraphicsEnvironment;
044: import javax.swing.JFrame;
045: import javax.swing.JMenu;
046: import javax.swing.JMenuBar;
047: import javax.swing.JMenuItem;
048: import javax.swing.JPopupMenu;
049:
050: // This code is based upon the code from Jeremy
051: // Site http://www.computerbooth.com/j3d/
052: // Email jeremy@computerbooth.com
053:
054: public class ResizableJFrame {
055:
056: private JFrame mainFrame;
057: private DisplayMode originalDM;
058: private GraphicsDevice device;
059:
060: /** Creates new Display */
061: public ResizableJFrame(ScreenProperties properties) {
062:
063: mainFrame = new JFrame();
064:
065: // we need to manually catch the kill window event so all our threads get completely shut down
066: WindowListener l = new WindowAdapter() {
067: public void windowClosing(WindowEvent e) {
068: quitSelected();
069: }
070: };
071:
072: mainFrame.addWindowListener(l);
073:
074: // Disable lightweight menus
075: JPopupMenu.setDefaultLightWeightPopupEnabled(false);
076:
077: JMenuBar menubar = new JMenuBar();
078:
079: // File menu
080: JMenu file_menu = new JMenu("File");
081: menubar.add(file_menu);
082:
083: JMenuItem close_menu = new JMenuItem("Exit");
084: SceneActionListener listener = new SceneActionListener();
085: close_menu.addActionListener(listener);
086: listener.setExitItem(close_menu);
087: file_menu.add(close_menu);
088:
089: mainFrame.setJMenuBar(menubar);
090:
091: GraphicsEnvironment env = GraphicsEnvironment
092: .getLocalGraphicsEnvironment();
093: device = env.getDefaultScreenDevice();
094:
095: originalDM = device.getDisplayMode();
096:
097: if (properties.isFullscreen()) {
098: System.out.println("Setting fullscreen resolution "
099: + properties.getResolutionWidth() + "x"
100: + properties.getResolutionHeight() + " in "
101: + properties.getColourDepth() + " bit colour at "
102: + properties.getRefreshRate() + "hz");
103: mainFrame.setUndecorated(true);
104: mainFrame.setSize(properties.getResolutionDimension());
105: device.setFullScreenWindow(mainFrame);
106: device.setDisplayMode(new DisplayMode(properties
107: .getResolutionWidth(), properties
108: .getResolutionHeight(),
109: properties.getColourDepth(), properties
110: .getRefreshRate()));
111: } else {
112: System.out.println("Setting window to "
113: + properties.getResolutionDimension());
114: mainFrame.setSize(properties.getResolutionDimension());
115: mainFrame.show();
116: }
117:
118: }
119:
120: public void add(Component c) {
121:
122: mainFrame.getContentPane().add(c);
123:
124: }
125:
126: public void add(String s, Component c) {
127:
128: mainFrame.getContentPane().add(s, c);
129:
130: }
131:
132: public void show() {
133:
134: mainFrame.show();
135:
136: }
137:
138: public void reset() {
139:
140: try {
141: device.setDisplayMode(originalDM);
142: } catch (Exception e) {
143: }
144:
145: }
146:
147: /**
148: * Called when a gui component selects to quit
149: */
150: public void quitSelected() {
151:
152: reset();
153: System.exit(0);
154:
155: }
156:
157: public class SceneActionListener implements ActionListener {
158:
159: private JMenuItem exitItem;
160:
161: public SceneActionListener() {
162: }
163:
164: public void setExitItem(JMenuItem exitItem) {
165:
166: this .exitItem = exitItem;
167:
168: }
169:
170: /**
171: * An mouse action has occurred. Used to process menu item selection.
172: *
173: * @param evt The event that caused this method to be called.
174: */
175: public void actionPerformed(ActionEvent evt) {
176:
177: Object src = evt.getSource();
178:
179: //System.out.println("Caught event " + evt);
180:
181: if (src == exitItem) {
182: //System.out.println("Close selected from menu");
183: try {
184: device.setDisplayMode(originalDM);
185: } catch (Exception e) {
186: }
187: quitSelected();
188: }
189:
190: }
191:
192: }
193:
194: }
|