Search in sources :

Example 1 with BlendingMode

use of games.strategy.triplea.image.BlendComposite.BlendingMode in project triplea by triplea-game.

the class TileImageFactory method loadBlendedImage.

private Image loadBlendedImage(final String fileName, final boolean cache, final boolean scaled) {
    BufferedImage reliefFile = null;
    BufferedImage baseFile = null;
    // The relief tile
    final String reliefFileName = fileName.replace("baseTiles", "reliefTiles");
    final URL urlrelief = resourceLoader.getResource(reliefFileName);
    // The base tile
    final String baseFileName = fileName.replace("reliefTiles", "baseTiles");
    final URL urlBase = resourceLoader.getResource(baseFileName);
    // blank relief tile
    final String blankReliefFileName = "reliefTiles/blank_relief.png";
    final URL urlBlankRelief = resourceLoader.getResource(blankReliefFileName);
    // Get buffered images
    try {
        final Stopwatch loadingImages = new Stopwatch(logger, Level.FINE, "Loading images:" + urlrelief + " and " + urlBase);
        if (urlrelief != null) {
            reliefFile = loadCompatibleImage(urlrelief);
        }
        if (urlBase != null) {
            baseFile = loadCompatibleImage(urlBase);
        }
        loadingImages.done();
    } catch (final IOException e) {
        ClientLogger.logQuietly("Failed to load one or more images: " + urlrelief + ", " + urlBase, e);
    }
    // This does the blend
    final float alpha = getShowMapBlendAlpha();
    if (reliefFile == null) {
        try {
            reliefFile = loadCompatibleImage(urlBlankRelief);
        } catch (final IOException e) {
            ClientLogger.logQuietly("Failed to load image: " + urlBlankRelief, e);
        }
    }
    // This fixes the blank land territories
    if (baseFile == null) {
        baseFile = makeMissingBaseTile(reliefFile);
    }
    /* reversing the to/from files leaves white underlays visible */
    if (reliefFile != null) {
        final BufferedImage blendedImage = new BufferedImage(reliefFile.getWidth(null), reliefFile.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g2 = blendedImage.createGraphics();
        if (scaled && scale != 1.0) {
            final AffineTransform transform = new AffineTransform();
            transform.scale(scale, scale);
            g2.setTransform(transform);
        }
        g2.drawImage(reliefFile, 0, 0, null);
        final BlendingMode blendMode = BlendComposite.BlendingMode.valueOf(getShowMapBlendMode());
        final BlendComposite blendComposite = BlendComposite.getInstance(blendMode).derive(alpha);
        g2.setComposite(blendComposite);
        g2.drawImage(baseFile, 0, 0, null);
        final ImageRef ref = new ImageRef(blendedImage);
        if (cache) {
            imageCache.put(fileName, ref);
        }
        return blendedImage;
    }
    final ImageRef ref = new ImageRef(baseFile);
    if (cache) {
        imageCache.put(fileName, ref);
    }
    return baseFile;
}
Also used : BlendingMode(games.strategy.triplea.image.BlendComposite.BlendingMode) Stopwatch(games.strategy.triplea.util.Stopwatch) AffineTransform(java.awt.geom.AffineTransform) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) URL(java.net.URL) Graphics2D(java.awt.Graphics2D)

Aggregations

BlendingMode (games.strategy.triplea.image.BlendComposite.BlendingMode)1 Stopwatch (games.strategy.triplea.util.Stopwatch)1 Graphics2D (java.awt.Graphics2D)1 AffineTransform (java.awt.geom.AffineTransform)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 URL (java.net.URL)1