001: /*
002: * Copyright (c) 2001-2006 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.animation.components;
032:
033: import java.awt.Color;
034: import java.awt.Graphics;
035: import java.awt.Graphics2D;
036: import java.awt.RenderingHints;
037:
038: import javax.swing.JComponent;
039:
040: import com.jgoodies.animation.renderer.BasicTextRenderer;
041: import com.jgoodies.animation.renderer.HeightMode;
042:
043: /**
044: * A Swing text component that can change the text, x and y scaling,
045: * glyph space, x and y offset and alignment mode.
046: *
047: * @author Karsten Lentzsch
048: * @version $Revision: 1.1 $
049: *
050: * @see BasicTextRenderer
051: */
052: public final class BasicTextLabel extends JComponent {
053:
054: // Names of the bound bean properties *************************************
055:
056: public static final String PROPERTYNAME_COLOR = "color";
057: public static final String PROPERTYNAME_HEIGHT_MODE = "heightMode";
058: public static final String PROPERTYNAME_SCALE = "scale";
059: public static final String PROPERTYNAME_SCALE_X = "scaleX";
060: public static final String PROPERTYNAME_SCALE_Y = "scaleY";
061: public static final String PROPERTYNAME_SPACE = "space";
062: public static final String PROPERTYNAME_TEXT = "text";
063: public static final String PROPERTYNAME_OFFSET_X = "offsetX";
064: public static final String PROPERTYNAME_OFFSET_Y = "offsetY";
065:
066: /**
067: * Refers to the object that renders the text onto a graphics2D.
068: */
069: private final BasicTextRenderer renderer;
070:
071: // Instance Creation ******************************************************
072:
073: /**
074: * Constructs a animation text Swing label with an empty initial text.
075: */
076: public BasicTextLabel() {
077: this ("");
078: }
079:
080: /**
081: * Constructs a animation text Swing label for the given text.
082: *
083: * @param text the initial text to be displayed
084: */
085: public BasicTextLabel(String text) {
086: renderer = new BasicTextRenderer(text);
087: }
088:
089: // Public Accessors *******************************************************
090:
091: public Color getColor() {
092: return renderer.getColor();
093: }
094:
095: public HeightMode getHeightMode() {
096: return renderer.getHeightMode();
097: }
098:
099: public float getScale() {
100: return Math.max(getScaleX(), getScaleX());
101: }
102:
103: public float getScaleX() {
104: return renderer.getScaleX();
105: }
106:
107: public float getScaleY() {
108: return renderer.getScaleY();
109: }
110:
111: public float getSpace() {
112: return renderer.getSpace();
113: }
114:
115: public float getOffsetX() {
116: return renderer.getOffsetX();
117: }
118:
119: public float getOffsetY() {
120: return renderer.getOffsetY();
121: }
122:
123: public String getText() {
124: return renderer.getText();
125: }
126:
127: public void setColor(Color newColor) {
128: Color oldColor = getColor();
129: if (oldColor.equals(newColor))
130: return;
131: renderer.setColor(newColor);
132: firePropertyChange(PROPERTYNAME_COLOR, oldColor, newColor);
133: repaint();
134: }
135:
136: public void setHeightMode(HeightMode heightMode) {
137: HeightMode oldMode = getHeightMode();
138: renderer.setHeightMode(heightMode);
139: firePropertyChange(PROPERTYNAME_HEIGHT_MODE, oldMode,
140: heightMode);
141: repaint();
142: }
143:
144: public void setScale(float newScale) {
145: float oldScale = getScale();
146: renderer.setScaleX(newScale);
147: renderer.setScaleY(newScale);
148: firePropertyChange(PROPERTYNAME_SCALE, oldScale, newScale);
149: repaint();
150: }
151:
152: public void setScaleX(float newScaleX) {
153: float oldScaleX = getScaleX();
154: if (oldScaleX == newScaleX)
155: return;
156: float oldScale = getScale();
157: renderer.setScaleX(newScaleX);
158: firePropertyChange(PROPERTYNAME_SCALE_X, oldScaleX, newScaleX);
159: firePropertyChange(PROPERTYNAME_SCALE, oldScale, getScale());
160: repaint();
161: }
162:
163: public void setScaleY(float newScaleY) {
164: float oldScaleY = getScaleY();
165: if (oldScaleY == newScaleY)
166: return;
167: float oldScale = getScale();
168: renderer.setScaleY(newScaleY);
169: firePropertyChange(PROPERTYNAME_SCALE_Y, oldScaleY, newScaleY);
170: firePropertyChange(PROPERTYNAME_SCALE, oldScale, getScale());
171: repaint();
172: }
173:
174: public void setSpace(float newSpace) {
175: float oldSpace = getSpace();
176: if (oldSpace == newSpace)
177: return;
178: renderer.setSpace(newSpace);
179: firePropertyChange(PROPERTYNAME_SPACE, oldSpace, newSpace);
180: repaint();
181: }
182:
183: public void setOffsetX(float offsetX) {
184: float oldOffsetX = getOffsetX();
185: renderer.setOffsetX(offsetX);
186: firePropertyChange(PROPERTYNAME_OFFSET_X, oldOffsetX, offsetX);
187: repaint();
188: }
189:
190: public void setOffsetY(float offsetY) {
191: float oldOffsetY = getOffsetY();
192: renderer.setOffsetY(offsetY);
193: firePropertyChange(PROPERTYNAME_OFFSET_Y, oldOffsetY, offsetY);
194: repaint();
195: }
196:
197: public void setText(String newText) {
198: String oldText = getText();
199: if (oldText.equals(newText))
200: return;
201: renderer.setText(newText);
202: firePropertyChange(PROPERTYNAME_TEXT, oldText, newText);
203: repaint();
204: }
205:
206: // Painting ***************************************************************
207:
208: /**
209: * Paints the component. Enabled anti-aliasing and sets high quality hints,
210: * then renderers the component via the underlying renderer.
211: *
212: * @param g the Graphics object to render on
213: */
214: public void paintComponent(Graphics g) {
215: Graphics2D g2 = (Graphics2D) g;
216:
217: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
218: RenderingHints.VALUE_ANTIALIAS_ON);
219: g2.setRenderingHint(RenderingHints.KEY_RENDERING,
220: RenderingHints.VALUE_RENDER_QUALITY);
221:
222: renderer.setFont(getFont());
223: renderer.render(g2, getWidth(), getHeight());
224: }
225: }
|