use of com.glitchcog.fontificator.gui.chat.ChatPanel in project ChatGameFontificator by GlitchCog.
the class ControlWindow method saveScreenshot.
/**
* Takes and saves a screenshot of the current chat window
*
* @return whether the screenshot was saved
*/
private boolean saveScreenshot() {
// Take the screenshot before the save file chooser is shown
ChatPanel chat = chatWindow.getChatPanel();
BufferedImage chatImage = new BufferedImage(chat.getWidth(), chat.getHeight(), screenshotOptions.isTransparencyEnabled() ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
Graphics chatGraphics = chatImage.getGraphics();
chat.paint(chatGraphics);
final boolean chromaEnabled = Boolean.toString(true).equalsIgnoreCase(fProps.getProperty(FontificatorProperties.KEY_CHAT_CHROMA_ENABLED));
if (screenshotOptions.isTransparencyEnabled() && chromaEnabled) {
final int chromaKey = new Color(Integer.parseInt(fProps.getProperty(FontificatorProperties.KEY_COLOR_CHROMA_KEY), 16)).getRGB();
final int transparentPixel = new Color(0, true).getRGB();
for (int r = 0; r < chatImage.getHeight(); r++) {
for (int c = 0; c < chatImage.getWidth(); c++) {
if (chatImage.getRGB(c, r) == chromaKey) {
chatImage.setRGB(c, r, transparentPixel);
}
}
}
}
File saveFile = getTargetSaveFile(screenshotSaver, DEFAULT_SCREENSHOT_FILE_EXTENSION);
if (saveFile != null) {
try {
if (screenshotOptions.isMetadataEnabled()) {
ImageWriter writer = ImageIO.getImageWritersByFormatName("PNG").next();
ImageOutputStream stream = ImageIO.createImageOutputStream(saveFile);
writer.setOutput(stream);
IIOMetadata metadata = writer.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(chatImage), writer.getDefaultWriteParam());
IIOMetadataNode title = generateMetadataNode("Title", "CGF Screenshot");
IIOMetadataNode software = generateMetadataNode("Software", "Chat Game Fontificator");
final String fontGameName = ControlPanelFont.getFontGameName(fProps.getProperty(FontificatorProperties.KEY_FONT_FILE_FONT));
final String borderGameName = ControlPanelFont.getBorderGameName(fProps.getProperty(FontificatorProperties.KEY_FONT_FILE_BORDER));
IIOMetadataNode description = generateMetadataNode("Description", fontGameName + " Font / " + borderGameName + " Border");
IIOMetadataNode text = new IIOMetadataNode("tEXt");
text.appendChild(title);
text.appendChild(software);
text.appendChild(description);
final String metadataFormatStr = "javax_imageio_png_1.0";
IIOMetadataNode root = new IIOMetadataNode(metadataFormatStr);
root.appendChild(text);
metadata.mergeTree(metadataFormatStr, root);
writer.write(metadata, new IIOImage(chatImage, null, metadata), writer.getDefaultWriteParam());
stream.close();
} else {
ImageIO.write(chatImage, "png", saveFile);
}
return true;
} catch (Exception e) {
logger.error("Unable to save screenshot", e);
return false;
}
}
return false;
}
Aggregations