Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.commands;
24 
28 import java.awt.Color;
29 import java.awt.Graphics;
30 import java.awt.image.BufferedImage;
31 import java.io.IOException;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import javax.imageio.ImageIO;
35 import org.jetbrains.annotations.NotNull;
36 
42 public class ScreenshotCommand extends AbstractCommand {
43 
47  @NotNull
49 
53  @NotNull
55 
64  super("screenshot", crossfireServerConnection);
65  this.windowRenderer = windowRenderer;
66  this.screenshotFiles = screenshotFiles;
67  }
68 
69  @Override
70  public boolean allArguments() {
71  return true;
72  }
73 
74  @Override
75  public void execute(@NotNull final String args) {
76  final Path file;
77  if (args.isEmpty()) {
78  try {
79  file = screenshotFiles.getFile();
80  } catch (final IOException ex) {
81  drawInfoError("Failed to create screenshot filename: "+ex.getMessage());
82  return;
83  }
84  } else {
85  file = Paths.get(args);
86  }
87 
88  final BufferedImage image = new BufferedImage(windowRenderer.getWindowWidth(), windowRenderer.getWindowHeight(), BufferedImage.TYPE_INT_RGB);
89  final Graphics g = image.createGraphics();
90  try {
91  g.setColor(Color.black);
94  } finally {
95  g.dispose();
96  }
97  try {
98  ImageIO.write(image, "png", file.toFile());
99  } catch (final IOException ex) {
100  drawInfoError("Cannot write screenshot "+file+": "+ex.getMessage());
101  return;
102  } catch (final NullPointerException ignored) { // ImageIO.write() crashes if the destination cannot be written to
103  drawInfoError("Cannot write screenshot "+file);
104  return;
105  }
106 
107  drawInfo("Saved screenshot to "+file);
108  }
109 
110  @NotNull
111  @Override
112  public String getHelp() {
113  //noinspection StringBufferReplaceableByString
114  final StringBuilder sb = new StringBuilder();
115  sb.append("Creates a screenshot image\n");
116  sb.append("\n");
117  sb.append("Usage: screenshot\n");
118  sb.append("Usage: screenshot <file>\n");
119  sb.append("\n");
120  sb.append("Write the current display contents into a PNG file. ");
121  sb.append("If <file> is omitted, the file names 'screenshot0.png', ..., 'screenshot9.png' are used instead.\n");
122  sb.append("Note: If the file already exists, it is overwritten without confirmation.\n");
123  return sb.toString();
124  }
125 
126 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.server
com.realtime.crossfire.jxclient.commands.ScreenshotCommand.windowRenderer
final JXCWindowRenderer windowRenderer
Definition: ScreenshotCommand.java:48
com.realtime.crossfire.jxclient.commands.ScreenshotCommand.execute
void execute(@NotNull final String args)
Definition: ScreenshotCommand.java:75
com.realtime.crossfire.jxclient.commands.AbstractCommand.crossfireServerConnection
final CrossfireServerConnection crossfireServerConnection
Definition: AbstractCommand.java:45
com.realtime.crossfire.jxclient.gui.commands.ScreenshotFiles.getFile
Path getFile()
Definition: ScreenshotFiles.java:54
com.realtime.crossfire.jxclient.gui.misc.JXCWindowRenderer
Definition: JXCWindowRenderer.java:87
com.realtime.crossfire.jxclient.commands.AbstractCommand.drawInfoError
void drawInfoError(@NotNull final String message)
Definition: AbstractCommand.java:69
com.realtime.crossfire.jxclient.commands.ScreenshotCommand.getHelp
String getHelp()
Definition: ScreenshotCommand.java:112
com.realtime.crossfire.jxclient.gui.misc.JXCWindowRenderer.redraw
void redraw(@NotNull final Graphics g)
Definition: JXCWindowRenderer.java:634
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection
Definition: CrossfireServerConnection.java:37
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.gui.misc.JXCWindowRenderer.getWindowHeight
int getWindowHeight()
Definition: JXCWindowRenderer.java:1116
com.realtime.crossfire.jxclient.commands.ScreenshotCommand.allArguments
boolean allArguments()
Definition: ScreenshotCommand.java:70
com.realtime.crossfire.jxclient.server.crossfire
Definition: AbstractCrossfireServerConnection.java:23
com.realtime.crossfire.jxclient.commands.ScreenshotCommand.ScreenshotCommand
ScreenshotCommand(@NotNull final JXCWindowRenderer windowRenderer, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final ScreenshotFiles screenshotFiles)
Definition: ScreenshotCommand.java:63
com.realtime.crossfire.jxclient.commands.ScreenshotCommand.screenshotFiles
final ScreenshotFiles screenshotFiles
Definition: ScreenshotCommand.java:54
com.realtime.crossfire
com.realtime.crossfire.jxclient.commands.AbstractCommand.drawInfo
void drawInfo(@NotNull final String message)
Definition: AbstractCommand.java:61
com.realtime
com.realtime.crossfire.jxclient.commands.ScreenshotCommand
Definition: ScreenshotCommand.java:42
com.realtime.crossfire.jxclient.gui.commands.ScreenshotFiles
Definition: ScreenshotFiles.java:34
com
com.realtime.crossfire.jxclient.commands.AbstractCommand
Definition: AbstractCommand.java:33
com.realtime.crossfire.jxclient.gui.commands
Definition: AccountCreateCharacterCommand.java:23
com.realtime.crossfire.jxclient.gui.misc
Definition: GUICheckBox.java:23
com.realtime.crossfire.jxclient.gui.misc.JXCWindowRenderer.getWindowWidth
int getWindowWidth()
Definition: JXCWindowRenderer.java:1108