Java ImageIcon Create createImageIcon(Class clazz, String pathToImage, int sizeX, int sizeY)

Here you can find the source of createImageIcon(Class clazz, String pathToImage, int sizeX, int sizeY)

Description

create Image Icon

License

Open Source License

Declaration

public static ImageIcon createImageIcon(Class clazz, String pathToImage, int sizeX, int sizeY) 

Method Source Code

//package com.java2s;
/* ESCAPE Software Copyright 2010 G2, Inc. - All rights reserved.
*
* ESCAPE is open source software distributed under GNU General Public License Version 3.  ESCAPE is not in the public domain 
* and G2, Inc. holds its copyright.  Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
    //from w  ww . jav  a 2 s.c o m
* 1. Redistributions of ESCAPE source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the ESCAPE Software distribution. 
* 3. Neither the name of G2, Inc. nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
    
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL G2, INC., THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
    
* You should have received a copy of the GNU General Public License Version 3 along with this program. 
* If not, see http://www.gnu.org/licenses/ for a copy.
*/

import java.awt.Image;
import java.io.ByteArrayOutputStream;

import java.io.InputStream;
import java.net.URL;

import javax.swing.ImageIcon;

public class Main {
    public static ImageIcon createImageIcon(Class clazz, String pathToImage) {
        ImageIcon ret = null;
        //log.debug("pathToImage=" + (pathToImage == null ? "null" : pathToImage));
        if (pathToImage != null) {
            try {
                URL url = clazz.getResource(pathToImage);
                //    log.debug("URL=" + (url == null ? "null" : url.toString()));
                ret = new ImageIcon(url);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return ret;
    }

    public static ImageIcon createImageIcon(Class clazz, String pathToImage, int sizeX, int sizeY) {
        ImageIcon ret = null;
        //log.debug("pathToImage=" + (pathToImage == null ? "null" : pathToImage));
        if (pathToImage != null) {
            try {
                InputStream is = clazz.getResourceAsStream(pathToImage);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int bytesRead = 0;
                while ((bytesRead = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, bytesRead);
                }
                ret = new ImageIcon(baos.toByteArray());
                //                URL url = clazz.getResource(pathToImage);
                //                System.out.println("URL=" + (url == null ? "null" : url.toString()));
                //                ret = new ImageIcon(url);

                Image i = ret.getImage();

                Image ni = i.getScaledInstance(sizeX, sizeY, java.awt.Image.SCALE_SMOOTH);

                ret = new ImageIcon(ni);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return ret;
    }
}

Related

  1. createImageIcon(Class clazz, String path)
  2. createImageIcon(Class theClass, String path, String description)
  3. createImageIcon(Class bezugsklasse, String path)
  4. createImageIcon(ClassLoader classloader, String path, String description)
  5. createImageIcon(File f, String description)