Java Code Examples for java.awt.Window#setBounds()

The following examples show how to use java.awt.Window#setBounds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocationByPlatformTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {

        Window window = new Window(null);
        window.setSize(100, 100);
        window.setLocationByPlatform(true);

        if (!window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not set");
        }

        window.setLocation(10, 10);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setBounds(20, 20, 50, 50);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(false);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(true);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.dispose();
    }
 
Example 2
Source File: LocationByPlatformTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {

        Window window = new Window(null);
        window.setSize(100, 100);
        window.setLocationByPlatform(true);

        if (!window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not set");
        }

        window.setLocation(10, 10);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setBounds(20, 20, 50, 50);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(false);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(true);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.dispose();
    }
 
Example 3
Source File: RefineryUtilities.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Positions the specified frame at a relative position in the screen, 
 * where 50% is considered to be the center of the screen.
 *
 * @param frame  the frame.
 * @param horizontalPercent  the relative horizontal position of the frame 
 *     (0.0 to 1.0, where 0.5 is the center of the screen).
 * @param verticalPercent  the relative vertical position of the frame 
 *     (0.0 to 1.0, where 0.5 is the center of the screen).
 */
public static void positionFrameOnScreen(Window frame,
                                         double horizontalPercent,
                                         double verticalPercent) {

    Rectangle s = getMaximumWindowBounds();
    Dimension f = frame.getSize();
    int w = Math.max(s.width - f.width, 0);
    int h = Math.max(s.height - f.height, 0);
    int x = (int) (horizontalPercent * w) + s.x;
    int y = (int) (verticalPercent * h) + s.y;
    frame.setBounds(x, y, f.width, f.height);

}
 
Example 4
Source File: DnDSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Window createDragWindow( Image dragImage, Rectangle bounds ) {
    Window w = new Window( SwingUtilities.windowForComponent(sourceRow) );
    w.add(new JLabel(ImageUtilities.image2Icon(dragImage)));
    w.setBounds(bounds);
    w.setVisible(true);
    NativeWindowSystem nws = NativeWindowSystem.getDefault();
    if( nws.isUndecoratedWindowAlphaSupported() ) {
        nws.setWindowAlpha(w, 0.7f);
    }
    return w;
}
 
Example 5
Source File: Win32GraphicsDevice.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void setDisplayMode(DisplayMode dm) {
    if (!isDisplayChangeSupported()) {
        super.setDisplayMode(dm);
        return;
    }
    if (dm == null || (dm = getMatchingDisplayMode(dm)) == null) {
        throw new IllegalArgumentException("Invalid display mode");
    }
    if (getDisplayMode().equals(dm)) {
        return;
    }
    Window w = getFullScreenWindow();
    if (w != null) {
        WWindowPeer peer = (WWindowPeer)w.getPeer();
        configDisplayMode(screen, peer, dm.getWidth(), dm.getHeight(),
            dm.getBitDepth(), dm.getRefreshRate());
        // resize the fullscreen window to the dimensions of the new
        // display mode
        Rectangle screenBounds = getDefaultConfiguration().getBounds();
        w.setBounds(screenBounds.x, screenBounds.y,
                    dm.getWidth(), dm.getHeight());
        // Note: no call to replaceSurfaceData is required here since
        // replacement will be caused by an upcoming display change event
    } else {
        throw new IllegalStateException("Must be in fullscreen mode " +
                                        "in order to set display mode");
    }
}
 
