1 21 22 27 28 package com.sun.activation.viewers; 29 30 import java.awt.*; 31 import java.io.*; 32 import java.beans.*; 33 import javax.activation.*; 34 35 public class ImageViewer extends Panel implements CommandObject { 36 private ImageViewerCanvas canvas = null; 38 39 private Image image = null; 42 private DataHandler _dh = null; 43 44 private boolean DEBUG = false; 45 48 public ImageViewer(){ 49 50 canvas = new ImageViewerCanvas(); 52 add(canvas); 53 } 54 58 public void setCommandContext(String verb, DataHandler dh) throws IOException{ 59 _dh = dh; 60 this.setInputStream( _dh.getInputStream() ); 61 } 62 64 68 private void setInputStream(InputStream ins) throws IOException { 69 MediaTracker mt = new MediaTracker(this); 70 int bytes_read = 0; 71 byte data[] = new byte[1024]; 72 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 73 74 while((bytes_read = ins.read(data)) >0) 75 baos.write(data, 0, bytes_read); 76 ins.close(); 77 78 image = getToolkit().createImage(baos.toByteArray()); 80 81 mt.addImage(image, 0); 82 83 try { 84 mt.waitForID(0); 85 mt.waitForAll(); 86 if(mt.statusID(0, true ) != MediaTracker.COMPLETE){ 87 System.out.println("Error occured in image loading = " + 88 mt.getErrorsID(0)); 89 90 } 91 92 } 93 catch(InterruptedException e) { 94 throw new IOException("Error reading image data"); 95 } 96 97 canvas.setImage(image); 98 if(DEBUG) 99 System.out.println("calling invalidate"); 100 101 } 102 public void addNotify(){ 104 super.addNotify(); this.invalidate(); 106 this.validate(); 107 this.doLayout(); 108 } 109 public Dimension getPreferredSize(){ 111 return canvas.getPreferredSize(); 112 } 113 114 } 115 116 117 118 119 120 121 122 123 124 125 126 | Popular Tags |