use of games.strategy.triplea.ui.MapPanel in project triplea by triplea-game.
the class ScreenshotExporter method save.
private void save(final GameData gameData, final HistoryNode node, final File file) throws IOException {
// get round/step/player from history tree
int round = 0;
final Object[] pathFromRoot = node.getPath();
for (final Object pathNode : pathFromRoot) {
final HistoryNode curNode = (HistoryNode) pathNode;
if (curNode instanceof Round) {
round = ((Round) curNode).getRoundNo();
}
}
final UiContext uiContext = frame.getUiContext();
final double scale = uiContext.getScale();
// print map panel to image
final MapPanel mapPanel = frame.getMapPanel();
final BufferedImage mapImage = Util.createImage((int) (scale * mapPanel.getImageWidth()), (int) (scale * mapPanel.getImageHeight()), false);
final Graphics2D mapGraphics = mapImage.createGraphics();
try {
// workaround to get the whole map
// (otherwise the map is cut if current window is not on top of map)
final int offsetX = mapPanel.getXOffset();
final int offsetY = mapPanel.getYOffset();
mapPanel.setTopLeft(0, 0);
mapPanel.drawMapImage(mapGraphics);
mapPanel.setTopLeft(offsetX, offsetY);
// overlay title
Color titleColor = uiContext.getMapData().getColorProperty(MapData.PROPERTY_SCREENSHOT_TITLE_COLOR);
if (titleColor == null) {
titleColor = Color.BLACK;
}
final String encodedTitleX = uiContext.getMapData().getProperty(MapData.PROPERTY_SCREENSHOT_TITLE_X);
final String encodedTitleY = uiContext.getMapData().getProperty(MapData.PROPERTY_SCREENSHOT_TITLE_Y);
final String encodedTitleSize = uiContext.getMapData().getProperty(MapData.PROPERTY_SCREENSHOT_TITLE_FONT_SIZE);
int titleX;
int titleY;
int titleSize;
try {
titleX = (int) (Integer.parseInt(encodedTitleX) * scale);
titleY = (int) (Integer.parseInt(encodedTitleY) * scale);
titleSize = Integer.parseInt(encodedTitleSize);
} catch (final NumberFormatException nfe) {
// choose safe defaults
titleX = (int) (15 * scale);
titleY = (int) (15 * scale);
titleSize = 15;
}
// everything else should be scaled down onto map image
final AffineTransform transform = new AffineTransform();
transform.scale(scale, scale);
mapGraphics.setTransform(transform);
mapGraphics.setFont(new Font("Ariel", Font.BOLD, titleSize));
mapGraphics.setColor(titleColor);
if (uiContext.getMapData().getBooleanProperty(MapData.PROPERTY_SCREENSHOT_TITLE_ENABLED)) {
mapGraphics.drawString(gameData.getGameName() + " Round " + round, titleX, titleY);
}
// save Image as .png
ImageIO.write(mapImage, "png", file);
} finally {
// Clean up objects. There might be some overkill here,
// but there were memory leaks that are fixed by some/all of these.
mapImage.flush();
mapGraphics.dispose();
}
}
Aggregations