KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > animation > components > BasicTextLabel


1 /*
2  * Copyright (c) 2001-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.animation.components;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Graphics2D JavaDoc;
36 import java.awt.RenderingHints JavaDoc;
37
38 import javax.swing.JComponent JavaDoc;
39
40 import com.jgoodies.animation.renderer.BasicTextRenderer;
41 import com.jgoodies.animation.renderer.HeightMode;
42
43 /**
44  * A Swing text component that can change the text, x and y scaling,
45  * glyph space, x and y offset and alignment mode.
46  *
47  * @author Karsten Lentzsch
48  * @version $Revision: 1.3 $
49  */

50 public final class BasicTextLabel extends JComponent JavaDoc {
51
52     private final BasicTextRenderer renderer;
53
54     /**
55      * Constructs a animation text Swing label for the given text.
56      *
57      * @param text the initial text to be displayed
58      */

59     public BasicTextLabel(String JavaDoc text) {
60         renderer = new BasicTextRenderer(text);
61     }
62
63     public Color JavaDoc getColor() {
64         return renderer.getColor();
65     }
66     
67     public HeightMode getHeightMode() {
68         return renderer.getHeightMode();
69     }
70     
71     public float getScaleX() {
72         return renderer.getScaleX();
73     }
74     
75     public float getScaleY() {
76         return renderer.getScaleY();
77     }
78     
79     public float getSpace() {
80         return renderer.getSpace();
81     }
82     
83     public float getOffsetX() {
84         return renderer.getOffsetX();
85     }
86     
87     public float getOffsetY() {
88         return renderer.getOffsetY();
89     }
90     
91     public String JavaDoc getText() {
92         return renderer.getText();
93     }
94
95     public void setColor(Color JavaDoc color) {
96         renderer.setColor(color);
97         repaint();
98     }
99
100     public void setHeightMode(HeightMode heightMode) {
101         renderer.setHeightMode(heightMode);
102     }
103
104     public void setScale(float scale) {
105         renderer.setScaleX(scale);
106         renderer.setScaleY(scale);
107         repaint();
108     }
109
110     public void setScaleX(float scaleX) {
111         renderer.setScaleX(scaleX);
112         repaint();
113     }
114
115     public void setScaleY(float scaleY) {
116         renderer.setScaleY(scaleY);
117         repaint();
118     }
119
120     public void setSpace(float space) {
121         renderer.setSpace(space);
122         repaint();
123     }
124
125     public void setOffsetX(float offsetX) {
126         renderer.setOffsetX(offsetX);
127         repaint();
128     }
129
130     public void setOffsetY(float offsetY) {
131         renderer.setOffsetY(offsetY);
132         repaint();
133     }
134
135     public void setText(String JavaDoc newText) {
136         renderer.setText(newText);
137         repaint();
138     }
139
140     /**
141      * Paints the component. Enabled anti-aliasing and sets high quality hints,
142      * then renderers the component via the underlying renderer.
143      *
144      * @param g the Graphics object to render on
145      */

146     public void paintComponent(Graphics JavaDoc g) {
147         Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
148
149         g2.setRenderingHint(
150             RenderingHints.KEY_ANTIALIASING,
151             RenderingHints.VALUE_ANTIALIAS_ON);
152         g2.setRenderingHint(
153             RenderingHints.KEY_RENDERING,
154             RenderingHints.VALUE_RENDER_QUALITY);
155
156         renderer.setFont(getFont());
157         renderer.render(g2, getWidth(), getHeight());
158     }
159 }
Popular Tags