01: package com.vividsolutions.jump.workbench.ui.renderer.style;
02:
03: import com.vividsolutions.jump.util.Blackboard;
04: import com.vividsolutions.jump.util.CollectionUtil;
05: import com.vividsolutions.jump.workbench.ui.images.IconLoader;
06:
07: import java.awt.AlphaComposite;
08: import java.awt.Color;
09: import java.awt.Graphics2D;
10: import java.awt.image.BufferedImage;
11:
12: import javax.swing.ImageIcon;
13:
14: /**
15: * You can set the alpha by calling #setColor (only the alpha will be read)
16: */
17: public class ImageFillPattern extends BasicFillPattern {
18: private static final String FILENAME_KEY = "FILENAME";
19: private static final String CLASS_KEY = "CLASS";
20:
21: /**
22: * @param resourceName name of a resource associated with the given class
23: * (e.g. the name of a .png, .gif, or .jpg file in the same package as the class)
24: */
25: public ImageFillPattern(Class c, String resourceName) {
26: super (new Blackboard().putAll(CollectionUtil
27: .createMap(new Object[] { BasicFillPattern.COLOR_KEY,
28: Color.black, CLASS_KEY, c, FILENAME_KEY,
29: resourceName })));
30: }
31:
32: /**
33: * Parameterless constructor for Java2XML
34: */
35: public ImageFillPattern() {
36: }
37:
38: public BufferedImage createImage(Blackboard properties) {
39: ImageIcon imageIcon = new ImageIcon(((Class) properties
40: .get(CLASS_KEY)).getResource(properties.get(
41: FILENAME_KEY).toString()));
42: BufferedImage bufferedImage = new BufferedImage(imageIcon
43: .getIconWidth(), imageIcon.getIconHeight(),
44: BufferedImage.TYPE_INT_ARGB);
45: Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
46: g.setComposite(AlphaComposite.getInstance(
47: AlphaComposite.SRC_OVER, ((Color) getProperties().get(
48: COLOR_KEY)).getAlpha() / 255f));
49: g.drawImage(imageIcon.getImage(), 0, 0, null);
50:
51: return bufferedImage;
52: }
53: }
|