Java Image findImagefromClasspath(Class relToThisClass, String relImageName)

Here you can find the source of findImagefromClasspath(Class relToThisClass, String relImageName)

Description

find Imagefrom Classpath

License

Open Source License

Declaration

private static Image findImagefromClasspath(Class relToThisClass, String relImageName) 

Method Source Code

//package com.java2s;
/*************************************************************************
    //from ww w .j ava  2s.  c o m
Copyright (C) 2009 Grandite
    
This file is part of Open ModelSphere.
    
Open ModelSphere is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
or see http://www.gnu.org/licenses/.
    
You can redistribute and/or modify this particular file even under the
terms of the GNU Lesser General Public License (LGPL) as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
    
You should have received a copy of the GNU Lesser General Public License 
(LGPL) along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
or see http://www.gnu.org/licenses/.
    
You can reach Grandite at: 
    
20-1220 Lebourgneuf Blvd.
Quebec, QC
Canada  G2K 2G4
    
or
    
open-modelsphere@grandite.com
    
 **********************************************************************/

import java.awt.*;

import java.io.*;

import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.swing.ImageIcon;

public class Main {
    private static final String CLASSPATH = "java.class.path";

    private static Image findImagefromClasspath(Class relToThisClass, String relImageName) {
        Image image = null;

        String name = relToThisClass.getName();
        String path = name.substring(0, name.lastIndexOf('.'));
        path = path.replace('.', '/');
        path = path + relImageName.substring(1); //remove the '.' of the relative path 

        String classpath = System.getProperty(CLASSPATH);
        StringTokenizer st = new StringTokenizer(classpath, ";:");
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            try {
                ZipFile file = new ZipFile(token);
                ZipEntry entry = file.getEntry(path);
                if (entry != null) {
                    InputStream input = file.getInputStream(entry);
                    int size = (int) entry.getSize();
                    byte[] data = getImageFromStream(input, size);
                    ImageIcon icon = new ImageIcon(data);
                    image = icon.getImage();
                    break;
                } //end if

            } catch (IOException ex) {
                //ignore, and continue the iterations
            } //end try
        } //end while
        return image;
    }

    private static byte[] getImageFromStream(InputStream input, int size) {
        byte[] image = new byte[size];

        try {
            int read = input.read(image, 0, size);
        } catch (IOException ex) {
            image = null;
        }

        return image;

    }
}

Related

  1. displayImage(Image image)
  2. displayImage(Image img)
  3. displayImage(Image img, String mesg, int locx, int locy, int sizex, int sizey)
  4. divideImage(Object obj, int rows, int cols)
  5. ensureImageLoaded(Image img)
  6. generateImageVisualizationPanel(String path, int width, int height, int scaling)
  7. getChatLogoImage(int width, int height)
  8. getChatLogoImageBig(int width, int height)
  9. getColoredImage(Color color, int width, int height)