Java ImageIcon loadJavaInternal(ImageIcon r, Dimension d, boolean noStretch)

Here you can find the source of loadJavaInternal(ImageIcon r, Dimension d, boolean noStretch)

Description

load Java Internal

License

Open Source License

Declaration

private static ImageIcon loadJavaInternal(ImageIcon r, Dimension d, boolean noStretch) 

Method Source Code


//package com.java2s;
/*//from   ww  w  .j av a 2  s.  c o m
*  Gallery Remote - a File Upload Utility for Gallery
*
*  Gallery - a web based photo album viewer and editor
*  Copyright (C) 2000-2001 Bharat Mediratta
*
*  This program 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 2 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.
*/

import javax.swing.*;

import java.awt.*;

public class Main {
    private static ImageIcon loadJavaInternal(ImageIcon r, Dimension d, boolean noStretch) {
        Image scaled = null;
        Dimension newD = getSizeKeepRatio(new Dimension(r.getIconWidth(), r.getIconHeight()), d, noStretch);

        if (newD == null) {
            return r;
        }

        scaled = r.getImage().getScaledInstance(newD.width, newD.height, Image.SCALE_FAST);

        r.getImage().flush();
        r.setImage(scaled);
        return r;
        /*Iterator iter = ImageIO.getImageReaders(iis);
        if (!iter.hasNext()) {
        return null;
        }
            
        ImageReader reader = (ImageReader)iter.next();
        ImageReadParam param = reader.getDefaultReadParam();
        reader.setInput(iis, true, false);
            
        IIOMetadata metadata = reader.getImageMetadata(0);
        String names[] = metadata.getMetadataFormatNames();
        for (int i = 0; i < names.length; i++) {
        displayMetadata(metadata.getAsTree(names[i]));
        }
            
        param.setSourceRenderSize(d);
            
        BufferedImage image = (BufferedImage) reader.readAsRenderedImage(0, param);
        return new ImageIcon(image);*/
    }

    public static Dimension getSizeKeepRatio(Dimension source, Dimension target, boolean noStretch) {
        if (noStretch && target.width > source.width && target.height > source.height) {
            return null;
        }

        Dimension result = new Dimension();

        float sourceRatio = Math.abs((float) source.width / source.height);
        float targetRatio = Math.abs((float) target.width / target.height);

        if (Math.abs(targetRatio) > Math.abs(sourceRatio)) {
            result.height = target.height;
            result.width = (int) (target.height * sourceRatio * (target.height * target.width > 0 ? 1 : -1));
        } else {
            result.width = target.width;
            result.height = (int) (target.width / sourceRatio * (target.height * target.width > 0 ? 1 : -1));
        }

        return result;
    }
}

Related

  1. imageFlip(int w, int h, ImageIcon... icons)
  2. imageToBytes(ImageIcon icon)
  3. imageWithBackground(ImageIcon icon, Color colorBg)
  4. initImageIcon(Class cl)
  5. isValidImg(ImageIcon img)
  6. makeDisabledImage(ImageIcon in)
  7. paintWithWatermarkHelper(JComponent component, ImageIcon watermark, Graphics g)
  8. parseImageIcon(Image image)
  9. prepareImage(ImageIcon icon, int x, int y)