source: josm/src/org/openstreetmap/josm/gui/ImageProvider.java@ 22

Last change on this file since 22 was 22, checked in by imi, 19 years ago

starting restructure of dataset. Checkpoint is broken!

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