1 29 38 39 package org.planetamessenger.ui; 40 41 import java.util.jar.*; 42 import java.awt.*; 43 import java.io.*; 44 import javax.swing.*; 45 46 47 public class JImage { 48 49 static final int IMAGE = 0; 50 static final int ICON = 1; 51 52 53 57 public JImage() { 58 59 } 60 61 66 static public Image loadImageFromJar( String strJarFile, String strImageFile ) { 67 68 return ( Image ) loadObjectFromJar( strJarFile, strImageFile, IMAGE ); 69 } 70 71 76 static public ImageIcon loadIconFromJar( String strJarFile, String strIconFile ) { 77 78 return ( ImageIcon ) loadObjectFromJar( strJarFile, strIconFile, ICON ); 79 } 80 81 87 static Object loadObjectFromJar( String strJarFile, String strObjectFile, int nObjectType ) { 88 89 try { 90 JarFile iconsJar = new JarFile( strJarFile ); 91 JarEntry iconEntry = iconsJar.getJarEntry( strObjectFile ); 92 93 94 if( iconEntry != null ) { 95 InputStream inStream = iconsJar.getInputStream( iconEntry ); 96 int nLen = ( int ) iconEntry.getSize(); 97 byte imageBytes[] = new byte[nLen]; 98 int nOffset = 0; 99 int nLoaded = 0; 100 101 102 while( ( nLoaded < nLen ) && ( inStream != null ) ) { 103 nLen-=nLoaded; 104 nOffset+=nLoaded; 105 106 nLoaded = inStream.read( imageBytes, nOffset, nLen ); 107 108 if( nLoaded == -1 ) { 109 inStream.close(); 110 return null; 111 } 112 } 113 114 inStream.close(); 115 116 switch( nObjectType ) { 117 118 case IMAGE : return Toolkit.getDefaultToolkit().createImage( imageBytes ); 119 120 case ICON : return new ImageIcon( imageBytes ); 121 122 default : return null; 123 } 124 } 125 else 126 return null; 127 128 } catch( java.io.IOException e ) { 129 System.err.println( "JResources.loadObjectFromJar() - " + e ); 130 return null; 131 } 132 } 133 } 134 135 | Popular Tags |