source: trunk/src_apollo/org/apollo/io/IconRepository.java@ 315

Last change on this file since 315 was 315, checked in by bjn8, 16 years ago

Apollo spin-off added

File size: 1.6 KB
Line 
1package org.apollo.io;
2
3import java.awt.BasicStroke;
4import java.awt.Color;
5import java.awt.Graphics2D;
6import java.awt.Image;
7import java.awt.image.BufferedImage;
8import java.net.URL;
9import java.util.HashMap;
10
11import javax.swing.Icon;
12import javax.swing.ImageIcon;
13
14/**
15 * An icon provider. Only loads icons once and when they are requested (somple proxy approach).
16 *
17 * @author Brook Novak
18 *
19 */
20public final class IconRepository {
21
22 private static HashMap<String, Icon> _loadedIcons = new HashMap<String, Icon>();
23 private static Icon missingIcon = null;
24
25 private IconRepository() {}
26
27 private static Icon getMissingIcon() {
28
29 if (missingIcon == null) {
30
31 Image image = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
32 Graphics2D g = (Graphics2D)image.getGraphics();
33
34 g.setStroke(new BasicStroke(2.0f));
35
36 // White square with X
37 g.setColor(Color.WHITE);
38 g.fillRect(0, 0, 16, 16);
39
40 g.setColor(Color.RED);
41 g.drawLine(0, 0, 16, 16);
42 g.drawLine(0, 16, 16, 0);
43
44 missingIcon = new ImageIcon(image);
45 }
46
47 return missingIcon;
48 }
49
50 /**
51 *
52 * @param name Exclude path / package
53 * @return
54 */
55 public static Icon getIcon(String name) {
56 Icon icon = _loadedIcons.get(name);
57
58 if (icon == null) {
59 try {
60
61 URL url = ClassLoader.getSystemResource("org/apollo/icons/" + name);
62
63 if (url != null) {
64 icon = new ImageIcon(url);
65 }
66
67 } catch (Exception e) {
68 e.printStackTrace();
69 }
70
71 if (icon == null) {
72 System.err.println("WARNING: Cannot find icon named \"" + name +"\"");
73 return getMissingIcon();
74 }
75 }
76
77 return icon;
78 }
79
80}
Note: See TracBrowser for help on using the repository browser.