Example 6
Source File: Win32GraphicsDevice.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void setDisplayMode(DisplayMode dm) {
    if (!isDisplayChangeSupported()) {
        super.setDisplayMode(dm);
        return;
    }
    if (dm == null || (dm = getMatchingDisplayMode(dm)) == null) {
        throw new IllegalArgumentException("Invalid display mode");
    }
    if (getDisplayMode().equals(dm)) {
        return;
    }
    Window w = getFullScreenWindow();
    if (w != null) {
        WWindowPeer peer = (WWindowPeer)w.getPeer();
        configDisplayMode(screen, peer, dm.getWidth(), dm.getHeight(),
            dm.getBitDepth(), dm.getRefreshRate());
        // resize the fullscreen window to the dimensions of the new
        // display mode
        Rectangle screenBounds = getDefaultConfiguration().getBounds();
        w.setBounds(screenBounds.x, screenBounds.y,
                    dm.getWidth(), dm.getHeight());
        // Note: no call to replaceSurfaceData is required here since
        // replacement will be caused by an upcoming display change event
    } else {
        throw new IllegalStateException("Must be in fullscreen mode " +
                                        "in order to set display mode");
    }
}
 
Example 7
Source File: Win32GraphicsDevice.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void setDisplayMode(DisplayMode dm) {
    if (!isDisplayChangeSupported()) {
        super.setDisplayMode(dm);
        return;
    }
    if (dm == null || (dm = getMatchingDisplayMode(dm)) == null) {
        throw new IllegalArgumentException("Invalid display mode");
    }
    if (getDisplayMode().equals(dm)) {
        return;
    }
    Window w = getFullScreenWindow();
    if (w != null) {
        WWindowPeer peer = (WWindowPeer)w.getPeer();
        configDisplayMode(screen, peer, dm.getWidth(), dm.getHeight(),
            dm.getBitDepth(), dm.getRefreshRate());
        // resize the fullscreen window to the dimensions of the new
        // display mode
        Rectangle screenBounds = getDefaultConfiguration().getBounds();
        w.setBounds(screenBounds.x, screenBounds.y,
                    dm.getWidth(), dm.getHeight());
        // Note: no call to replaceSurfaceData is required here since
        // replacement will be caused by an upcoming display change event
    } else {
        throw new IllegalStateException("Must be in fullscreen mode " +
                                        "in order to set display mode");
    }
}
 
Example 8
Source File: Win32GraphicsDevice.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void setDisplayMode(DisplayMode dm) {
    if (!isDisplayChangeSupported()) {
        super.setDisplayMode(dm);
        return;
    }
    if (dm == null || (dm = getMatchingDisplayMode(dm)) == null) {
        throw new IllegalArgumentException("Invalid display mode");
    }
    if (getDisplayMode().equals(dm)) {
        return;
    }
    Window w = getFullScreenWindow();
    if (w != null) {
        WWindowPeer peer = (WWindowPeer)w.getPeer();
        configDisplayMode(screen, peer, dm.getWidth(), dm.getHeight(),
            dm.getBitDepth(), dm.getRefreshRate());
        // resize the fullscreen window to the dimensions of the new
        // display mode
        Rectangle screenBounds = getDefaultConfiguration().getBounds();
        w.setBounds(screenBounds.x, screenBounds.y,
                    dm.getWidth(), dm.getHeight());
        // Note: no call to replaceSurfaceData is required here since
        // replacement will be caused by an upcoming display change event
    } else {
        throw new IllegalStateException("Must be in fullscreen mode " +
                                        "in order to set display mode");
    }
}
 
Example 9
Source File: Win32GraphicsDevice.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void setDisplayMode(DisplayMode dm) {
    if (!isDisplayChangeSupported()) {
        super.setDisplayMode(dm);
        return;
    }
    if (dm == null || (dm = getMatchingDisplayMode(dm)) == null) {
        throw new IllegalArgumentException("Invalid display mode");
    }
    if (getDisplayMode().equals(dm)) {
        return;
    }
    Window w = getFullScreenWindow();
    if (w != null) {
        WWindowPeer peer = (WWindowPeer)w.getPeer();
        configDisplayMode(screen, peer, dm.getWidth(), dm.getHeight(),
            dm.getBitDepth(), dm.getRefreshRate());
        // resize the fullscreen window to the dimensions of the new
        // display mode
        Rectangle screenBounds = getDefaultConfiguration().getBounds();
        w.setBounds(screenBounds.x, screenBounds.y,
                    dm.getWidth(), dm.getHeight());
        // Note: no call to replaceSurfaceData is required here since
        // replacement will be caused by an upcoming display change event
    } else {
        throw new IllegalStateException("Must be in fullscreen mode " +
                                        "in order to set display mode");
    }
}
 
