Search in sources :

Example 41 with GraphicsDevice

use of java.awt.GraphicsDevice in project gephi by gephi.

the class PreviewTopComponent method isRetina.

/**
     * Returns true if the default screen is in retina display (high dpi).
     *
     * @return true if retina, false otherwise
     */
protected static boolean isRetina() {
    boolean isRetina = false;
    try {
        GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        Field field = graphicsDevice.getClass().getDeclaredField("scale");
        if (field != null) {
            field.setAccessible(true);
            Object scale = field.get(graphicsDevice);
            if (scale instanceof Integer && ((Integer) scale).intValue() == 2) {
                isRetina = true;
            }
        }
    } catch (Exception e) {
    //Ignore
    }
    return isRetina;
}
Also used : Field(java.lang.reflect.Field) GraphicsDevice(java.awt.GraphicsDevice)

Example 42 with GraphicsDevice

use of java.awt.GraphicsDevice in project libgdx by libgdx.

the class LwjglApplicationConfiguration method getDesktopDisplayMode.

public static DisplayMode getDesktopDisplayMode() {
    GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = genv.getDefaultScreenDevice();
    java.awt.DisplayMode mode = device.getDisplayMode();
    return new LwjglApplicationConfigurationDisplayMode(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getBitDepth());
}
Also used : GraphicsDevice(java.awt.GraphicsDevice) GraphicsEnvironment(java.awt.GraphicsEnvironment)

Example 43 with GraphicsDevice

use of java.awt.GraphicsDevice in project jmonkeyengine by jMonkeyEngine.

the class JoglDisplay method applySettings.

protected void applySettings(AppSettings settings) {
    final boolean isDisplayModeModified;
    final GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    // Get the current display mode
    final DisplayMode previousDisplayMode = gd.getDisplayMode();
    // Handle full screen mode if requested.
    if (settings.isFullscreen()) {
        frame.setUndecorated(true);
        // Check if the full-screen mode is supported by the OS
        boolean isFullScreenSupported = gd.isFullScreenSupported();
        if (isFullScreenSupported) {
            gd.setFullScreenWindow(frame);
            // Check if display mode changes are supported by the OS
            if (gd.isDisplayChangeSupported()) {
                // Get all available display modes
                final DisplayMode[] displayModes = gd.getDisplayModes();
                DisplayMode multiBitsDepthSupportedDisplayMode = null;
                DisplayMode refreshRateUnknownDisplayMode = null;
                DisplayMode multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode = null;
                DisplayMode matchingDisplayMode = null;
                DisplayMode currentDisplayMode;
                // that matches exactly with your parameters
                for (int i = 0; i < displayModes.length && matchingDisplayMode == null; i++) {
                    currentDisplayMode = displayModes[i];
                    if (currentDisplayMode.getWidth() == settings.getWidth() && currentDisplayMode.getHeight() == settings.getHeight()) {
                        if (currentDisplayMode.getBitDepth() == settings.getBitsPerPixel()) {
                            if (currentDisplayMode.getRefreshRate() == settings.getFrequency()) {
                                matchingDisplayMode = currentDisplayMode;
                            } else if (currentDisplayMode.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN) {
                                refreshRateUnknownDisplayMode = currentDisplayMode;
                            }
                        } else if (currentDisplayMode.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI) {
                            if (currentDisplayMode.getRefreshRate() == settings.getFrequency()) {
                                multiBitsDepthSupportedDisplayMode = currentDisplayMode;
                            } else if (currentDisplayMode.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN) {
                                multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode = currentDisplayMode;
                            }
                        }
                    }
                }
                DisplayMode nextDisplayMode = null;
                if (matchingDisplayMode != null) {
                    nextDisplayMode = matchingDisplayMode;
                } else if (multiBitsDepthSupportedDisplayMode != null) {
                    nextDisplayMode = multiBitsDepthSupportedDisplayMode;
                } else if (refreshRateUnknownDisplayMode != null) {
                    nextDisplayMode = refreshRateUnknownDisplayMode;
                } else if (multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode != null) {
                    nextDisplayMode = multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode;
                } else {
                    isFullScreenSupported = false;
                }
                // with the input parameters, use it
                if (nextDisplayMode != null) {
                    gd.setDisplayMode(nextDisplayMode);
                    isDisplayModeModified = true;
                } else {
                    isDisplayModeModified = false;
                }
            } else {
                isDisplayModeModified = false;
                // Resize the canvas if the display mode cannot be changed
                // and the screen size is not equal to the canvas size
                final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                if (screenSize.width != settings.getWidth() || screenSize.height != settings.getHeight()) {
                    canvas.setSize(screenSize);
                }
            }
        } else {
            isDisplayModeModified = false;
        }
        // Software windowed full-screen mode
        if (!isFullScreenSupported) {
            final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            // Resize the canvas
            canvas.setSize(screenSize);
            // Resize the frame so that it occupies the whole screen
            frame.setSize(screenSize);
            // Set its location at the top left corner
            frame.setLocation(0, 0);
        }
    } else // Otherwise, center the window on the screen.
    {
        isDisplayModeModified = false;
        frame.pack();
        int x, y;
        x = (Toolkit.getDefaultToolkit().getScreenSize().width - settings.getWidth()) / 2;
        y = (Toolkit.getDefaultToolkit().getScreenSize().height - settings.getHeight()) / 2;
        frame.setLocation(x, y);
    }
    frame.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent evt) {
            // If required, restore the previous display mode
            if (isDisplayModeModified) {
                gd.setDisplayMode(previousDisplayMode);
            }
            // If required, get back to the windowed mode
            if (gd.getFullScreenWindow() == frame) {
                gd.setFullScreenWindow(null);
            }
            windowCloseRequest.set(true);
        }

        @Override
        public void windowActivated(WindowEvent evt) {
            active.set(true);
        }

        @Override
        public void windowDeactivated(WindowEvent evt) {
            active.set(false);
        }
    });
    logger.log(Level.FINE, "Selected display mode: {0}x{1}x{2} @{3}", new Object[] { gd.getDisplayMode().getWidth(), gd.getDisplayMode().getHeight(), gd.getDisplayMode().getBitDepth(), gd.getDisplayMode().getRefreshRate() });
}
Also used : DisplayMode(java.awt.DisplayMode) GraphicsDevice(java.awt.GraphicsDevice) WindowEvent(java.awt.event.WindowEvent) WindowAdapter(java.awt.event.WindowAdapter) Dimension(java.awt.Dimension)

