Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2009-2011, EzWare
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * Redistributions of source code must retain the above copyright notice,
  * this list of conditions and the following disclaimer.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 distribution.Neither the name of the
  * EzWare nor the names of its 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 THE COPYRIGHT OWNER 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.
  *
  */
 
 package org.oxbow.swingbits.util.swing;
 
 import java.awt.*;
 import javax.swing.*;

The CompoundIcon will paint two, or more, Icons as a single Icon. The Icons are painted in the order in which they are added. The Icons can be laid out
  • Horizontally
  • Vertically
  • Stacked
 
 public class CompoundIcon implements Icon {
 	public enum Layout {
 		HORIZONTAL, VERTICAL, STACKED;
 	}
 
 	public final static float TOP = 0.0f;
 	public final static float LEFT = 0.0f;
 	public final static float CENTER = 0.5f;
 	public final static float BOTTOM = 1.0f;
 	public final static float RIGHT = 1.0f;
 
 	private Icon[] icons;
 
 	private Layout layout;
 
 	private int gap;
 
 	private float alignmentX = ;
 	private float alignmentY = ;

Convenience constructor for creating a CompoundIcon where the icons are laid out horizontally, the gap is 0 and the X/Y alignments will default to CENTER.

Parameters:
icons the Icons to be painted as part of the CompoundIcon
 
 	public CompoundIcon(Icon... icons) {
 		this(.icons);
 	}

Convenience constructor for creating a CompoundIcon where the gap is 0 and the X/Y alignments will default to CENTER.

Parameters:
layout the layout used to lay out the icons for painting.
icons the Icons to be painted as part of the CompoundIcon
 
 	public CompoundIcon(Layout layoutIcon... icons) {
 		this(layout, 0, icons);
 	}

Convenience constructor for creating a CompoundIcon where the X/Y alignments will default to CENTER.

Parameters:
layout the layout used to lay out the icons for painting
gap the gap between the icons
icons the Icons to be painted as part of the CompoundIcon
	public CompoundIcon(Layout layoutint gapIcon... icons) {
		this(layoutgapicons);
	}

Create a CompoundIcon specifying all the properties.

Parameters:
layout the layout used to lay out the icons for painting
gap the gap between the icons
alignmentX the X alignment of the icons. Common values are LEFT, CENTER, RIGHT. Can be any value between 0.0 and 1.0
alignmentY the Y alignment of the icons. Common values are TOP, CENTER, BOTTOM. Can be any value between 0.0 and 1.0
icons the Icons to be painted as part of the CompoundIcon
	public CompoundIcon(Layout layoutint gapfloat alignmentX,
			float alignmentYIcon... icons) {
		this. = layout;
		this. = gap;
		this. = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f
alignmentX;
		this. = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f
alignmentY;
		for (int i = 0; i < icons.lengthi++) {
			if (icons[i] == null) {
				throw new IllegalArgumentException("Icon (" + i
") cannot be null");
			}
		}
		this. = icons;
	}

Get the layout along which each icon is painted.

Returns:
the layout
	public Layout getLayout() {
		return ;
	}

Get the gap between each icon

Returns:
the gap in pixels
	public int getGap() {
		return ;
	}

Get the alignment of the icon on the x-layout

Returns:
the alignment
	public float getAlignmentX() {
		return ;
	}

Get the alignment of the icon on the y-layout

Returns:
the alignment
	public float getAlignmentY() {
		return ;
	}

Get the number of Icons contained in this CompoundIcon.

Returns:
the total number of Icons
	public int getIconCount() {
		return .;
	}

Get the Icon at the specified index.

Parameters:
index the index of the Icon to be returned
Returns:
the Icon at the specified index
Throws:
java.lang.IndexOutOfBoundsException if the index is out of range
	public Icon getIcon(int index) {
		return [index];
	}
	// ///// Icon Interface /////////////////////////////
Gets the width of this icon.

Returns:
the width of the icon in pixels.
	public int getIconWidth() {
		int width = 0;
		// Add the width of all Icons while also including the gap
		if ( == .) {
			width += (. - 1) * ;
			for (Icon icon : )
				width += icon.getIconWidth();
else { // Just find the maximum width
			for (Icon icon : )
				width = Math.max(widthicon.getIconWidth());
		}
		return width;
	}

Gets the height of this icon.

Returns:
the height of the icon in pixels.
	public int getIconHeight() {
		int height = 0;
		// Add the height of all Icons while also including the gap
		if ( == .) {
			height += (. - 1) * ;
			for (Icon icon : )
				height += icon.getIconHeight();
else // Just find the maximum height
		{
			for (Icon icon : )
				height = Math.max(heighticon.getIconHeight());
		}
		return height;
	}

Paint the icons of this compound icon at the specified location

Parameters:
c The component on which the icon is painted
g the graphics context
x the X coordinate of the icon's top-left corner
y the Y coordinate of the icon's top-left corner
	public void paintIcon(Component cGraphics gint xint y) {
		if ( == .) {
			int height = getIconHeight();
			for (Icon icon : ) {
				int iconY = getOffset(heighticon.getIconHeight(), );
				icon.paintIcon(cgxy + iconY);
				x += icon.getIconWidth() + ;
			}
else if ( == .) {
			int width = getIconWidth();
			for (Icon icon : ) {
				int iconX = getOffset(widthicon.getIconWidth(), );
				icon.paintIcon(cgx + iconXy);
				y += icon.getIconHeight() + ;
			}
else  {// must be Z_layout
			int width = getIconWidth();
			int height = getIconHeight();
			for (Icon icon : ) {
				int iconX = getOffset(widthicon.getIconWidth(), );
				int iconY = getOffset(heighticon.getIconHeight(), );
				icon.paintIcon(cgx + iconXy + iconY);
			}
		}
	}
	/*
	 * When the icon value is smaller than the maximum value of all icons the
	 * icon needs to be aligned appropriately. Calculate the offset to be used
	 * when painting the icon to achieve the proper alignment.
	 */
	private int getOffset(int maxValueint iconValuefloat alignment) {
		float offset = (maxValue - iconValue) * alignment;
		return Math.round(offset);
	}
New to GrepCode? Check out our FAQ X