Example 10
Source File: SwingUtil.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Positions the specified frame at a relative position in the screen, where 50% is considered to be the center of the
 * screen.
 *
 * @param frame
 *          the frame.
 * @param horizontalPercent
 *          the relative horizontal position of the frame (0.0 to 1.0, where 0.5 is the center of the screen).
 * @param verticalPercent
 *          the relative vertical position of the frame (0.0 to 1.0, where 0.5 is the center of the screen).
 */
public static void positionFrameOnScreen( final Window frame, final double horizontalPercent,
    final double verticalPercent ) {

  final Rectangle s = frame.getGraphicsConfiguration().getBounds();
  final Dimension f = frame.getSize();

  final int spaceOnX = Math.max( s.width - f.width, 0 );
  final int spaceOnY = Math.max( s.height - f.height, 0 );
  final int x = (int) ( horizontalPercent * spaceOnX ) + s.x;
  final int y = (int) ( verticalPercent * spaceOnY ) + s.y;
  frame.setBounds( x, y, f.width, f.height );
  frame.setBounds( s.intersection( frame.getBounds() ) );
}
 
Example 11
Source File: Win32GraphicsDevice.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void setDisplayMode(DisplayMode dm) {
    if (!isDisplayChangeSupported()) {
        super.setDisplayMode(dm);
        return;
    }
    if (dm == null || (dm = getMatchingDisplayMode(dm)) == null) {
        throw new IllegalArgumentException("Invalid display mode");
    }
    if (getDisplayMode().equals(dm)) {
        return;
    }
    Window w = getFullScreenWindow();
    if (w != null) {
        WWindowPeer peer = (WWindowPeer)w.getPeer();
        configDisplayMode(screen, peer, dm.getWidth(), dm.getHeight(),
            dm.getBitDepth(), dm.getRefreshRate());
        // resize the fullscreen window to the dimensions of the new
        // display mode
        Rectangle screenBounds = getDefaultConfiguration().getBounds();
        w.setBounds(screenBounds.x, screenBounds.y,
                    dm.getWidth(), dm.getHeight());
        // Note: no call to replaceSurfaceData is required here since
        // replacement will be caused by an upcoming display change event
    } else {
        throw new IllegalStateException("Must be in fullscreen mode " +
                                        "in order to set display mode");
    }
}
 
Example 12
Source File: LocationByPlatformTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {

        Window window = new Window(null);
        window.setSize(100, 100);
        window.setLocationByPlatform(true);

        if (!window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not set");
        }

        window.setLocation(10, 10);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setBounds(20, 20, 50, 50);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(false);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(true);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.dispose();
    }
 
Example 13
Source File: UserPreferences.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Restores the window size, position and state if possible. Tracks the
 * window size, position and state.
 * 
 * @param window
 */
public static void track(Window window) {
  Preferences prefs = node().node("Windows");

  String bounds = prefs.get(window.getName() + ".bounds", null);
  if (bounds != null) {
    Rectangle rect =
      (Rectangle)ConverterRegistry.instance().convert(
        Rectangle.class,
        bounds);
    window.setBounds(rect);
  }

  window.addComponentListener(windowDimension);
}
 
