@Override
    public void stateAttached(AppStateManager stateManager) {
        super.stateAttached(stateManager); //To change body of generated methods, choose Tools | Templates.

        if (settings == null) {
            settings = new AppSettings(true);
            logger.config("Using default settings.");
        } else {
            logger.config("Using given settings.");
        }

        // Attach VR environment to the application
        if (!environment.isInitialized()){
            environment.initialize();
        }

        if (environment.isInitialized()){
            environment.atttach(this, stateManager.getApplication());
        } else {
            logger.severe("Cannot attach VR environment to the VR app state as its not initialized.");
        }

        GraphicsDevice defDev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

        if( environment.isInVR() && !environment.compositorAllowed() ) {
            // "easy extended" mode
            // setup experimental JFrame on external 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);
                    settings.setVSync(true); // allow vsync on this display
                    stateManager.getApplication().setSettings(settings);
                    logger.config("Updated underlying application settings.");

                    //VRdev.setFullScreenWindow(VRwindow);
                    // make sure we are in the right display mode
                    if( VRdev.getDisplayMode().equals(useDM) == false ) {
                        VRdev.setDisplayMode(useDM);
                    }

                    return;
                } catch(Exception e) {
                    logger.log(Level.SEVERE, e.getMessage(), e);
                }
            } else {
                logger.config("Cannot access to external screen.");
            }
        } else {
            if (!environment.isInVR()){
              logger.config("Cannot switch to VR mode (VR disabled by user).");
            } else if (!environment.compositorAllowed()){
              logger.warning("Cannot switch to VR mode (VR not supported).");
            }
        }

        if( !environment.isInVR() ) {
            //FIXME: Handling GLFW workaround on MacOS
            boolean macOs = false;
            if (macOs) {
                // 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 {
            // use basic mirroring window, skip settings window
            settings.setSamples(1);
            settings.setWidth(xWin);
            settings.setHeight(yWin);
            settings.setBitsPerPixel(32);
            settings.setFrameRate(0);
            settings.setFrequency(environment.getVRHardware().getDisplayFrequency());
            settings.setFullscreen(false);
            settings.setVSync(false); // stop vsyncing on primary monitor!
            settings.setSwapBuffers(environment.isSwapBuffers());
        }

        // Updating application settings
        stateManager.getApplication().setSettings(settings);
        logger.config("Updated underlying application settings.");
    }
Set the {@link AppSettings settings} attached to this app state.
@param settings the {@link AppSettings settings} attached to this app state.
@see #getSettings()
jme3-vr/src/main/java/com/jme3/app/VRAppState.java