diff options
author | Kenneth Russel <[email protected]> | 2001-12-12 07:19:25 +0000 |
---|---|---|
committer | Kenneth Russel <[email protected]> | 2001-12-12 07:19:25 +0000 |
commit | 48c2a0f69ee4f85e2abc0044dca76516fbf834bd (patch) | |
tree | 15bed40d59850abd5753e0adcce9fb75e8eb18d7 /demos | |
parent | 10f15a12d51ef116c46140a45875c011d9c1dbd8 (diff) |
Fixed gearsFullScreen demo
Diffstat (limited to 'demos')
-rw-r--r-- | demos/MiscDemos/ScreenResSelector.java | 201 | ||||
-rw-r--r-- | demos/MiscDemos/gearRenderer.java | 2 | ||||
-rw-r--r-- | demos/MiscDemos/gearsFullScreen.java | 424 | ||||
-rw-r--r-- | demos/MiscDemos/makefile | 5 |
4 files changed, 414 insertions, 218 deletions
diff --git a/demos/MiscDemos/ScreenResSelector.java b/demos/MiscDemos/ScreenResSelector.java new file mode 100644 index 0000000..508fa53 --- /dev/null +++ b/demos/MiscDemos/ScreenResSelector.java @@ -0,0 +1,201 @@ +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import javax.swing.*; + +/** Simple class for selecting a display mode for the purpose of + switching to full-screen mode. + + @author Kenneth Russell ([email protected]) +*/ + +public class ScreenResSelector { + static interface DisplayModeFilter { + public boolean filter(DisplayMode mode); + } + + public static java.util.List getAvailableDisplayModes() { + java.util.List modes = getDisplayModes(); + final DisplayMode curMode = getCurrentDisplayMode(); + // Filter everything which is higher frequency than the current + // display mode + modes = filterDisplayModes(modes, new DisplayModeFilter() { + public boolean filter(DisplayMode mode) { + return (mode.getRefreshRate() <= curMode.getRefreshRate()); + } + }); + // Filter everything that is not at least 24-bit + modes = filterDisplayModes(modes, new DisplayModeFilter() { + public boolean filter(DisplayMode mode) { + return (mode.getBitDepth() >= 24); + } + }); + // Filter everything less than 640x480 + modes = filterDisplayModes(modes, new DisplayModeFilter() { + public boolean filter(DisplayMode mode) { + return (mode.getWidth() >= 640 && mode.getHeight() >= 480); + } + }); + if (modes.size() == 0) { + throw new RuntimeException("Couldn't find any valid display modes"); + } + return modes; + } + + /** Shows a modal dialog containing the available screen resolutions + and requests selection of one of them. Returns the selected one. */ + public static DisplayMode showSelectionDialog() { + SelectionDialog dialog = new SelectionDialog(); + dialog.show(); + dialog.waitFor(); + return dialog.selected(); + } + + public static void main(String[] args) { + DisplayMode mode = showSelectionDialog(); + if (mode != null) { + System.err.println("Selected display mode:"); + System.err.println(modeToString(mode)); + } else { + System.err.println("No display mode selected."); + } + System.exit(0); + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private static DisplayMode getCurrentDisplayMode() { + GraphicsDevice dev = getDefaultScreenDevice(); + return dev.getDisplayMode(); + } + + private static java.util.List/*<DisplayMode>*/ getDisplayModes() { + GraphicsDevice dev = getDefaultScreenDevice(); + DisplayMode[] modes = dev.getDisplayModes(); + return toList(modes); + } + + private static GraphicsDevice getDefaultScreenDevice() { + return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); + } + + private static java.util.List/*<DisplayMode>*/ toList(DisplayMode[] modes) { + java.util.List res = new ArrayList(); + for (int i = 0; i < modes.length; i++) { + res.add(modes[i]); + } + return res; + } + + private static String modeToString(DisplayMode mode) { + return (mode.getBitDepth() + " bits, " + + mode.getWidth() + "x" + mode.getHeight() + ", " + + mode.getRefreshRate() + " Hz"); + } + + private static String[] modesToString(java.util.List/*<DisplayMode>*/ modes) { + String[] res = new String[modes.size()]; + int i = 0; + for (Iterator iter = modes.iterator(); iter.hasNext(); ) { + res[i++] = modeToString((DisplayMode) iter.next()); + } + return res; + } + + private static java.util.List/*<DisplayMode>*/ filterDisplayModes(java.util.List/*<DisplayMode>*/ modes, + DisplayModeFilter filter) { + java.util.List res = new ArrayList(); + for (Iterator iter = modes.iterator(); iter.hasNext(); ) { + DisplayMode mode = (DisplayMode) iter.next(); + if (filter.filter(mode)) { + res.add(mode); + } + } + return res; + } + + static class SelectionDialog extends JFrame { + private Object monitor = new Object(); + private java.util.List modes; + private volatile boolean done = false; + private volatile int selectedIndex; + + public SelectionDialog() { + super(); + + setTitle("Display Modes"); + modes = getAvailableDisplayModes(); + String[] strings = modesToString(modes); + final JList modeList = new JList(strings); + modeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + modeList.setSelectedIndex(0); + JScrollPane scroller = new JScrollPane(modeList); + getContentPane().setLayout(new BorderLayout()); + getContentPane().add(BorderLayout.NORTH, new JLabel("Select display mode:")); + getContentPane().add(BorderLayout.CENTER, scroller); + JPanel buttonPanel = new JPanel(); + buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); + buttonPanel.add(Box.createGlue()); + JButton button = new JButton("OK"); + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + selectedIndex = modeList.getSelectedIndex(); + done = true; + synchronized(monitor) { + monitor.notify(); + } + hide(); + dispose(); + } + }); + buttonPanel.add(button); + buttonPanel.add(Box.createHorizontalStrut(10)); + button = new JButton("Cancel"); + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + selectedIndex = -1; + done = true; + synchronized(monitor) { + monitor.notify(); + } + hide(); + dispose(); + } + }); + buttonPanel.add(button); + buttonPanel.add(Box.createGlue()); + getContentPane().add(BorderLayout.SOUTH, buttonPanel); + pack(); + center(this, Toolkit.getDefaultToolkit().getScreenSize()); + } + + public void waitFor() { + synchronized(monitor) { + while (!done) { + try { + monitor.wait(); + } catch (InterruptedException e) { + } + } + } + } + + public DisplayMode selected() { + if (selectedIndex < 0) { + return null; + } else { + return (DisplayMode) modes.get(selectedIndex); + } + } + } + + private static void center(Component component, + Dimension containerDimension) { + Dimension sz = component.getSize(); + int x = ((containerDimension.width - sz.width) / 2); + int y = ((containerDimension.height - sz.height) / 2); + component.setLocation(x, y); + } +} diff --git a/demos/MiscDemos/gearRenderer.java b/demos/MiscDemos/gearRenderer.java index 345790a..aa5a803 100644 --- a/demos/MiscDemos/gearRenderer.java +++ b/demos/MiscDemos/gearRenderer.java @@ -328,7 +328,7 @@ public class gearRenderer //System.out.println("mouse dragged: "); int x = evt.getX(); int y = evt.getY(); - Dimension size = gldrawable.getSize(); + Dimension size = evt.getComponent().getSize(); float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)size.width); float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)size.height); diff --git a/demos/MiscDemos/gearsFullScreen.java b/demos/MiscDemos/gearsFullScreen.java index 8cabd83..e51ba84 100644 --- a/demos/MiscDemos/gearsFullScreen.java +++ b/demos/MiscDemos/gearsFullScreen.java @@ -1,215 +1,209 @@ -/**
- * @(#) gearsFullScreen.java
- * @(#) author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel)
- *
- * This version is equal to Brian Paul's version 1.2 1999/10/21
- */
-
-import java.applet.*;
-import java.awt.*;
-import java.awt.event.*;
-import java.lang.*;
-import java.util.*;
-import java.io.*;
-import gl4java.*;
-import gl4java.drawable.*;
-import gl4java.awt.GLAnimCanvas;
-import gl4java.applet.SimpleGLAnimApplet1;
-
-public class gearsFullScreen extends SimpleGLAnimApplet1
- implements MouseListener
-{
-
- /* Initialize the applet */
- public void init()
- {
- init(false);
- }
-
- public void init(boolean showGL)
- {
- super.init();
-
- Dimension d = getSize();
-
- GLCapabilities caps = new GLCapabilities();
-
- canvas =
- GLDrawableFactory.getFactory().createGLAnimCanvas(caps, d.width, d.height);
-
- gearRenderer gear = new gearRenderer(showGL);
- canvas.addGLEventListener(gear);
-
- add("Center", canvas);
- addMouseListener(this);
- }
-
- public void destroy()
- {
- removeMouseListener(this);
- super.destroy();
- }
-
-
- public static void main( String args[] )
- {
- int i = 0;
- String gljLib = null;
- String glLib = null;
- String gluLib = null;
- boolean perftest=false;
-
- GLContext.gljNativeDebug = false;
- GLContext.gljClassDebug = false;
- GLContext.gljThreadDebug = false;
-
- while(args.length>i)
- {
- if(args[i].equals("-perftest"))
- {
- perftest=true;
- } else if(args[i].equals("-gljLib"))
- {
- i++;
- if(args.length>i)
- gljLib=args[i];
- } else if(args[i].equals("-glLib"))
- {
- i++;
- if(args.length>i)
- glLib=args[i];
- } else if(args[i].equals("-gluLib"))
- {
- i++;
- if(args.length>i)
- gluLib=args[i];
- } else {
- System.out.println("illegal arg "+i+": "+args[i]);
- }
- i++;
- }
-
- if(perftest)
- {
- GLContext.gljNativeDebug = false;
- GLContext.gljThreadDebug = false;
- GLContext.gljClassDebug = false;
- }
-
- if(perftest)
- GLContext.gljClassDebug=true;
- GLContext.doLoadNativeLibraries(gljLib, glLib, gluLib);
- if(perftest)
- GLContext.gljClassDebug=false;
-
- GraphicsEnvironment env =
- GraphicsEnvironment.getLocalGraphicsEnvironment();
- GraphicsDevice device = env.getDefaultScreenDevice();
- Frame mainFrame = null;
-
- System.out.println("isFullScreenSupported: "+
- device.isFullScreenSupported() );
-
- try {
- GraphicsConfiguration gc ;
- gl4java.drawable.GLDrawableFactory df =
- gl4java.drawable.GLDrawableFactory.getFactory();
-
- if(df instanceof gl4java.drawable.SunJDK13GLDrawableFactory)
- {
- GLCapabilities glCaps = new GLCapabilities();
- gl4java.drawable.SunJDK13GLDrawableFactory sdf =
- (gl4java.drawable.SunJDK13GLDrawableFactory)df;
-
- gc = sdf.getGraphicsConfiguration(glCaps, device);
- } else {
- gc = device.getDefaultConfiguration();
- }
-
- mainFrame = new Frame(gc);
-
- gearsFullScreen applet = new gearsFullScreen();
- mainFrame.add(applet);
- applet.setSize(400,500);
- applet.init();
-
- if(perftest)
- {
- applet.canvas.setUseFpsSleep(false);
- applet.canvas.setUseRepaint(false);
- applet.canvas.setUseYield(false);
-
- System.out.println("useFpsSleep: "+
- applet.canvas.getUseFpsSleep());
- System.out.println("useRepaint: "+
- applet.canvas.getUseRepaint());
-
- System.out.println("useFpsSleep: "+
- applet.canvas.getUseFpsSleep());
- }
-
- applet.start();
-
- Dimension ps = applet.getPreferredSize();
- mainFrame.setBounds(-100,-100,99,99);
- mainFrame.setVisible(true);
- mainFrame.setVisible(false);
- mainFrame.setVisible(true);
- Insets is = mainFrame.getInsets();
- mainFrame.setBounds(0,0,
- ps.width+is.left+is.right,
- ps.height+is.top+is.bottom);
- mainFrame.setVisible(true);
- Thread.currentThread().sleep(10000, 0 );
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- device.setFullScreenWindow(null);
- }
-
- }
-
- // Methods required for the implementation of MouseListener
- public void mouseEntered( MouseEvent evt )
- {
- //System.out.println("mouse entered: ");
- super.mouseEntered(evt);
- }
-
- public void mouseExited( MouseEvent evt )
- {
- //System.out.println("mouse exit: ");
- super.mouseExited(evt);
- }
-
- public void mousePressed( MouseEvent evt )
- {
- //System.out.println("mouse pressed: ");
- super.mousePressed(evt);
- }
-
- public void mouseReleased( MouseEvent evt )
- {
- //System.out.println("mouse released: ");
- super.mouseReleased(evt);
- }
-
- public void mouseClicked( MouseEvent evt )
- {
- //System.out.println("mouse clicked: ");
- super.mouseClicked(evt);
-
- if ((evt.getModifiers() & evt.BUTTON2_MASK) != 0)
- {
- System.out.println("stopping applet now .. (after 1s, restart)");
- stop();
- try {
- Thread.sleep(1000);
- } catch (Exception e)
- { System.out.println("oops, somebody woke us up .."); }
- System.out.println("restarting applet now .. ");
- canvas.setVisible(true);
- start();
- }
- }
-}
+/** + * @(#) gearsFullScreen.java + * @(#) author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) + * + * This version is equal to Brian Paul's version 1.2 1999/10/21 + */ + +import java.applet.*; +import java.awt.*; +import java.awt.event.*; +import java.lang.*; +import java.util.*; +import java.io.*; +import gl4java.*; +import gl4java.drawable.*; +import gl4java.awt.GLAnimCanvas; +import gl4java.applet.SimpleGLAnimApplet1; + +public class gearsFullScreen extends SimpleGLAnimApplet1 + implements MouseListener +{ + + /* Initialize the applet */ + public void init() { + init(false); + } + + public void init(boolean showGL) { + super.init(); + + Dimension d = getSize(); + + GLCapabilities caps = new GLCapabilities(); + + canvas = + GLDrawableFactory.getFactory().createGLAnimCanvas(caps, d.width, d.height); + + gearRenderer gear = new gearRenderer(showGL); + canvas.addGLEventListener(gear); + + add("Center", canvas); + addMouseListener(this); + } + + public void destroy() { + removeMouseListener(this); + super.destroy(); + } + + + public static void main(String args[]) { + int i = 0; + String gljLib = null; + String glLib = null; + String gluLib = null; + boolean perftest=false; + + GLContext.gljNativeDebug = false; + GLContext.gljClassDebug = false; + GLContext.gljThreadDebug = false; + + while(args.length > i) { + if(args[i].equals("-perftest")) { + perftest=true; + } else if(args[i].equals("-gljLib")) { + i++; + if(args.length>i) + gljLib=args[i]; + } else if(args[i].equals("-glLib")) { + i++; + if(args.length>i) + glLib=args[i]; + } else if(args[i].equals("-gluLib")) { + i++; + if(args.length>i) + gluLib=args[i]; + } else { + System.out.println("illegal arg "+i+": "+args[i]); + } + i++; + } + + if(perftest) { + GLContext.gljNativeDebug = false; + GLContext.gljThreadDebug = false; + GLContext.gljClassDebug = false; + } + + if(perftest) + GLContext.gljClassDebug=true; + GLContext.doLoadNativeLibraries(gljLib, glLib, gluLib); + if(perftest) + GLContext.gljClassDebug=false; + + GraphicsEnvironment env = + GraphicsEnvironment.getLocalGraphicsEnvironment(); + final GraphicsDevice device = env.getDefaultScreenDevice(); + Frame mainFrame = null; + final DisplayMode origMode = device.getDisplayMode(); + DisplayMode newMode = null; + int initWidth = 400; + int initHeight = 300; + + System.out.println("isFullScreenSupported: " + + device.isFullScreenSupported()); + + if (device.isFullScreenSupported()) { + newMode = ScreenResSelector.showSelectionDialog(); + if (newMode != null) { + initWidth = newMode.getWidth(); + initHeight = newMode.getHeight(); + } + } + + try { + mainFrame = new Frame(); + gearsFullScreen applet = new gearsFullScreen(); + mainFrame.add(applet); + + if (device.isFullScreenSupported()) { + mainFrame.setUndecorated(true); + } + + applet.init(); + if(perftest) { + applet.canvas.setUseFpsSleep(false); + applet.canvas.setUseRepaint(false); + applet.canvas.setUseYield(false); + + System.out.println("useFpsSleep: "+ + applet.canvas.getUseFpsSleep()); + System.out.println("useRepaint: "+ + applet.canvas.getUseRepaint()); + + System.out.println("useFpsSleep: "+ + applet.canvas.getUseFpsSleep()); + } + + mainFrame.setSize(initWidth, initHeight); + mainFrame.show(); + mainFrame.setLocation(0, 0); + + if (device.isFullScreenSupported()) { + device.setFullScreenWindow(mainFrame); + if (device.isDisplayChangeSupported()) { + device.setDisplayMode(newMode); + } else { + newMode = null; + } + final DisplayMode tmpMode = newMode; + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + if (tmpMode != null) { + try { + device.setDisplayMode(origMode); + } catch (Exception e) { + } + } + device.setFullScreenWindow(null); + } + }); + } + + applet.start(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + device.setFullScreenWindow(null); + } + } + + // Methods required for the implementation of MouseListener + public void mouseEntered( MouseEvent evt ) { + //System.out.println("mouse entered: "); + super.mouseEntered(evt); + } + + public void mouseExited( MouseEvent evt ) { + //System.out.println("mouse exit: "); + super.mouseExited(evt); + } + + public void mousePressed( MouseEvent evt ) { + //System.out.println("mouse pressed: "); + super.mousePressed(evt); + } + + public void mouseReleased( MouseEvent evt ) { + //System.out.println("mouse released: "); + super.mouseReleased(evt); + } + + public void mouseClicked( MouseEvent evt ) { + //System.out.println("mouse clicked: "); + super.mouseClicked(evt); + + if ((evt.getModifiers() & evt.BUTTON2_MASK) != 0) { + System.out.println("stopping applet now .. (after 1s, restart)"); + stop(); + try { + Thread.sleep(1000); + } catch (Exception e) { + System.out.println("oops, somebody woke us up .."); } + System.out.println("restarting applet now .. "); + canvas.setVisible(true); + start(); + } + } +} diff --git a/demos/MiscDemos/makefile b/demos/MiscDemos/makefile index e7a1ae4..89d83c3 100644 --- a/demos/MiscDemos/makefile +++ b/demos/MiscDemos/makefile @@ -44,7 +44,8 @@ JAVA13_CLASSES = ${JAVA13_SOURCES:.java=.class}) ifdef JAVAC_14 JAVA14_SOURCES = \ - gearsFullScreen.java + gearsFullScreen.java \ + ScreenResSelector.java JAVA14_CLASSES = ${JAVA14_SOURCES:.java=.class}) HAVE_JAVAC_14 = 1 else @@ -53,7 +54,7 @@ else endif all: cleanup $(JAVA13_CLASSES) $(JAVA14_CLASSES) - + $(JAVA13_CLASSES) : $(JAVA13_SOURCES) $(JAVAC_13) -O -deprecation $^ | tee -a errors |