Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.nio.FloatBuffer;
- import javax.imageio.ImageIO;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.input.Keyboard;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.opengl.GL11;
- public class MainWindow {
- /**
- * Display width
- */
- private static final int WIDTH = 400;
- /**
- * Display height
- */
- private static final int HEIGHT = 300;
- /**
- * Entry point
- * @param args
- */
- public static void main(String[] args) {
- MainWindow mw = new MainWindow();
- if(mw.init()) {
- mw.mainLoop();
- }
- }
- /**
- * Empty constructor.
- */
- public MainWindow() {
- }
- /**
- * Initializes the OpenGL Context (Display) and the Keyboard.
- * @return true if successful
- */
- public boolean init() {
- try {
- Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
- Display.setTitle("Screenshots");
- Display.create();
- Keyboard.create();
- return true;
- } catch (LWJGLException e) {
- e.printStackTrace();
- }
- return false;
- }
- /**
- * Handles the mainLoop.
- */
- public void mainLoop() {
- GL11.glClearColor(0, 0, 0, 0);
- while(!Display.isCloseRequested()) {
- // handle input
- while(Keyboard.next()) {
- if(Keyboard.getEventKeyState()) {
- switch(Keyboard.getEventKey()) {
- case Keyboard.KEY_F2: saveScreenshot(); break;
- }
- }
- }
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
- // draw a red triangle
- GL11.glColor3f(1, 0, 0);
- GL11.glBegin(GL11.GL_LINE_LOOP);
- GL11.glVertex2f(0, 0);
- GL11.glVertex2f(0, 1);
- GL11.glVertex2f(1, 0);
- GL11.glEnd();
- Display.update();
- }
- }
- /**
- * Reads the current front buffer and stores it into a file.
- */
- private void saveScreenshot() {
- // read current buffer
- FloatBuffer imageData = BufferUtils.createFloatBuffer(WIDTH * HEIGHT * 3);
- GL11.glReadPixels(0, 0, WIDTH, HEIGHT, GL11.GL_RGB, GL11.GL_FLOAT, imageData);
- imageData.rewind();
- // fill rgbArray for BufferedImage
- int[] rgbArray = new int[WIDTH * HEIGHT];
- for(int y = 0; y < HEIGHT; ++y) {
- for(int x = 0; x < WIDTH; ++x) {
- int r = (int)(imageData.get() * 255) << 16;
- int g = (int)(imageData.get() * 255) << 8;
- int b = (int)(imageData.get() * 255);
- int i = ((HEIGHT - 1) - y) * WIDTH + x;
- rgbArray[i] = r + g + b;
- }
- }
- // create and save image
- BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
- image.setRGB(0, 0, WIDTH, HEIGHT, rgbArray, 0, WIDTH);
- File outputfile = getNextScreenFile();
- try {
- ImageIO.write(image, "png", outputfile);
- } catch (IOException e) {
- e.printStackTrace();
- System.err.println("Can not save screenshot!");
- }
- }
- /**
- * Generates a screenshot file.
- * @return generated File
- */
- private File getNextScreenFile() {
- // create image name
- String fileName = "screenshot_" + System.currentTimeMillis();
- File imageToSave = new File(fileName + ".png");
- // check for duplicates
- int duplicate = 0;
- while(imageToSave.exists()) {
- imageToSave = new File(fileName + "_" + ++duplicate + ".png");
- }
- return imageToSave;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement