KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > ui > JImage


1 /*
2     =========================================================================
3     Package ui - Implements the ui package.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12     
13     This library is free software; you can redistribute it and/or
14     modify it under the terms of the GNU Lesser General Public
15     License as published by the Free Software Foundation; either
16     version 2.1 of the License, or (at your option) any later version.
17
18     This library is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21     Lesser General Public License for more details.
22
23     You should have received a copy of the GNU Lesser General Public
24     License along with this library; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JImage.java,v 1.6 2007/01/28 17:39:22 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.6 $
35  * $State: Exp $
36  *
37  */

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   /**
54    * Creates and initializes the
55    * JImage class.
56    */

57   public JImage() {
58     
59   }
60
61   /**
62    * Load a image from jar file.
63    * @param strJarFile The jar owner of image;
64    * @param strImageFile The image that will be loaded;
65    */

66   static public Image loadImageFromJar( String JavaDoc strJarFile, String JavaDoc strImageFile ) {
67     
68     return ( Image ) loadObjectFromJar( strJarFile, strImageFile, IMAGE );
69   }
70
71   /**
72    * Load a icon from jar file.
73    * @param strJarFile The jar owner of icon;
74    * @param strIconFile The icon that will be loaded;
75    */

76   static public ImageIcon loadIconFromJar( String JavaDoc strJarFile, String JavaDoc strIconFile ) {
77     
78     return ( ImageIcon ) loadObjectFromJar( strJarFile, strIconFile, ICON );
79   }
80
81   /**
82    * Load an object from jar file.
83    * @param strJarFile The jar owner of object;
84    * @param strObjectFile The object that will be loaded;
85    * @param nObjectType The object type that will be loaded;
86    */

87   static Object JavaDoc loadObjectFromJar( String JavaDoc strJarFile, String JavaDoc 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 JavaDoc e ) {
129       System.err.println( "JResources.loadObjectFromJar() - " + e );
130       return null;
131     }
132   }
133 }
134
135 // JImage class
136
Popular Tags