1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.awt.Image ; 23 import java.awt.Toolkit ; 24 import java.awt.image.BufferedImage ; 25 import java.awt.image.ColorModel ; 26 import java.awt.image.ImageConsumer ; 27 import java.awt.image.ImageObserver ; 28 import java.awt.image.MemoryImageSource ; 29 import java.awt.image.PixelGrabber ; 30 import java.io.ByteArrayOutputStream ; 31 32 import java.io.Externalizable ; 33 import java.io.IOException ; 34 import java.io.ObjectInput ; 35 import java.io.ObjectOutput ; 36 import java.util.Hashtable ; 37 38 import javax.swing.Icon ; 39 import javax.swing.ImageIcon ; 40 import javax.swing.JPanel ; 41 import org.openide.util.Utilities; 42 43 47 public class ExtIcon { 48 49 private Image image; 50 51 public ExtIcon() { 52 } 53 54 public ExtIcon(byte[] content) { 55 Toolkit toolkit = Toolkit.getDefaultToolkit(); 56 ColorModel cm = ColorModel.getRGBdefault(); 57 image = toolkit.createImage(new MemoryImageSource (16, 16, cm, content, 0, 16 * 4)); 58 } 59 60 public void setIcon(Icon icn) { 61 image = Utilities.icon2Image(icn); 62 } 63 64 public Icon getIcon() { 65 return new ImageIcon (image); 66 } 67 68 69 public byte[] getBytes() throws IOException { 70 PixelGrabber pg = new PixelGrabber (image, 0, 0, 16, 16, false); 71 try { 72 pg.grabPixels(); 73 if ((pg.getStatus() & ImageObserver.ABORT) != 0) { 74 throw new IOException ("Cannot load image data"); 75 } 76 } catch (InterruptedException e) { 77 throw new IOException ("Loading image interrupted"); 78 } 79 Object obj = pg.getPixels(); 80 if (obj instanceof byte[]) { 81 return (byte[])obj; 82 } else { 83 return intToByteArray((int[])obj); 84 } 85 } 86 87 public static byte[] intToByteArray(int[] value) { 88 byte[] b = new byte[value.length * 4]; 89 for (int j = 0; j < b.length; j = j + 4) { 90 int val = value[j / 4]; 91 b[j] = (byte)(val >>> 24); 92 b[j + 1] = (byte)(val >> 16 & 0xff); 93 b[j + 2] = (byte)(val >> 8 & 0xff); 94 b[j + 3] = (byte)(val & 0xff); 95 } 96 return b; 97 } 98 } 99 | Popular Tags |