From 612166a5724c5b82fea7183a672bef135dfa9a67 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 16 May 2002 02:58:59 +0000 Subject: [PATCH] Created an Icon implementation that can be used to render sprites anywhere that icons can be rendered. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1365 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/media/sprite/SpriteIcon.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/java/com/threerings/media/sprite/SpriteIcon.java diff --git a/src/java/com/threerings/media/sprite/SpriteIcon.java b/src/java/com/threerings/media/sprite/SpriteIcon.java new file mode 100644 index 000000000..808c56f84 --- /dev/null +++ b/src/java/com/threerings/media/sprite/SpriteIcon.java @@ -0,0 +1,69 @@ +// +// $Id: SpriteIcon.java,v 1.1 2002/05/16 02:58:59 mdb Exp $ + +package com.threerings.media.sprite; + +import java.awt.Component; +import java.awt.Graphics; +import java.awt.Graphics2D; + +import javax.swing.Icon; + +/** + * Implements the icon interface, using a {@link Sprite} to render the + * icon image. + */ +public class SpriteIcon implements Icon +{ + /** + * Creates a sprite icon that will use the supplied sprite to render + * itself. This sprite should not be used for anything else while + * being used in this icon because it will be "moved" when the icon is + * rendered. The sprite's origin will be set to the bottom center of + * the label. If this is undesirable, the origin can be offset via + * {@link #setOriginOffsets}. + */ + public SpriteIcon (Sprite sprite) + { + _sprite = sprite; + } + + /** + * Origin offsets to use when rendering the icon (useful for tweaking + * when trying to get sprites that weren't designed to be stuffed into + * labels to behave appropriately). + */ + public void setOriginOffsets (int offx, int offy) + { + _offx = offx; + _offy = offy; + } + + // documentation inherited from interface + public void paintIcon (Component c, Graphics g, int x, int y) + { + // move the sprite to a "location" that is in the bottom center of + // our bounds (plus whatever offsets we have) + _sprite.setLocation(x + _sprite.getWidth()/2 + _offx, + y + _sprite.getHeight() + _offy); + _sprite.paint((Graphics2D)g); + } + + // documentation inherited from interface + public int getIconWidth () + { + return _sprite.getWidth(); + } + + // documentation inherited from interface + public int getIconHeight () + { + return _sprite.getHeight(); + } + + /** The sprite used to render this icon. */ + protected Sprite _sprite; + + /** Origin offsets to use when rendering the icon. */ + protected int _offx, _offy; +}