// Copyright 2006 by Basil Vandegriend.  All rights reserved.

package com.basilv.core.richui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.Icon;

import com.basilv.core.Assert;

/**
 * Represents an icon that resizes a supplied icon.
 *
 */
public class ResizedIcon implements Icon
{
	private static final long serialVersionUID = 1L;

	private Dimension size;
	private Icon wrappedIcon;
	
	private int xAdjust;
	private int yAdjust;
	
	public ResizedIcon(Dimension size, Icon icon) {
		Assert.notNull("size", size);
		Assert.notNull("icon", icon);
		Assert.isTrue(size.height >= icon.getIconHeight());
		Assert.isTrue(size.width >= icon.getIconWidth());
		
		this.size = size;
		this.wrappedIcon = icon;
		
		// Center 
		yAdjust = (size.height - icon.getIconHeight()) / 2;
		xAdjust = (size.width - icon.getIconWidth()) / 2; 
	}

	public int getIconHeight() {
		return size.height;
	}

	public int getIconWidth() {
		return size.width;
	}

	public void paintIcon(Component c, Graphics g, int x, int y) {
		wrappedIcon.paintIcon(c, g, x+xAdjust, y+yAdjust);
	}

}

