1 24 25 26 package swingwtx.swing; 27 28 import java.io.ByteArrayInputStream ; 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.net.URL ; 32 33 import swingwt.awt.Component; 34 import swingwt.awt.Graphics; 35 36 37 public class ImageIcon implements Icon { 38 39 private String description = null; 40 private swingwt.awt.Image image = null; 41 42 43 public ImageIcon() { 44 } 45 46 public ImageIcon(swingwt.awt.Image image) { 47 this.image = image; 48 } 49 50 public ImageIcon(swingwt.awt.Image image, String description) { 51 this.image = image; 52 setDescription(description); 53 } 54 55 public ImageIcon(String filename) { 56 this(filename, ""); 57 } 58 59 public ImageIcon(String filename, String description) { 60 try { 61 image = new swingwt.awt.Image(); 62 image.image = new org.eclipse.swt.graphics.Image(SwingWTUtils.getDisplay(), 63 new FileInputStream (new File (filename))); 64 setDescription(description); 65 } catch (Exception e) { e.printStackTrace(); } 66 } 67 68 public ImageIcon(URL location) { 69 try { 70 image = new swingwt.awt.Image(); 71 image.image = new org.eclipse.swt.graphics.Image(SwingWTUtils.getDisplay(), location.openStream()); 72 } catch (Exception e) { e.printStackTrace(); } 73 } 74 75 public ImageIcon(URL location, String description) { 76 this(location); 77 setDescription(description); 78 } 79 80 public ImageIcon(byte[] data) { 81 this(data, ""); 82 } 83 84 public ImageIcon(byte[] data, String description) { 85 try { 86 image = new swingwt.awt.Image(); 87 image.image = new org.eclipse.swt.graphics.Image(SwingWTUtils.getDisplay(), 88 new ByteArrayInputStream (data)); 89 setDescription(description); 90 } 91 catch (Exception e) { 92 e.printStackTrace(); 93 } 94 } 95 96 97 101 public java.lang.String getDescription() { 102 return description; 103 } 104 105 109 public void setDescription(java.lang.String description) { 110 this.description = description; 111 } 112 113 117 public swingwt.awt.Image getImage() { 118 return image; 119 } 120 121 125 public void setImage(swingwt.awt.Image image) { 126 this.image = image; 127 } 128 129 public int getIconHeight() { 130 if (image == null) return 0; 131 if (image.image == null) return 0; 132 return image.image.getBounds().height; 133 } 134 135 public int getIconWidth() { 136 if (image == null) return 0; 137 if (image.image == null) return 0; 138 return image.image.getBounds().width; 139 } 140 141 public void paintIcon(Component c, Graphics g, int x, int y) { 142 if (image != null) 143 g.drawImage(image, x, y, null); 144 } 145 146 } 147 | Popular Tags |