take ScreenShoot

thiss code is used to take an screen shoot and save it on project path ( or .jar path)

  • it only save PNG format files
  • it implement getEpochSeconds to avoud repeated names
  • tested on windows 10

PD: if you have any way to improve, this let me know…


/** 
395      * this method is used to take an screen shot of the game 
396      * NOTE: fileName is the name of the screenshoot saved, it have 
397      * the next format fileName[epochseconds].[extension], i suggest to 
398      * pass on the fileName the name of the game 
399      * @param w 
400      * @param h 
401      * @param fileName this is the name of the new screenshot + seconds + png  
402      * @return  
403      */ 
404     public static boolean takeScreenShoot( float w, float h, String fileName ) 
405     { 
406      
407      
408         try  
409         { 
410         Robot robot = new Robot(); 
411              
412         String format = "png"; 
413 //        String pictureName = fileName + ( Instant.now().getEpochSecond() ) + format; 
414          
415         StringBuffer sb = new StringBuffer(); 
416         sb.append( fileName ); 
417         sb.append( Instant.now().getEpochSecond() ); 
418         sb.append( '.' ); 
419         sb.append( format ); 
420          
421         Rectangle screenRect =  
422                     new Rectangle( ( int )w, ( int )h ); 
423          
424         BufferedImage screenFullImage =  
425                     robot.createScreenCapture( screenRect ); 
426          
427         ImageIO.write(  
428                 screenFullImage,  
429                 format,  
430                 new File( sb.toString() ) ); 
431          
432              
433         return true;     
434         }  
435         catch (AWTException ex)  
436         { 
437             Logger.getLogger( 
438                     Util.class.getName()). 
439                     log(Level.SEVERE, 
440                             null,  
441                     "::: error when trying to take screenshoot "+ex ); 
442         } catch (IOException ex) { 
443             Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); 
444         } 
445           
446          
447          
448         return false; 
449     }// 


Why are the w & h parameters floats?

ScreenShoot?

Also createScreenCapture works in screen coordinates, not Window coordinates, so your code is assuming the game window is positioned at the origin.
For your game that might be true, but in general it’s a flawed assumption.

If capturing the desktop is your intent, then your code should not assume the top left of the screen begins at 0,0.
It’s common for multi-monitor setups to have the primary monitor in the centre (origin: 0,0), and secondary displays to the left (origin: -width,0), and right (origin: +width,0)

To ascertain the full desktop dimensions you’ll need to query GraphicsDevice(s) & GraphicsConfiguration(s) via GraphicsEnvironment.getLocalGraphicsEnvironment().

Personally, I recommend doing something like the following:


BufferedImage b = graphicsConfiguration.createCompatibleImage(width, height, transparency);
Graphics2D g = (Graphics2D) g.createGraphics();
(render to g2d, I recommend having some "Renderer" class that renders to any g2d object and then you just submit this g2d to it and call the main render method)
g.dispose();
ImageIO.write(g, "png", fileToWriteTo);

I’m just doing this from memory but this is exactly how I have it in my game and it avoids other things interfering with the screenshot (like other windows).

For when you have multiple monitors and want to just screenshot all of them at once.

public static final BufferedImage screenshot() {

    Rectangle size = new Rectangle();
         
    for(GraphicsDevice screen : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
            
        size = size.union(screen.getDefaultConfiguration().getBounds());
    }
         
    return new Robot().createScreenCapture(size);
}