Java全屏截图

发布于 2025-01-08 19:08:02 字数 3114 浏览 0 评论 0原文

我正在开发一个游戏项目,并且编写了一些允许游戏全屏运行的基本代码。

我的问题是,当游戏处于全屏模式时,我无法按 Prnt Scrn 截屏!如果我尝试截取屏幕截图,它只会截取全屏游戏窗口后面的内容。有什么想法为什么这不起作用吗?

我在 Windows 7 上运行。这是说明我的问题的 SSCCE:

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();
    }
}

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光清浅 2025-01-15 19:08:03

问题仍然没有解决。总之,该问题可能是我的机器的图形设备/驱动程序固有的,因为其他用户无法重现我遇到的问题。因此,这个问题在原生Java中已经不值得追求了。

The problem remains unresolved. In conclusion, the problem is likely inherent in my machine's graphics devices/drivers since other users cannot reproduce the problem I am having. Therefore, this problem is no longer worth pursuing in native Java.

离鸿 2025-01-15 19:08:02

尝试 Alt-PrintScreen(捕获当前窗口)。这可能会在全屏独占模式下发挥作用。祝你好运:-)

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

赠我空喜 2025-01-15 19:08:02

不需要任何其他课程。当按下 PRINTSCREEN 时,将游戏渲染到 BufferedImage 上,并使用 ImageIO 保存它。

如何保存图像。
http://docs.oracle.com/javase/tutorial/ 2d/images/saveimage.html

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

非要怀念 2025-01-15 19:08:02

您可以尝试 java.awt.Robot:

    try
    {
        Robot pixelGrabber = new Robot ();
        java.awt.image.BufferedImage bi = pixelGrabber.createScreenCapture (new Rectangle (0, 0, 1024, 768));
    }
    catch (AWTException ex)
    {
        ex.printStackTrace ();
    }

您可以将 1024、768 调整为您的屏幕设置,然后寻找一种动态获取屏幕尺寸的方法。

You could try java.awt.Robot:

    try
    {
        Robot pixelGrabber = new Robot ();
        java.awt.image.BufferedImage bi = pixelGrabber.createScreenCapture (new Rectangle (0, 0, 1024, 768));
    }
    catch (AWTException ex)
    {
        ex.printStackTrace ();
    }

You would adjust 1024, 768 to your Screen settings, then look for a method to get the screen size dynamically.

好倦 2025-01-15 19:08:02

如果有效,请尝试 ctrl + alt + PrtScrn。

Try ctrl + alt + PrtScrn if it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文