Screenshots in Java Full Screen

I am working on a game project, and I wrote a basic code that allows the game to work in full screen mode.

My problem is that while the game is in full screen mode, I can’t click Prnt Scrn to take screenshots! If I try to take a screenshot, these are just screenshots that are behind the full-screen game window. Any ideas why this is not working?

I start on Windows 7. Here is 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 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(); } } 
+4
source share
5 answers

The problem remains unresolved. In conclusion, the problem is probably inherent in my machine graphics devices / drivers, as other users cannot reproduce the problem I am experiencing. Therefore, this problem is no longer worth looking for in native Java.

0
source

Try Alt-PrintScreen (captures the current window). This can do the trick in full screen. Good luck :-)

+1
source

No need for any other classes. When you press PRINTSCREEN draw your game on a BufferedImage and save it using ImageIO .

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

+1
source

Try ctrl + alt + PrtScrn if it works.

0
source

You can 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 should set 1024, 768 to your screen settings, and then look for a way to get the screen size dynamically.

0
source

Source: https://habr.com/ru/post/1398110/


All Articles