Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  take ScreenShoot  (Read 858 times)
0 Members and 1 Guest are viewing this topic.
Offline pavul

Junior Devvie


Medals: 3
Exp: 4 years



« Posted 2017-09-05 15:26:53 »

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...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
/** 
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     }//

its better not know why it works, that know why is not working
Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2017-09-11 21:07:54 »

Why are the w & h parameters floats?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Abuse

JGO Ninja


Medals: 60


falling into the abyss of reality


« Reply #2 - Posted 2017-09-11 22:59:44 »

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().
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cygnus
« Reply #3 - Posted 2017-09-14 18:06:26 »

Personally, I recommend doing something like the following:
1  
2  
3  
4  
5  
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).
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1569 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!