Java Swing Icon getClosestIcon(Collection icons, int height)

Here you can find the source of getClosestIcon(Collection icons, int height)

Description

From the given icons, chooses the icon taller than (if available) and closest to the given height.

License

Open Source License

Parameter

Parameter Description
icons the icons to choose from
height the desired height of the icon

Declaration

public static <I extends Icon> I getClosestIcon(Collection<I> icons, int height) 

Method Source Code

//package com.java2s;
/*/*from w  w w. j a va2 s.c o m*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

import java.util.*;

import javax.swing.*;

public class Main {
    /**
     * From the given icons, chooses the icon taller than (if available) and
     * closest to the given height.
     *
     * @param       icons
     *          the icons to choose from
     *
     * @param       height
     *          the desired height of the icon
     */
    public static <I extends Icon> I getClosestIcon(Collection<I> icons, int height) {

        I icon = null;

        if (icons != null) {
            int diff = 0;

            for (I i : icons) {
                int d = i.getIconHeight() - height;
                if (d == 0) {
                    return i;
                }

                if (icon == null || (diff < 0 && d > diff) || (diff > 0 && d < diff && d >= 0)) {

                    icon = i;
                    diff = d;
                }
            }
        }

        return icon;
    }
}

Related

  1. emptyIcon(final int width, final int height)
  2. getApplicationIcons()
  3. getBadgedIcon(Icon icon, Icon badge)
  4. getbtnIcon(String iconpath)
  5. getbtniconpresspress(String Iconpath1, String pressIconpath2, String pressIconpath3)
  6. getDefaultIcon()
  7. getDefaultIcon()
  8. getDefaultIcon()
  9. getEmptyIcon()