Example 44 with GraphicsDevice

use of java.awt.GraphicsDevice in project jmonkeyengine by jMonkeyEngine.

the class VRApplication method start.

@Override
public void start() {
    logger.config("Starting application...");
    // set some default settings in-case
    // settings dialog is not shown
    boolean loadSettings = false;
    if (settings == null) {
        setSettings(new AppSettings(true));
        loadSettings = true;
    }
    GraphicsDevice defDev = null;
    try {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        defDev = ge.getDefaultScreenDevice();
    } catch (Throwable e1) {
        logger.log(Level.SEVERE, "Cannot access default screen device: " + e1.getMessage(), e1);
    }
    if (isInVR() && !compositorAllowed()) {
        logger.warning("VR Composition is not allowed.");
        // "easy extended" mode
        // TO-DO: JFrame was removed in LWJGL 3, need to use new GLFW library to pick "monitor" display of VR device
        // first, find the VR device
        GraphicsDevice VRdev = null;
        GraphicsDevice[] devs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
        // pick the display that isn't the default one
        for (GraphicsDevice gd : devs) {
            if (gd != defDev) {
                VRdev = gd;
                break;
            }
        }
        // did we get the VR device?
        if (VRdev != null) {
            // set properties for VR acceleration
            try {
                java.awt.DisplayMode useDM = null;
                int max = 0;
                for (java.awt.DisplayMode dm : VRdev.getDisplayModes()) {
                    int check = dm.getHeight() + dm.getWidth() + dm.getRefreshRate() + dm.getBitDepth();
                    if (check > max) {
                        max = check;
                        useDM = dm;
                    }
                }
                // create a window for the VR device
                settings.setWidth(useDM.getWidth());
                settings.setHeight(useDM.getHeight());
                settings.setBitsPerPixel(useDM.getBitDepth());
                settings.setFrequency(useDM.getRefreshRate());
                settings.setSwapBuffers(true);
                // allow vsync on this display
                settings.setVSync(true);
                setSettings(settings);
                // make sure we are in the right display mode
                if (VRdev.getDisplayMode().equals(useDM) == false) {
                    VRdev.setDisplayMode(useDM);
                }
                // make a blank cursor to hide it
                //BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
                //Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");                    
                //VRwindow.setCursor(blankCursor);
                //jmeCanvas.getCanvas().setCursor(blankCursor);
                //VRwindow.pack();
                //VRwindow.setVisible(true);
                //startCanvas();
                logger.config("Starting application [SUCCESS]");
                return;
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Error during application start: " + e.getMessage(), e);
            }
        }
    }
    if (!isInVR()) {
        logger.config("VR mode disabled.");
        // not in VR, show settings dialog
        if (Platform.get() != Platform.MACOSX) {
            if (!JmeSystem.showSettingsDialog(settings, loadSettings)) {
                logger.config("Starting application [SUCCESS]");
                return;
            }
        } else {
            // GLFW workaround on macs
            settings.setFrequency(defDev.getDisplayMode().getRefreshRate());
            settings.setDepthBits(24);
            settings.setVSync(true);
            // try and read resolution from file in local dir
            File resfile = new File("resolution.txt");
            if (resfile.exists()) {
                try {
                    BufferedReader br = new BufferedReader(new FileReader(resfile));
                    settings.setWidth(Integer.parseInt(br.readLine()));
                    settings.setHeight(Integer.parseInt(br.readLine()));
                    try {
                        settings.setFullscreen(br.readLine().toLowerCase(Locale.ENGLISH).contains("full"));
                    } catch (Exception e) {
                        settings.setFullscreen(false);
                    }
                    br.close();
                } catch (Exception e) {
                    settings.setWidth(1280);
                    settings.setHeight(720);
                }
            } else {
                settings.setWidth(1280);
                settings.setHeight(720);
                settings.setFullscreen(false);
            }
            settings.setResizable(false);
        }
        settings.setSwapBuffers(true);
    } else {
        logger.config("VR mode enabled.");
        // use basic mirroring window, skip settings window
        settings.setWidth(xWin);
        settings.setHeight(yWin);
        settings.setBitsPerPixel(24);
        // never sleep in main loop
        settings.setFrameRate(0);
        settings.setFrequency(VRhardware.getDisplayFrequency());
        settings.setFullscreen(false);
        // stop vsyncing on primary monitor!
        settings.setVSync(false);
        settings.setSwapBuffers(!disableSwapBuffers || VRhardware instanceof OSVR);
        settings.setTitle("Put Headset On Now: " + settings.getTitle());
        settings.setResizable(true);
    }
    if (forceDisableMSAA) {
        logger.config("Disabling multisampling.");
        // disable multisampling, which is more likely to break things than be useful
        settings.setSamples(1);
    }
    // set opengl mode
    if (tryOpenGL3) {
        logger.config("Using LWJGL OpenGL 3 renderer.");
        settings.setRenderer(AppSettings.LWJGL_OPENGL3);
    } else {
        logger.config("Using LWJGL OpenGL 2 renderer.");
        settings.setRenderer(AppSettings.LWJGL_OPENGL2);
    }
    setSettings(settings);
    start(JmeContext.Type.Display, false);
    // disable annoying warnings about GUI stuff being updated, which is normal behavior
    // for late GUI placement for VR purposes
    Logger.getLogger("com.jme3").setLevel(Level.SEVERE);
}
Also used : AppSettings(com.jme3.system.AppSettings) OSVR(com.jme3.input.vr.OSVR) GraphicsEnvironment(java.awt.GraphicsEnvironment) CullHint(com.jme3.scene.Spatial.CullHint) MalformedURLException(java.net.MalformedURLException) GraphicsDevice(java.awt.GraphicsDevice) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 45 with GraphicsDevice

