CompoundIcon.java :  » Source-Control » gruntspud » gruntspud » ui » icons » Java Open Source

Java Open Source » Source Control » gruntspud 
gruntspud » gruntspud » ui » icons » CompoundIcon.java
/*
 *  Gruntspud
 *
 *  Copyright (C) 2002 Brett Smith.
 *
 *  Written by: Brett Smith <t_magicthize@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library 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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package gruntspud.ui.icons;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.Icon;

/**
 * DOCUMENT ME!
 *
 * @author $author$
 */
public class CompoundIcon
    implements Icon {
    private Icon icon1;
    private Icon icon2;
    private int gap;
    private Insets margin;

    /**
     * Creates a new CompoundIcon object.
     *
     * @param icon1 DOCUMENT ME!
     * @param icon2 DOCUMENT ME!
     */
    public CompoundIcon(Icon icon1, Icon icon2) {
        this();
        setIcon1(icon1);
        setIcon2(icon2);
        margin = new Insets(0, 0, 0, 0);
    }

    /**
     * Creates a new CompoundIcon object.
     */
    public CompoundIcon() {
        gap = 2;
    }

    public void setGap(int gap) {
        this.gap = gap;
    }

    public void setMargin(Insets margin) {
        this.margin = margin;
    }

    /**
     * DOCUMENT ME!
     *
     * @param c DOCUMENT ME!
     * @param g DOCUMENT ME!
     * @param x DOCUMENT ME!
     * @param y DOCUMENT ME!
     */
    public void paintIcon(Component c, Graphics g, int x, int y) {
        if ( (icon1 != null) && (icon2 != null)) {
            icon1.paintIcon(c, g, 0 + margin.left,
                            ((getIconHeight() - icon1.getIconHeight()) / 2) + margin.top);
            icon2.paintIcon(c, g, icon1.getIconWidth() + gap + margin.left,
                            ((getIconHeight() - icon2.getIconHeight()) / 2) + margin.top);
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param icon1 DOCUMENT ME!
     */
    public void setIcon1(Icon icon1) {
        this.icon1 = icon1;
    }

    /**
     * DOCUMENT ME!
     *
     * @param icon2 DOCUMENT ME!
     */
    public void setIcon2(Icon icon2) {
        this.icon2 = icon2;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getIconWidth() {
        return ( ( (icon1 == null) || (icon2 == null)) ? 16
            : (icon1.getIconWidth() +
               gap + icon2.getIconWidth()) ) + margin.left + margin.right;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getIconHeight() {
        return ( ( (icon1 == null) || (icon2 == null)) ? 16
            : Math.max(icon1.getIconHeight(),
                       icon2.getIconHeight()) ) + margin.top + margin.bottom;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.