1 10 11 12 33 34 package com.oop.util; 35 36 import java.awt.*; 37 import java.util.*; 38 import java.util.zip.*; 39 import java.io.*; 40 import java.net.*; 41 import java.awt.image.*; 42 import com.sun.java.swing.*; 43 44 public class ImageExtractor { 45 final static Object nullObject = new Object (); 46 final static private Hashtable images = new Hashtable(); 47 48 51 Object hashTableIndex; 52 53 64 public ImageExtractor(String zipFileName) { 65 if (!(zipFileName.toLowerCase().endsWith(".zip") || 66 zipFileName.toLowerCase().endsWith(".jar"))) { 67 throw new RuntimeException ("Invalid extension in image extractor"); 68 } 69 File f = new File(zipFileName); 70 if (!f.exists()) { 71 System.out.println("Could not find the " + zipFileName + " file"); 72 System.out.println(" please modify your jbot.properties file to locate this image file correctly."); 73 System.out.println(" This file is typically found in your current directory or specified by a -proploc "); 74 System.out.println(" parameter on the command line. "); 75 com.sun.java.swing.JFileChooser fc = new com.sun.java.swing.JFileChooser(); 76 fc.setName("Find the image file : " + zipFileName); 77 Msg.getInstance().MBox("Could not find image library, please choose\r\n choosing a file here will update your properties (jbot.properties) \r\n to reflect the current images.jar file.\r\nThis file is typically found (during development) in the <jbot home>/images/ directory."); 78 79 if (fc.showOpenDialog(null) == -1) { 80 hashTableIndex = null; 81 } else { 82 File f1 = new File(fc.getSelectedFile().getAbsolutePath()); 83 if (!f1.exists()) { 84 hashTableIndex = null; 85 } else { 86 hashTableIndex = fc.getSelectedFile().getAbsolutePath(); 87 } 88 com.oop.fwork.Global.getFramework().addToProperties("0.image.category.path", hashTableIndex.toString()); 89 } 90 } else 91 hashTableIndex = zipFileName; 92 } 93 94 98 public ImageExtractor(URL zipFileURL) { 99 if (!(zipFileURL.getFile().toLowerCase().endsWith(".zip") || 100 zipFileURL.getFile().toLowerCase().endsWith(".jar"))) { 101 throw new RuntimeException ("Invalid extension in image extractor"); 102 } 103 hashTableIndex = zipFileURL; 104 } 105 106 109 public ImageIcon extractImageIcon(String ImageName) { 110 return extractImageIcon(ImageName, true); 111 } 112 113 117 public ImageIcon extractDisabledImageIcon(String IconName) { 118 Image i = null; 119 try { 120 i = extractImage(IconName); 121 } catch (Exception e) { 122 return null; 123 } 124 if (i == null) { 125 return null; 126 } else { 127 return new ImageIcon(com.sun.java.swing.GrayFilter.createDisabledImage(i)); 128 } 129 } 130 131 135 public ImageIcon extractImageIcon(String ImageName, boolean cache) { 136 Image i = extractImage(ImageName, cache); 137 if (i == null) { 138 return null; 139 } else { 140 return new ImageIcon(i); 141 } 142 } 143 144 145 148 public Image extractImage(String ImageName) { 149 return extractImage(ImageName, true); 150 } 151 152 165 public Image extractImage(String ImageName, boolean cache) { 166 if (hashTableIndex == null) 169 return null; 170 171 try { 172 if (images.containsKey(hashTableIndex.toString() + ImageName)) { 175 Object o = images.get(hashTableIndex.toString() + ImageName); 176 if (o == nullObject) 177 return null; 178 else 179 return (Image)o; 180 } 181 182 InputStream inputStream = null; 183 if (hashTableIndex instanceof String ) { 184 inputStream = new FileInputStream((String )hashTableIndex); 185 } else if (hashTableIndex instanceof URL) { 186 URL u = (URL)hashTableIndex; 187 inputStream = u.openStream(); 188 } 189 190 if (inputStream == null) throw new Exception ("No image archive file/url was specified"); 193 194 ZipInputStream zstream = new ZipInputStream(inputStream); 196 ZipEntry ze = null; 197 while (true) { 198 if (ze != null) { 200 ze = null; 201 zstream.closeEntry(); 202 } 203 ze = zstream.getNextEntry(); 204 if (ze == null) { 205 break; 207 } else if ( ze.getName().toUpperCase().equals(ImageName.toUpperCase())) { 208 break; 209 } 210 } 211 212 if (ze == null) { 214 images.put(hashTableIndex.toString() + ImageName, nullObject); 215 return null; 216 } 217 218 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 220 221 byte buffer[] = new byte[1024]; 222 223 while (true) { 225 int nRead = zstream.read(buffer, 0, buffer.length); 226 if (nRead <= 0) { 227 break; 228 } 229 baos.write(buffer, 0, nRead); 230 } 231 232 zstream.closeEntry(); 236 inputStream.close(); 237 238 Image output = 240 Toolkit.getDefaultToolkit().createImage(baos.toByteArray()); 241 242 baos.close(); 244 245 if (cache) 246 images.put(hashTableIndex.toString() + ImageName, output); 247 return output; 249 } catch (Exception e) { 250 e.printStackTrace(); 251 return null; 252 } 253 } 254 255 } 256 | Popular Tags |