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

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

Many fixes and usability improvements.

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