public synchronized void reset() { basesCache = new ImageCache<IHex, Image>(); supersCache = new ImageCache<IHex, List<Image>>(); orthosCache = new ImageCache<IHex, List<Image>>(); }
public void clearHexImageCache() { hexImageCache.clear(); }
BufferedImage createShadowMask(Image image) { int hashCode = image.hashCode(); BufferedImage mask = shadowImageCache.get(hashCode); if (mask != null) { return mask; } mask = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); float opacity = 0.4f; Graphics2D g2d = mask.createGraphics(); g2d.drawImage(image, 0, 0, null); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, opacity)); g2d.setColor(Color.BLACK); g2d.fillRect(0, 0, image.getWidth(null), image.getHeight(null)); g2d.dispose(); shadowImageCache.put(hashCode, mask); return mask; }
public synchronized Image getBase(IHex hex, Component comp) { Image i = basesCache.get(hex); if (i == null) { Object[] pair = assignMatch(hex, comp); return (Image) pair[0]; } return i; }
public synchronized void clearHex(IHex hex) { basesCache.remove(hex); }
/** * This assigns images to a hex based on the best matches it can find. * <p/> * First it assigns any images to be superimposed on a hex. These images must * have a match value of 1.0 to be added, and any time a match of this level is * achieved, any terrain involved in the match is removed from further * consideration. * <p/> * Any terrain left is used to match a base image for the hex. This time, a * match can be any value, and the first, best image is used. */ public synchronized Object[] assignMatch(IHex hex, Component comp) { IHex hexCopy = hex.duplicate(); List<Image> ortho = orthoFor(hexCopy, comp); List<Image> supers = supersFor(hexCopy, comp); Image base = baseFor(hexCopy, comp); Object[] pair = new Object[] { base, supers, ortho }; basesCache.put(hex, base); supersCache.put(hex, supers); orthosCache.put(hex, ortho); return pair; }
@SuppressWarnings("unchecked") public synchronized List<Image> getSupers(IHex hex, Component comp) { List<Image> l = supersCache.get(hex); if (l == null) { Object[] pair = assignMatch(hex, comp); return (List<Image>) pair[1]; } return l; }
/** * Clear a specific list of Coords from the hex image cache. * @param coords */ public void clearHexImageCache(Set<Coords> coords) { for (Coords c : coords) { hexImageCache.remove(c); } }
private Image createBlurredShadow(Image orig) { if ((orig == null) || orig.getWidth(this) < 0 || orig.getHeight(this) < 0) { return null; } BufferedImage mask = shadowImageCache.get(orig.hashCode()); if (mask == null) { GraphicsConfiguration config = GraphicsEnvironment .getLocalGraphicsEnvironment().getDefaultScreenDevice() .getDefaultConfiguration(); // a slightly bigger image to give room for blurring mask = config.createCompatibleImage(orig.getWidth(this)+4, orig.getHeight(this)+4, Transparency.TRANSLUCENT); Graphics g = mask.getGraphics(); g.drawImage(orig,2,2,null); g.dispose(); mask = createShadowMask(mask); mask = blurOp.filter(mask, null); if (game.getPlanetaryConditions().getLight() != PlanetaryConditions.L_DAY) { mask = blurOp.filter(mask, null); } shadowImageCache.put(orig.hashCode(), mask); } return mask; }
@SuppressWarnings("unchecked") public synchronized List<Image> getOrtho(IHex hex, Component comp) { List<Image> o = orthosCache.get(hex); if (o == null) { Object[] pair = assignMatch(hex, comp); return (List<Image>) pair[2]; } return o; }
scaledImageCache = new ImageCache<Integer, Image>();
public synchronized void boardChangedHex(BoardEvent b) { hexImageCache.remove(b.getCoords()); IHex hex = game.getBoard().getHex(b.getCoords()); tileManager.clearHex(hex); tileManager.waitForHex(hex); clearShadowMap(); // Maybe have to set the hexes' theme. Null clientgui implies board editor - don't mess with theme if ((selectedTheme != null) && !selectedTheme.equals("(Original Theme)") && (clientgui != null)) { if (selectedTheme.equals("(No Theme)") && (hex.getTheme() != null) && !hex.getTheme().equals("")) { hex.setTheme(""); game.getBoard().setHex(b.getCoords(), hex); } else if (!selectedTheme.equals(hex.getTheme())) { hex.setTheme(selectedTheme); game.getBoard().setHex(b.getCoords(), hex); } } repaint(); }
/** * Adds all images associated with the hex to the specified tracker */ public synchronized void trackHexImages(IHex hex, MediaTracker tracker) { Image base = basesCache.get(hex); List<Image> superImgs = supersCache.get(hex); List<Image> orthoImgs = orthosCache.get(hex); // add base tracker.addImage(base, 1); // add superImgs if (superImgs != null) { for (Image img: superImgs) { tracker.addImage(img, 1); } } if (orthoImgs != null) { for (Image img: orthoImgs) { tracker.addImage(img, 1); } } }
this.clientgui = clientgui; hexImageCache = new ImageCache<Coords, HexImageCacheEntry>();