source: josm/src/org/openstreetmap/josm/tools/ImageProvider.java@ 91

Last change on this file since 91 was 91, checked in by imi, 18 years ago
  • added autoscale after selection, layer and conflict
  • added URL download for selection search
File size: 2.6 KB
Line 
1package org.openstreetmap.josm.tools;
2
3import java.awt.Graphics;
4import java.awt.GraphicsConfiguration;
5import java.awt.GraphicsEnvironment;
6import java.awt.Image;
7import java.awt.Toolkit;
8import java.awt.Transparency;
9import java.awt.image.BufferedImage;
10import java.net.URL;
11import java.util.HashMap;
12import java.util.Map;
13
14import javax.swing.Icon;
15import javax.swing.ImageIcon;
16
17import org.openstreetmap.josm.Main;
18
19/**
20 * Helperclass to support the application with images.
21 * @author imi
22 */
23public 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}
Note: See TracBrowser for help on using the repository browser.