Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java fullscreen screenshots

I'm working on a game project and I've written some basic code that allows the game to run in fullscreen.

My problem is that while the game is in fullscreen mode, I can't press Prnt Scrn to take screenshots! If I try to take a screenshot, it just screenshots whatever is behind the fullscreen game window. Any ideas why this isn't working?

I'm running on Windows 7. Here is an SSCCE illustrating my problem:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FullscreenScreenShotSSCCE extends JFrame
{
    private JPanel screenP;

    private GraphicsDevice grDev;

    /**
    *   Constructor
    *   Preconditions: None.
    *   Postconditions: The window for the SSCCE is created.
    **/

    public FullscreenScreenShotSSCCE()
    {
        super("Fullscreen Prnt Scrn problem SSCCE");
        int screenX = 640;  
        int screenY = 480;
        this.setSize(screenX,screenY);

        // set up resolution change mode

        grDev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); // obtains your graphics device

        // setup the game for full-screen if requested.

        System.out.println("Trying to start program in Fullscreen mode.");

        if(grDev.isFullScreenSupported()) // makes sure fullscreen is supported before doing anything.
        {
            System.out.println("FullScreen is supported");
            this.setUndecorated(true);
            DisplayMode resChangeMode = new DisplayMode(640,480,32,DisplayMode.REFRESH_RATE_UNKNOWN); // create new DisplayMode with different resolution.

            try
            {
                grDev.setFullScreenWindow(this); // set fullscreen mode on. Otherwise this won't work
                grDev.setDisplayMode(resChangeMode); // change DisplayMode to our new resolution.
                System.out.println("Change resolution: Success!");
            }
            catch(Exception e)
            {
                System.out.println("Change resolution: FAIL!");
            }
        }
        this.setExtendedState(MAXIMIZED_BOTH);

        // instantiate main panel

        screenP = new SSCCEPanel();
        this.add(screenP);

        // finishing touches on Game window

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        System.out.println("Game Window successfully created!!!");
    }


    public static void main(String[] args)
    {
        FullscreenScreenShotSSCCE gui = new FullscreenScreenShotSSCCE();        
    }
}




/**
*   SSCCEPanel is the JPanel that manages the example's timer, painting, and logic. 
**/

class SSCCEPanel extends JPanel
{
    private Timer timer;
    public double prevFPS;
    boolean timerReady;

    // The SoundPlayer object is used by the example to play the sounds.

    public SSCCEPanel()
    {
        super(true);
    }

    /**
    *   repaints the SSCCE.
    *   This just shows the current FPS and the number of sounds currently playing.
    **/

    public void paintComponent(Graphics g)
    {
            super.paintComponent(g);

            Graphics2D g2D = (Graphics2D) g;
            g2D.setColor(new Color(0x000000));
            g2D.drawString("Java fullscreen!", 20,20);
            g2D.drawString("Try to take a screenshot!", 20,40);
            g.dispose();
    }
}
like image 946
Cazra Avatar asked Nov 05 '22 05:11

Cazra


2 Answers

Try Alt-PrintScreen (captures the current window). That may do the trick in Full Screen Exclusive Mode. Good luck:-)

like image 76
John Avatar answered Nov 09 '22 11:11

John


No need of any other classes. When pressed PRINTSCREEN, render your game on to a BufferedImage and save that using ImageIO.

How to save images. http://docs.oracle.com/javase/tutorial/2d/images/saveimage.html

like image 45
Sri Harsha Chilakapati Avatar answered Nov 09 '22 13:11

Sri Harsha Chilakapati