1 | package org.openstreetmap.josm.tools;
|
---|
2 |
|
---|
3 | import java.awt.Graphics;
|
---|
4 | import java.awt.GraphicsConfiguration;
|
---|
5 | import java.awt.GraphicsEnvironment;
|
---|
6 | import java.awt.Image;
|
---|
7 | import java.awt.Toolkit;
|
---|
8 | import java.awt.Transparency;
|
---|
9 | import java.awt.image.BufferedImage;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import javax.swing.Icon;
|
---|
15 | import javax.swing.ImageIcon;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Helperclass to support the application with images.
|
---|
21 | * @author imi
|
---|
22 | */
|
---|
23 | public class ImageProvider {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Position of an overlay icon
|
---|
27 | * @author imi
|
---|
28 | */
|
---|
29 | public final static class OverlayPosition {
|
---|
30 | private OverlayPosition() {}
|
---|
31 | public static OverlayPosition NORTHWEST = new OverlayPosition();
|
---|
32 | public static OverlayPosition NORTHEAST = new OverlayPosition();
|
---|
33 | public static OverlayPosition SOUTHWEST = new OverlayPosition();
|
---|
34 | public static OverlayPosition SOUTHEAST = new OverlayPosition();
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * The icon cache
|
---|
40 | */
|
---|
41 | private static Map<URL, Image> cache = new HashMap<URL, Image>();
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Return an image from the specified location.
|
---|
45 | *
|
---|
46 | * @param subdir The position of the directory, e.g. "layer"
|
---|
47 | * @param name The icons name (without the ending of ".png")
|
---|
48 | * @return The requested Image.
|
---|
49 | */
|
---|
50 | public static ImageIcon get(String subdir, String name) {
|
---|
51 | if (subdir != "")
|
---|
52 | subdir += "/";
|
---|
53 | String ext = name.indexOf('.') != -1 ? "" : ".png";
|
---|
54 | URL path = Main.class.getResource("/images/"+subdir+name+ext);
|
---|
55 | if (path == null)
|
---|
56 | throw new NullPointerException("/images/"+subdir+name+ext+" not found");
|
---|
57 | Image img = cache.get(path);
|
---|
58 | if (img == null) {
|
---|
59 | img = Toolkit.getDefaultToolkit().createImage(path);
|
---|
60 | cache.put(path, img);
|
---|
61 | }
|
---|
62 | return new ImageIcon(img);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Shortcut for get("", name);
|
---|
67 | */
|
---|
68 | public static ImageIcon get(String name) {
|
---|
69 | return get("", name);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Return an icon that represent the overlay of the two given icons. The
|
---|
74 | * second icon is layed on the first relative to the given position.
|
---|
75 | *
|
---|
76 | * @param ground The ground icon (base)
|
---|
77 | * @param overlay The icon to put on top of the ground (overlay)
|
---|
78 | * @return The merged icon.
|
---|
79 | */
|
---|
80 | public static Icon overlay(Icon ground, Icon overlay, OverlayPosition pos) {
|
---|
81 | GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
|
---|
82 | int w = ground.getIconWidth();
|
---|
83 | int h = ground.getIconHeight();
|
---|
84 | int wo = overlay.getIconWidth();
|
---|
85 | int ho = overlay.getIconHeight();
|
---|
86 | BufferedImage img = conf.createCompatibleImage(w,h, Transparency.TRANSLUCENT);
|
---|
87 | Graphics g = img.createGraphics();
|
---|
88 | ground.paintIcon(null, g, 0, 0);
|
---|
89 | int x = 0, y = 0;
|
---|
90 | if (pos == OverlayPosition.NORTHWEST) {
|
---|
91 | x = 0;
|
---|
92 | y = 0;
|
---|
93 | } else if (pos == OverlayPosition.NORTHEAST) {
|
---|
94 | x = w-wo;
|
---|
95 | y = 0;
|
---|
96 | } else if (pos == OverlayPosition.SOUTHWEST) {
|
---|
97 | x = 0;
|
---|
98 | y = h-ho;
|
---|
99 | } else if (pos == OverlayPosition.SOUTHEAST) {
|
---|
100 | x = w-wo;
|
---|
101 | y = h-ho;
|
---|
102 | }
|
---|
103 | overlay.paintIcon(null, g, x, y);
|
---|
104 | return new ImageIcon(img);
|
---|
105 | }
|
---|
106 | }
|
---|