Example 14
Source File: Win32GraphicsDevice.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void setDisplayMode(DisplayMode dm) {
    if (!isDisplayChangeSupported()) {
        super.setDisplayMode(dm);
        return;
    }
    if (dm == null || (dm = getMatchingDisplayMode(dm)) == null) {
        throw new IllegalArgumentException("Invalid display mode");
    }
    if (getDisplayMode().equals(dm)) {
        return;
    }
    Window w = getFullScreenWindow();
    if (w != null) {
        WWindowPeer peer = (WWindowPeer)w.getPeer();
        configDisplayMode(screen, peer, dm.getWidth(), dm.getHeight(),
            dm.getBitDepth(), dm.getRefreshRate());
        // resize the fullscreen window to the dimensions of the new
        // display mode
        Rectangle screenBounds = getDefaultConfiguration().getBounds();
        w.setBounds(screenBounds.x, screenBounds.y,
                    dm.getWidth(), dm.getHeight());
        // Note: no call to replaceSurfaceData is required here since
        // replacement will be caused by an upcoming display change event
    } else {
        throw new IllegalStateException("Must be in fullscreen mode " +
                                        "in order to set display mode");
    }
}
 
Example 15
Source File: LocationByPlatformTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {

        Window window = new Window(null);
        window.setSize(100, 100);
        window.setLocationByPlatform(true);

        if (!window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not set");
        }

        window.setLocation(10, 10);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setBounds(20, 20, 50, 50);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(false);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.setLocationByPlatform(true);
        window.setVisible(true);

        if (window.isLocationByPlatform()) {
            throw new RuntimeException("Location by platform is not cleared");
        }

        window.dispose();
    }
 
Example 16
Source File: X11GraphicsDevice.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public synchronized void setDisplayMode(DisplayMode dm) {
    if (!isDisplayChangeSupported()) {
        super.setDisplayMode(dm);
        return;
    }
    Window w = getFullScreenWindow();
    if (w == null) {
        throw new IllegalStateException("Must be in fullscreen mode " +
                                        "in order to set display mode");
    }
    if (getDisplayMode().equals(dm)) {
        return;
    }
    if (dm == null ||
        (dm = getMatchingDisplayMode(dm)) == null)
    {
        throw new IllegalArgumentException("Invalid display mode");
    }

    if (!shutdownHookRegistered) {
        // register a shutdown hook so that we return to the
        // original DisplayMode when the VM exits (if the application
        // is already in the original DisplayMode at that time, this
        // hook will have no effect)
        shutdownHookRegistered = true;
        PrivilegedAction<Void> a = () -> {
            ThreadGroup rootTG = ThreadGroupUtils.getRootThreadGroup();
            Runnable r = () -> {
                Window old = getFullScreenWindow();
                if (old != null) {
                    exitFullScreenExclusive(old);
                    if (isDisplayChangeSupported()) {
                        setDisplayMode(origDisplayMode);
                    }
                }
            };
            Thread t = new Thread(rootTG, r,"Display-Change-Shutdown-Thread-"+screen);
            t.setContextClassLoader(null);
            Runtime.getRuntime().addShutdownHook(t);
            return null;
        };
        AccessController.doPrivileged(a);
    }

    // switch to the new DisplayMode
    configDisplayMode(screen,
                      dm.getWidth(), dm.getHeight(),
                      dm.getRefreshRate());

    // update bounds of the fullscreen window
    w.setBounds(0, 0, dm.getWidth(), dm.getHeight());

    // configDisplayMode() is synchronous, so the display change will be
    // complete by the time we get here (and it is therefore safe to call
    // displayChanged() now)
    ((X11GraphicsEnvironment)
     GraphicsEnvironment.getLocalGraphicsEnvironment()).displayChanged();
}
 
