1 19 20 package org.netbeans.modules.tasklist.core.util; 21 22 import java.io.File ; 23 import org.openide.ErrorManager; 24 25 import java.awt.*; 26 import java.awt.image.*; 27 import java.util.HashMap ; 28 import javax.imageio.ImageIO ; 29 import javax.swing.ImageIcon ; 30 31 32 39 public class IconManager { 40 private HashMap written = null; 41 private File directory = null; 42 private int nextId = 1; 43 44 50 public IconManager(File base) { 51 this.directory = base; 52 } 53 54 62 public String getIcon(Image icon) { 63 if (written == null) { 64 written = new HashMap (50); 65 } 66 67 String name = (String )written.get(icon); 68 if (name == null) { 69 BufferedImage image = toBufferedImage(icon); 73 File output = null; 74 while (true) { 76 name = "tasklist-html-" + (nextId++) + ".png"; output = new File (directory, name); 80 if (!output.exists()) { 81 break; 82 } 83 } 84 try { 85 ImageIO.write(image, "png", output); } catch (Exception e) { 87 e.printStackTrace(); 88 return null; 89 } 90 written.put(icon, name); 91 } 92 return name; 93 } 94 95 100 private static BufferedImage toBufferedImage(Image image) { 101 if (image instanceof BufferedImage) { 103 return (BufferedImage)image; 104 } 105 106 image = new ImageIcon (image).getImage(); 108 109 boolean hasAlpha = true; 113 114 BufferedImage bimage = null; 116 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 117 try { 118 int transparency = Transparency.OPAQUE; 120 if (hasAlpha) { 121 transparency = Transparency.BITMASK; 122 } 123 124 GraphicsDevice gs = ge.getDefaultScreenDevice(); 126 GraphicsConfiguration gc = gs.getDefaultConfiguration(); 127 bimage = gc.createCompatibleImage( 128 image.getWidth(null), image.getHeight(null), transparency); 129 } catch (HeadlessException e) { 130 } 132 133 if (bimage == null) { 134 int type = BufferedImage.TYPE_INT_RGB; 136 if (hasAlpha) { 137 type = BufferedImage.TYPE_INT_ARGB; 138 } 139 bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); 140 } 141 142 Graphics g = bimage.createGraphics(); 144 145 g.drawImage(image, 0, 0, null); 147 g.dispose(); 148 149 return bimage; 150 } 151 } 152 | Popular Tags |