1 package net.sf.openrocket.gui.main;
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
6 import java.util.HashMap;
8 import javax.imageio.ImageIO;
9 import javax.swing.Icon;
10 import javax.swing.ImageIcon;
12 import net.sf.openrocket.rocketcomponent.BodyTube;
13 import net.sf.openrocket.rocketcomponent.Bulkhead;
14 import net.sf.openrocket.rocketcomponent.CenteringRing;
15 import net.sf.openrocket.rocketcomponent.EllipticalFinSet;
16 import net.sf.openrocket.rocketcomponent.EngineBlock;
17 import net.sf.openrocket.rocketcomponent.FreeformFinSet;
18 import net.sf.openrocket.rocketcomponent.InnerTube;
19 import net.sf.openrocket.rocketcomponent.LaunchLug;
20 import net.sf.openrocket.rocketcomponent.MassComponent;
21 import net.sf.openrocket.rocketcomponent.NoseCone;
22 import net.sf.openrocket.rocketcomponent.Parachute;
23 import net.sf.openrocket.rocketcomponent.ShockCord;
24 import net.sf.openrocket.rocketcomponent.Streamer;
25 import net.sf.openrocket.rocketcomponent.Transition;
26 import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
27 import net.sf.openrocket.rocketcomponent.TubeCoupler;
30 public class ComponentIcons {
32 private static final String ICON_DIRECTORY = "pix/componenticons/";
33 private static final String SMALL_SUFFIX = "-small.png";
34 private static final String LARGE_SUFFIX = "-large.png";
36 private static final HashMap<Class<?>,ImageIcon> SMALL_ICONS =
37 new HashMap<Class<?>,ImageIcon>();
38 private static final HashMap<Class<?>,ImageIcon> LARGE_ICONS =
39 new HashMap<Class<?>,ImageIcon>();
40 private static final HashMap<Class<?>,ImageIcon> DISABLED_ICONS =
41 new HashMap<Class<?>,ImageIcon>();
44 load("nosecone", "Nose cone", NoseCone.class);
45 load("bodytube", "Body tube", BodyTube.class);
46 load("transition", "Transition", Transition.class);
47 load("trapezoidfin", "Trapezoidal fin set", TrapezoidFinSet.class);
48 load("ellipticalfin", "Elliptical fin set", EllipticalFinSet.class);
49 load("freeformfin", "Freeform fin set", FreeformFinSet.class);
50 load("launchlug", "Launch lug", LaunchLug.class);
51 load("innertube", "Inner tube", InnerTube.class);
52 load("tubecoupler", "Tube coupler", TubeCoupler.class);
53 load("centeringring", "Centering ring", CenteringRing.class);
54 load("bulkhead", "Bulk head", Bulkhead.class);
55 load("engineblock", "Engine block", EngineBlock.class);
56 load("parachute", "Parachute", Parachute.class);
57 load("streamer", "Streamer", Streamer.class);
58 load("shockcord", "Shock cord", ShockCord.class);
59 load("mass", "Mass component", MassComponent.class);
62 private static void load(String filename, String name, Class<?> componentClass) {
63 ImageIcon icon = loadSmall(ICON_DIRECTORY + filename + SMALL_SUFFIX, name);
64 SMALL_ICONS.put(componentClass, icon);
66 ImageIcon[] icons = loadLarge(ICON_DIRECTORY + filename + LARGE_SUFFIX, name);
67 LARGE_ICONS.put(componentClass, icons[0]);
68 DISABLED_ICONS.put(componentClass, icons[1]);
73 public static Icon getSmallIcon(Class<?> c) {
74 return SMALL_ICONS.get(c);
76 public static Icon getLargeIcon(Class<?> c) {
77 return LARGE_ICONS.get(c);
79 public static Icon getLargeDisabledIcon(Class<?> c) {
80 return DISABLED_ICONS.get(c);
86 private static ImageIcon loadSmall(String file, String desc) {
87 URL url = ClassLoader.getSystemResource(file);
89 ExceptionHandler.handleErrorCondition("ERROR: Couldn't find file: " + file);
92 return new ImageIcon(url, desc);
96 private static ImageIcon[] loadLarge(String file, String desc) {
97 ImageIcon[] icons = new ImageIcon[2];
99 URL url = ClassLoader.getSystemResource(file);
101 BufferedImage bi,bi2;
103 bi = ImageIO.read(url);
104 bi2 = ImageIO.read(url); // How the fsck can one duplicate a BufferedImage???
105 } catch (IOException e) {
106 ExceptionHandler.handleErrorCondition("ERROR: Couldn't read file: "+file, e);
107 return new ImageIcon[]{null,null};
110 icons[0] = new ImageIcon(bi,desc);
112 // Create disabled icon
113 if (false) { // Fade using alpha
115 int rgb[] = bi2.getRGB(0,0,bi2.getWidth(),bi2.getHeight(),null,0,bi2.getWidth());
116 for (int i=0; i<rgb.length; i++) {
117 final int alpha = (rgb[i]>>24)&0xFF;
118 rgb[i] = (rgb[i]&0xFFFFFF) | (alpha/3)<<24;
120 //rgb[i] = (rgb[i]&0xFFFFFF) | ((rgb[i]>>1)&0x3F000000);
122 bi2.setRGB(0, 0, bi2.getWidth(), bi2.getHeight(), rgb, 0, bi2.getWidth());
124 } else { // Raster alpha
126 for (int x=0; x < bi.getWidth(); x++) {
127 for (int y=0; y < bi.getHeight(); y++) {
136 icons[1] = new ImageIcon(bi2,desc + " (disabled)");
140 ExceptionHandler.handleErrorCondition("ERROR: Couldn't find file: " + file);
141 return new ImageIcon[]{null,null};