Example 17
Source File: LizziePane.java    From lizzie with GNU General Public License v3.0 4 votes vote down vote up
public void mouseDragged(MouseEvent e) {
  Window w = (Window) e.getSource();
  Point pt = e.getPoint();

  if (isMovingWindow) {
    Point eventLocationOnScreen = e.getLocationOnScreen();
    w.setLocation(eventLocationOnScreen.x - dragOffsetX, eventLocationOnScreen.y - dragOffsetY);
  } else if (dragCursor != 0) {
    Rectangle r = w.getBounds();
    Rectangle startBounds = new Rectangle(r);
    Dimension min = w.getMinimumSize();

    switch (dragCursor) {
      case Cursor.E_RESIZE_CURSOR:
        adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0);
        break;
      case Cursor.S_RESIZE_CURSOR:
        adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height);
        break;
      case Cursor.N_RESIZE_CURSOR:
        adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY));
        break;
      case Cursor.W_RESIZE_CURSOR:
        adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0);
        break;
      case Cursor.NE_RESIZE_CURSOR:
        adjust(
            r,
            min,
            0,
            pt.y - dragOffsetY,
            pt.x + (dragWidth - dragOffsetX) - r.width,
            -(pt.y - dragOffsetY));
        break;
      case Cursor.SE_RESIZE_CURSOR:
        adjust(
            r,
            min,
            0,
            0,
            pt.x + (dragWidth - dragOffsetX) - r.width,
            pt.y + (dragHeight - dragOffsetY) - r.height);
        break;
      case Cursor.NW_RESIZE_CURSOR:
        adjust(
            r,
            min,
            pt.x - dragOffsetX,
            pt.y - dragOffsetY,
            -(pt.x - dragOffsetX),
            -(pt.y - dragOffsetY));
        break;
      case Cursor.SW_RESIZE_CURSOR:
        adjust(
            r,
            min,
            pt.x - dragOffsetX,
            0,
            -(pt.x - dragOffsetX),
            pt.y + (dragHeight - dragOffsetY) - r.height);
        break;
      default:
        break;
    }
    if (!r.equals(startBounds)) {
      w.setBounds(r);
      // Defer repaint/validate on mouseReleased unless dynamic
      // layout is active.
      if (Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {
        w.validate();
        getRootPane().repaint();
      }
    }
  }
}
 
Example 18
Source File: X11GraphicsDevice.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public synchronized void setDisplayMode(DisplayMode dm) {
    if (!isDisplayChangeSupported()) {
        super.setDisplayMode(dm);
        return;
    }
    Window w = getFullScreenWindow();
    if (w == null) {
        throw new IllegalStateException("Must be in fullscreen mode " +
                                        "in order to set display mode");
    }
    if (getDisplayMode().equals(dm)) {
        return;
    }
    if (dm == null ||
        (dm = getMatchingDisplayMode(dm)) == null)
    {
        throw new IllegalArgumentException("Invalid display mode");
    }

    if (!shutdownHookRegistered) {
        // register a shutdown hook so that we return to the
        // original DisplayMode when the VM exits (if the application
        // is already in the original DisplayMode at that time, this
        // hook will have no effect)
        shutdownHookRegistered = true;
        PrivilegedAction<Void> a = () -> {
            ThreadGroup rootTG = ThreadGroupUtils.getRootThreadGroup();
            Runnable r = () -> {
                Window old = getFullScreenWindow();
                if (old != null) {
                    exitFullScreenExclusive(old);
                    if (isDisplayChangeSupported()) {
                        setDisplayMode(origDisplayMode);
                    }
                }
            };
            Thread t = new Thread(rootTG, r,"Display-Change-Shutdown-Thread-"+screen);
            t.setContextClassLoader(null);
            Runtime.getRuntime().addShutdownHook(t);
            return null;
        };
        AccessController.doPrivileged(a);
    }

    // switch to the new DisplayMode
    configDisplayMode(screen,
                      dm.getWidth(), dm.getHeight(),
                      dm.getRefreshRate());

    // update bounds of the fullscreen window
    w.setBounds(0, 0, dm.getWidth(), dm.getHeight());

    // configDisplayMode() is synchronous, so the display change will be
    // complete by the time we get here (and it is therefore safe to call
    // displayChanged() now)
    ((X11GraphicsEnvironment)
     GraphicsEnvironment.getLocalGraphicsEnvironment()).displayChanged();
}
 
Example 19
Source File: X11GraphicsDevice.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public synchronized void setDisplayMode(DisplayMode dm) {
    if (!isDisplayChangeSupported()) {
        super.setDisplayMode(dm);
        return;
    }
    Window w = getFullScreenWindow();
    if (w == null) {
        throw new IllegalStateException("Must be in fullscreen mode " +
                                        "in order to set display mode");
    }
    if (getDisplayMode().equals(dm)) {
        return;
    }
    if (dm == null ||
        (dm = getMatchingDisplayMode(dm)) == null)
    {
        throw new IllegalArgumentException("Invalid display mode");
    }

    if (!shutdownHookRegistered) {
        // register a shutdown hook so that we return to the
        // original DisplayMode when the VM exits (if the application
        // is already in the original DisplayMode at that time, this
        // hook will have no effect)
        shutdownHookRegistered = true;
        PrivilegedAction<Void> a = () -> {
            ThreadGroup rootTG = ThreadGroupUtils.getRootThreadGroup();
            Runnable r = () -> {
                Window old = getFullScreenWindow();
                if (old != null) {
                    exitFullScreenExclusive(old);
                    setDisplayMode(origDisplayMode);
                }
            };
            Thread t = new Thread(rootTG, r,"Display-Change-Shutdown-Thread-"+screen);
            t.setContextClassLoader(null);
            Runtime.getRuntime().addShutdownHook(t);
            return null;
        };
        AccessController.doPrivileged(a);
    }

    // switch to the new DisplayMode
    configDisplayMode(screen,
                      dm.getWidth(), dm.getHeight(),
                      dm.getRefreshRate());

    // update bounds of the fullscreen window
    w.setBounds(0, 0, dm.getWidth(), dm.getHeight());

    // configDisplayMode() is synchronous, so the display change will be
    // complete by the time we get here (and it is therefore safe to call
    // displayChanged() now)
    ((X11GraphicsEnvironment)
     GraphicsEnvironment.getLocalGraphicsEnvironment()).displayChanged();
}
 
Example 20
Source File: X11GraphicsDevice.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public synchronized void setDisplayMode(DisplayMode dm) {
    if (!isDisplayChangeSupported()) {
        super.setDisplayMode(dm);
        return;
    }
    Window w = getFullScreenWindow();
    if (w == null) {
        throw new IllegalStateException("Must be in fullscreen mode " +
                                        "in order to set display mode");
    }
    if (getDisplayMode().equals(dm)) {
        return;
    }
    if (dm == null ||
        (dm = getMatchingDisplayMode(dm)) == null)
    {
        throw new IllegalArgumentException("Invalid display mode");
    }

    if (!shutdownHookRegistered) {
        // register a shutdown hook so that we return to the
        // original DisplayMode when the VM exits (if the application
        // is already in the original DisplayMode at that time, this
        // hook will have no effect)
        shutdownHookRegistered = true;
        PrivilegedAction<Void> a = () -> {
            ThreadGroup rootTG = ThreadGroupUtils.getRootThreadGroup();
            Runnable r = () -> {
                Window old = getFullScreenWindow();
                if (old != null) {
                    exitFullScreenExclusive(old);
                    if (isDisplayChangeSupported()) {
                        setDisplayMode(origDisplayMode);
                    }
                }
            };
            Thread t = new Thread(rootTG, r,"Display-Change-Shutdown-Thread-"+screen);
            t.setContextClassLoader(null);
            Runtime.getRuntime().addShutdownHook(t);
            return null;
        };
        AccessController.doPrivileged(a);
    }

    // switch to the new DisplayMode
    configDisplayMode(screen,
                      dm.getWidth(), dm.getHeight(),
                      dm.getRefreshRate());

    // update bounds of the fullscreen window
    w.setBounds(0, 0, dm.getWidth(), dm.getHeight());

    // configDisplayMode() is synchronous, so the display change will be
    // complete by the time we get here (and it is therefore safe to call
    // displayChanged() now)
    ((X11GraphicsEnvironment)
     GraphicsEnvironment.getLocalGraphicsEnvironment()).displayChanged();
}