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 }//