summaryrefslogtreecommitdiffstats
path: root/src/demos/fullscreen/GearsFullscreen2.java
blob: 9b08cba6ba9b2e5025beeb405393ddb92e530992 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package demos.fullscreen;

import demos.gears.Gears;
import demos.util.ScreenResSelector;
import java.awt.BorderLayout;
import java.awt.DisplayMode;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.util.Animator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.ToolTipManager;


/**
 * GearsFullscreen2.java <BR>
 * author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P>
 *
 * This version is equal to Brian Paul's version 1.2 1999/10/21 <P>
 *
 * Illustrates more complex usage of GLCanvas in full-screen mode. On
 * Windows this demo should be run with the system property
 * -Dsun.java2d.noddraw=true specified to prevent Java2D from using
 * DirectDraw, which is incompatible with OpenGL at the driver level.
 */

public class GearsFullscreen2 {
  private GraphicsDevice dev;
  private DisplayMode origMode;
  private boolean fullScreen;
  private JFrame frame;
  private Animator animator;
  private int initWidth = 300;
  private int initHeight = 300;

  public static void main(String[] args) {
    new GearsFullscreen2().run(args);
  }

  public void run(String[] args) {
    dev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    origMode = dev.getDisplayMode();
    DisplayMode newMode = null;

    if (dev.isFullScreenSupported()) {
      newMode = ScreenResSelector.showSelectionDialog();
      if (newMode != null) {
        initWidth = newMode.getWidth();
        initHeight = newMode.getHeight();
      }
    } else {
      System.err.println("NOTE: full-screen mode not supported; running in window instead");
    }

    frame = new JFrame("Gear Demo");
    if (newMode != null) {
      frame.setUndecorated(true);
    }
    GLCanvas canvas = new GLCanvas();

    canvas.addGLEventListener(new Gears());
    canvas.addGLEventListener(new FullscreenWorkaround(initWidth, initHeight));
    frame.getContentPane().setLayout(new BorderLayout());

    ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);

    JButton button = new JButton("West");
    button.setToolTipText("West ToolTip");
    frame.getContentPane().add(button, BorderLayout.WEST);

    button = new JButton("East");
    button.setToolTipText("East ToolTip");
    frame.getContentPane().add(button, BorderLayout.EAST);

    button = new JButton("North");
    button.setToolTipText("North ToolTip");
    frame.getContentPane().add(button, BorderLayout.NORTH);

    button = new JButton("South");
    button.setToolTipText("South ToolTip");
    frame.getContentPane().add(button, BorderLayout.SOUTH);

    frame.getContentPane().add(canvas, BorderLayout.CENTER);
    frame.setSize(initWidth, initHeight);
    animator = new Animator(canvas);
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          runExit();
        }
      });
    frame.setVisible(true);

    if (dev.isFullScreenSupported() && (newMode != null)) {
      dev.setFullScreenWindow(frame);
      if (dev.isDisplayChangeSupported()) {
        dev.setDisplayMode(newMode);
        fullScreen = true;
      } else {
        // Not much point in having a full-screen window in this case
        dev.setFullScreenWindow(null);
        final Frame f2 = frame;
        try {
          EventQueue.invokeAndWait(new Runnable() {
              public void run() {
                f2.setVisible(false);
                f2.setUndecorated(false);
                f2.setVisible(true);
                f2.setSize(initWidth, initHeight);
              }
            });
        } catch (Exception e) {
          e.printStackTrace();
        }
        System.err.println("NOTE: was not able to change display mode; full-screen disabled");
      }
    }

    animator.start();
  }

  public void runExit() {
    // Run this on another thread than the AWT event queue to
    // make sure the call to Animator.stop() completes before
    // exiting
    new Thread(new Runnable() {
        public void run() {
          animator.stop();
          try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                  if (fullScreen) {
                    try {
                      dev.setDisplayMode(origMode);
                    } catch (Exception e1) {
                    }
                    try {
                      dev.setFullScreenWindow(null);
                    } catch (Exception e2) {
                    }
                    fullScreen = false;
                  }
                }
              });
          } catch (Exception e) {
            e.printStackTrace();
          }
          System.exit(0);
        }
      }).start();
  }
}