Frames | No Frames |
1: /* ImageViewer.java -- Simple image display component. 2: Copyright (C) 2004 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: package gnu.javax.activation.viewers; 39: 40: import java.awt.Component; 41: import java.awt.Dimension; 42: import java.awt.Image; 43: import java.awt.Graphics; 44: import java.awt.MediaTracker; 45: import java.awt.Toolkit; 46: import java.io.ByteArrayOutputStream; 47: import java.io.InputStream; 48: import java.io.IOException; 49: 50: import javax.activation.CommandObject; 51: import javax.activation.DataHandler; 52: 53: /** 54: * Simple image display component. 55: * 56: * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> 57: * @version 1.0.2 58: */ 59: public class ImageViewer extends Component 60: implements CommandObject 61: { 62: 63: private Image image; 64: 65: /** 66: * Returns the preferred size for this component (the image size). 67: */ 68: public Dimension getPreferredSize() 69: { 70: Dimension ps = new Dimension(0, 0); 71: if (image != null) 72: { 73: ps.width = image.getWidth(this); 74: ps.height = image.getHeight(this); 75: } 76: return ps; 77: } 78: 79: public void setCommandContext(String verb, DataHandler dh) 80: throws IOException 81: { 82: // Read image into a byte array 83: InputStream in = dh.getInputStream(); 84: ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 85: byte[] buf = new byte[4096]; 86: for (int len = in.read(buf); len != -1; len = in.read(buf)) 87: bytes.write(buf, 0, len); 88: in.close(); 89: // Create and prepare the image 90: Toolkit toolkit = getToolkit(); 91: Image img = toolkit.createImage(bytes.toByteArray()); 92: try 93: { 94: MediaTracker tracker = new MediaTracker(this); 95: tracker.addImage(img, 0); 96: tracker.waitForID(0); 97: } 98: catch (InterruptedException e) 99: { 100: } 101: toolkit.prepareImage(img, -1, -1, this); 102: } 103: 104: /** 105: * Image bits arrive. 106: */ 107: public boolean imageUpdate(Image image, int flags, int x, int y, 108: int width, int height) 109: { 110: if ((flags & ALLBITS) != 0) 111: { 112: this.image = image; 113: invalidate(); 114: repaint(); 115: return false; 116: } 117: return ((flags & ERROR) == 0); 118: } 119: 120: /** 121: * Scale the image into this component's bounds. 122: */ 123: public void paint(Graphics g) 124: { 125: if (image != null) 126: { 127: Dimension is = new Dimension(image.getWidth(this), 128: image.getHeight(this)); 129: if (is.width > -1 && is.height > -1) 130: { 131: Dimension cs = getSize(); 132: g.drawImage(image, 0, 0, cs.width, cs.height, 133: 0, 0, is.width, is.height, this); 134: } 135: } 136: } 137: 138: }