use of java.awt.GraphicsDevice in project screenbird by adamhub.

the class ScreenUtilTest method testIsDualScreen.

/**
     * Test of isMultipleScreen method, of class ScreenUtil.
     */
@Test
public void testIsDualScreen() {
    log("isDualScreen");
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] screens = ge.getScreenDevices();
    boolean expResult = screens.length > 1;
    boolean result = ScreenUtil.isMultipleScreen();
    assertEquals(expResult, result);
}
Also used : GraphicsDevice(java.awt.GraphicsDevice) GraphicsEnvironment(java.awt.GraphicsEnvironment) Test(org.junit.Test)

Aggregations

GraphicsDevice (java.awt.GraphicsDevice)80 GraphicsEnvironment (java.awt.GraphicsEnvironment)46 GraphicsConfiguration (java.awt.GraphicsConfiguration)25 Point (java.awt.Point)17 Dimension (java.awt.Dimension)15 Frame (java.awt.Frame)13 DisplayMode (java.awt.DisplayMode)12 Rectangle (java.awt.Rectangle)11 Graphics (java.awt.Graphics)7 Insets (java.awt.Insets)7 Toolkit (java.awt.Toolkit)7 BufferedImage (java.awt.image.BufferedImage)7 HeadlessException (java.awt.HeadlessException)5 Window (java.awt.Window)4 JFrame (javax.swing.JFrame)4 Test (org.junit.Test)4 MouseAdapter (java.awt.event.MouseAdapter)3 WindowAdapter (java.awt.event.WindowAdapter)3 WindowEvent (java.awt.event.WindowEvent)3 VolatileImage (java.awt.image.VolatileImage)3