Crossfire JXClient, Trunk  R20561
ScreenshotCommand.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.commands;
23 
27 import java.awt.Color;
28 import java.awt.Graphics;
29 import java.awt.image.BufferedImage;
30 import java.io.File;
31 import java.io.IOException;
32 import javax.imageio.ImageIO;
33 import org.jetbrains.annotations.NotNull;
34 
40 public class ScreenshotCommand extends AbstractCommand {
41 
45  @NotNull
47 
51  @NotNull
53 
61  public ScreenshotCommand(@NotNull final JXCWindowRenderer windowRenderer, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final ScreenshotFiles screenshotFiles) {
62  super("screenshot", crossfireServerConnection);
63  this.windowRenderer = windowRenderer;
64  this.screenshotFiles = screenshotFiles;
65  }
66 
70  @Override
71  public boolean allArguments() {
72  return true;
73  }
74 
78  @Override
79  public void execute(@NotNull final String args) {
80  final File file;
81  if (args.isEmpty()) {
82  try {
83  file = screenshotFiles.getFile();
84  } catch (final IOException ex) {
85  drawInfoError("Failed to create screenshot filename: "+ex.getMessage());
86  return;
87  }
88  } else {
89  file = new File(args);
90  }
91 
92  final BufferedImage image = new BufferedImage(windowRenderer.getWindowWidth(), windowRenderer.getWindowHeight(), BufferedImage.TYPE_INT_RGB);
93  final Graphics g = image.createGraphics();
94  try {
95  g.setColor(Color.black);
96  g.fillRect(0, 0, windowRenderer.getWindowWidth(), windowRenderer.getWindowHeight());
97  windowRenderer.redraw(g);
98  } finally {
99  g.dispose();
100  }
101  try {
102  ImageIO.write(image, "png", file);
103  } catch (final IOException ex) {
104  drawInfoError("Cannot write screenshot "+file.getPath()+": "+ex.getMessage());
105  return;
106  } catch (final NullPointerException ignored) { // ImageIO.write() crashes if the destination cannot be written to
107  drawInfoError("Cannot write screenshot "+file.getPath());
108  return;
109  }
110 
111  drawInfo("Saved screenshot to "+file.getPath());
112  }
113 
114 }
Abstract base class for Command implementations.
boolean allArguments()
Returns whether all remaining commands should be included as arguments.whether all remaining commands...
void drawInfoError(@NotNull final String message)
Displays an error message.
File getFile()
Returns a File for the next screenshot file.
final CrossfireServerConnection crossfireServerConnection
The connection instance.
final ScreenshotFiles screenshotFiles
The ScreenshotFiles instance for creating screenshot file names.
int getWindowHeight()
Returns the height of the client area.
Helper class for creating file names for screenshot files.
void redraw(@NotNull final Graphics g)
Paints the view into the given graphics instance.
ScreenshotCommand(@NotNull final JXCWindowRenderer windowRenderer, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final ScreenshotFiles screenshotFiles)
Creates a new instance.
final JXCWindowRenderer windowRenderer
The renderer to use.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
int getWindowWidth()
Returns the width of the client area.
void drawInfo(@NotNull final String message)
Displays a regular output message.
void execute(@NotNull final String args)
Executes the command with the given arguments.the command arguments