Java JFrame Size getFramesCurrentScreenSize(JFrame currentFrame)

Here you can find the source of getFramesCurrentScreenSize(JFrame currentFrame)

Description

get Frames Current Screen Size

License

Apache License

Declaration

public static Rectangle getFramesCurrentScreenSize(JFrame currentFrame) 

Method Source Code

//package com.java2s;
/*//w  ww  .j  av a2s .  c om
 *    Copyright 2016 Roche NimbleGen Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;

public class Main {
    public static Rectangle getFramesCurrentScreenSize(JFrame currentFrame) {
        Rectangle nonFullScreenBounds = currentFrame.getBounds();

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();

        boolean currentScreenFound = false;
        int index = 0;

        java.awt.Rectangle currentScreenSize = null;
        // figure out which screen the frame is currently in and get its max
        // size
        // and upper left corner
        while (!currentScreenFound && index < gs.length) {
            java.awt.Rectangle screenBounds = gs[index].getDefaultConfiguration().getBounds();

            int frameCenterX = (int) (nonFullScreenBounds.getX() + (nonFullScreenBounds.getWidth() / 2));
            int frameCenterY = (int) (nonFullScreenBounds.getY() + (nonFullScreenBounds.getHeight() / 2));
            int screenStartX = (int) (screenBounds.getX());
            int screenEndX = (int) (screenStartX + screenBounds.getWidth());
            int screenStartY = (int) (screenBounds.getY());
            int screenEndY = (int) (screenStartY + screenBounds.getHeight());
            Rectangle2D screen = new Rectangle2D.Double(screenStartX, screenStartY, (screenEndX - screenStartX + 1),
                    (screenEndY - screenStartY + 1));
            currentScreenFound = screen.contains(frameCenterX, frameCenterY);
            if (currentScreenFound) {
                currentScreenSize = screenBounds;
            }
            index++;
        }

        if (!currentScreenFound) {
            currentScreenSize = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
        }
        return currentScreenSize;
    }
}

Related

  1. adaptSize(JFrame frame, int preferredWidth, int preferredHeight)
  2. getFrameSize(Component c)
  3. getFrameSize(Component c)
  4. getFrameSize(Component c)
  5. isNormalSizeMode(JFrame window)