• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.jme3.app.state;
2 
3 import com.jme3.app.Application;
4 import com.jme3.input.InputManager;
5 import com.jme3.input.KeyInput;
6 import com.jme3.input.controls.ActionListener;
7 import com.jme3.input.controls.KeyTrigger;
8 import com.jme3.post.SceneProcessor;
9 import com.jme3.renderer.RenderManager;
10 import com.jme3.renderer.Renderer;
11 import com.jme3.renderer.ViewPort;
12 import com.jme3.renderer.queue.RenderQueue;
13 import com.jme3.texture.FrameBuffer;
14 import com.jme3.util.BufferUtils;
15 import com.jme3.util.Screenshots;
16 import java.awt.image.BufferedImage;
17 import java.io.File;
18 import java.io.IOException;
19 import java.nio.ByteBuffer;
20 import java.util.List;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import javax.imageio.ImageIO;
24 
25 public class ScreenshotAppState extends AbstractAppState implements ActionListener, SceneProcessor {
26 
27     private static final Logger logger = Logger.getLogger(ScreenshotAppState.class.getName());
28     private boolean capture = false;
29     private Renderer renderer;
30     private ByteBuffer outBuf;
31     private String appName;
32     private int shotIndex = 0;
33     private BufferedImage awtImage;
34 
35     @Override
initialize(AppStateManager stateManager, Application app)36     public void initialize(AppStateManager stateManager, Application app) {
37         if (!super.isInitialized()){
38             InputManager inputManager = app.getInputManager();
39             inputManager.addMapping("ScreenShot", new KeyTrigger(KeyInput.KEY_SYSRQ));
40             inputManager.addListener(this, "ScreenShot");
41 
42             List<ViewPort> vps = app.getRenderManager().getPostViews();
43             ViewPort last = vps.get(vps.size()-1);
44             last.addProcessor(this);
45 
46             appName = app.getClass().getSimpleName();
47         }
48 
49         super.initialize(stateManager, app);
50     }
51 
onAction(String name, boolean value, float tpf)52     public void onAction(String name, boolean value, float tpf) {
53         if (value){
54             capture = true;
55         }
56     }
57 
initialize(RenderManager rm, ViewPort vp)58     public void initialize(RenderManager rm, ViewPort vp) {
59         renderer = rm.getRenderer();
60         reshape(vp, vp.getCamera().getWidth(), vp.getCamera().getHeight());
61     }
62 
63     @Override
isInitialized()64     public boolean isInitialized() {
65         return super.isInitialized() && renderer != null;
66     }
67 
reshape(ViewPort vp, int w, int h)68     public void reshape(ViewPort vp, int w, int h) {
69         outBuf = BufferUtils.createByteBuffer(w*h*4);
70         awtImage = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
71     }
72 
preFrame(float tpf)73     public void preFrame(float tpf) {
74     }
75 
postQueue(RenderQueue rq)76     public void postQueue(RenderQueue rq) {
77     }
78 
postFrame(FrameBuffer out)79     public void postFrame(FrameBuffer out) {
80         if (capture){
81             capture = false;
82             shotIndex++;
83 
84             renderer.readFrameBuffer(out, outBuf);
85             Screenshots.convertScreenShot(outBuf, awtImage);
86 
87             try {
88                 ImageIO.write(awtImage, "png", new File(appName + shotIndex + ".png"));
89             } catch (IOException ex){
90                 logger.log(Level.SEVERE, "Error while saving screenshot", ex);
91             }
92         }
93     }
94 }
95