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 static enum OverlayPosition {NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST}
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * The icon cache
|
---|
33 | */
|
---|
34 | private static Map<URL, Image> cache = new HashMap<URL, Image>();
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Return an image from the specified location.
|
---|
38 | *
|
---|
39 | * @param subdir The position of the directory, e.g. "layer"
|
---|
40 | * @param name The icons name (without the ending of ".png")
|
---|
41 | * @return The requested Image.
|
---|
42 | */
|
---|
43 | public static ImageIcon get(String subdir, String name) {
|
---|
44 | if (subdir != "")
|
---|
45 | subdir += "/";
|
---|
46 | String ext = name.indexOf('.') != -1 ? "" : ".png";
|
---|
47 | URL path = Main.class.getResource("/images/"+subdir+name+ext);
|
---|
48 | if (path == null)
|
---|
49 | throw new NullPointerException("/images/"+subdir+name+ext+" not found");
|
---|
50 | Image img = cache.get(path);
|
---|
51 | if (img == null) {
|
---|
52 | img = Toolkit.getDefaultToolkit().createImage(path);
|
---|
53 | cache.put(path, img);
|
---|
54 | }
|
---|
55 | return new ImageIcon(img);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Shortcut for get("", name);
|
---|
60 | */
|
---|
61 | public static ImageIcon get(String name) {
|
---|
62 | return get("", name);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * @return an icon that represent the overlay of the two given icons. The
|
---|
67 | * second icon is layed on the first relative to the given position.
|
---|
68 | */
|
---|
69 | public static Icon overlay(Icon ground, String overlayImage, OverlayPosition pos) {
|
---|
70 | GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
|
---|
71 | int w = ground.getIconWidth();
|
---|
72 | int h = ground.getIconHeight();
|
---|
73 | ImageIcon overlay = ImageProvider.get("overlay",overlayImage);
|
---|
74 | int wo = overlay.getIconWidth();
|
---|
75 | int ho = overlay.getIconHeight();
|
---|
76 | BufferedImage img = conf.createCompatibleImage(w,h, Transparency.TRANSLUCENT);
|
---|
77 | Graphics g = img.createGraphics();
|
---|
78 | ground.paintIcon(null, g, 0, 0);
|
---|
79 | int x = 0, y = 0;
|
---|
80 | switch (pos) {
|
---|
81 | case NORTHWEST:
|
---|
82 | x = 0;
|
---|
83 | y = 0;
|
---|
84 | break;
|
---|
85 | case NORTHEAST:
|
---|
86 | x = w-wo;
|
---|
87 | y = 0;
|
---|
88 | break;
|
---|
89 | case SOUTHWEST:
|
---|
90 | x = 0;
|
---|
91 | y = h-ho;
|
---|
92 | break;
|
---|
93 | case SOUTHEAST:
|
---|
94 | x = w-wo;
|
---|
95 | y = h-ho;
|
---|
96 | break;
|
---|
97 | }
|
---|
98 | overlay.paintIcon(null, g, x, y);
|
---|
99 | return new ImageIcon(img);
|
---|
100 | }
|
---|
101 | }
|
---|