1 29 30 package nextapp.echo2.app; 31 32 import java.awt.Image ; 33 import java.awt.Toolkit ; 34 import java.awt.image.ColorModel ; 35 import java.awt.image.ImageObserver ; 36 import java.awt.image.MemoryImageSource ; 37 import java.awt.image.PixelGrabber ; 38 import java.io.IOException ; 39 import java.io.ObjectInputStream ; 40 import java.io.ObjectOutputStream ; 41 42 48 public class AwtImageReference 49 implements ImageReference { 50 51 private transient Image image; 52 private String id; 53 54 59 public AwtImageReference() { 60 this(null); 61 } 62 63 72 public AwtImageReference(Image image) { 73 super(); 74 this.image = image; 75 id = ApplicationInstance.generateSystemId(); 76 } 77 78 81 public boolean equals(Object o) { 82 if (!(o instanceof AwtImageReference)) { 83 return false; 84 } 85 AwtImageReference that = (AwtImageReference) o; 86 if (!(this.image == that.image || (this.image != null && this.image.equals(that.image)))) { 87 return false; 88 } 89 return true; 90 } 91 92 95 public Extent getHeight() { 96 if (image == null) { 97 return null; 98 } 99 int height = image.getHeight(null); 100 if (height > 0) { 101 return new Extent(height, Extent.PX); 102 } else { 103 return null; 104 } 105 } 106 107 110 public String getRenderId() { 111 return id; 112 } 113 114 122 public Image getImage() { 123 return image; 124 } 125 126 129 public Extent getWidth() { 130 if (image == null) { 131 return null; 132 } 133 int width = image.getWidth(null); 134 if (width > 0) { 135 return new Extent(width, Extent.PX); 136 } else { 137 return null; 138 } 139 } 140 141 144 private void readObject(ObjectInputStream in) 145 throws IOException , ClassNotFoundException { 146 in.defaultReadObject(); 147 148 int width = in.readInt(); 149 int height = in.readInt(); 150 int[] pixels = (int[]) in.readObject(); 151 152 if (pixels != null) { 153 Toolkit toolkit = Toolkit.getDefaultToolkit(); 154 ColorModel colorModel = ColorModel.getRGBdefault(); 155 image = toolkit.createImage(new MemoryImageSource (width, height, colorModel, pixels, 0, width)); 156 } 157 } 158 159 162 private void writeObject(ObjectOutputStream out) 163 throws IOException { 164 out.defaultWriteObject(); 165 166 int width = image.getWidth(null); 167 int height = image.getHeight(null); 168 169 out.writeInt(width); 170 out.writeInt(height); 171 172 if (image == null) { 173 out.writeObject(null); 174 } else { 175 int[] pixels = new int[width * height]; 176 try { 177 PixelGrabber pg = new PixelGrabber (image, 0, 0, width, height, pixels, 0, width); 178 pg.grabPixels(); 179 if ((pg.getStatus() & ImageObserver.ABORT) != 0) { 180 throw new IOException ("Unable to serialize java.awt.image: PixelGrabber aborted."); 181 } 182 } catch (InterruptedException ex) { 183 throw new IOException ("Unable to serialize java.awt.Image: PixelGrabber interrupted."); 184 } 185 out.writeObject(pixels); 186 } 187 } 188 } 189 | Popular Tags |