001: /*
002: * ComponentTextEffect.java
003: *
004: * Created on March 31, 2007, 11:32 AM
005: *
006: * Copyright 2006-2007 Nigel Hughes
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
009: * in compliance with the License. You may obtain a copy of the License at http://www.apache.org/
010: * licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
012: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
013: * governing permissions and limitations under the License.
014: */
015:
016: package com.blogofbug.swing.components.effects;
017:
018: import com.blogofbug.swing.components.DockPanel;
019: import java.awt.AlphaComposite;
020: import java.awt.Color;
021: import java.awt.Container;
022: import java.awt.Font;
023: import java.awt.FontMetrics;
024: import java.awt.Graphics2D;
025: import java.awt.geom.Rectangle2D;
026: import javax.swing.JComponent;
027:
028: /**
029: * TODO: Add API to change color of text
030: * @author nigel
031: */
032: public class ComponentTextEffect extends ComponentEffect implements
033: AlphaEffect {
034: private String text = "";
035: private Color background = Color.BLACK;
036: private Color foreground = Color.WHITE;
037: private float alpha = 1.0f;
038: private TextLocation side = TextLocation.SOUTH;
039:
040: public enum TextLocation {
041: NORTH, SOUTH
042: };
043:
044: /**
045: * A font used for reference purposes when evaluating the size of the rendered component
046: */
047: private static final Font reference = new Font("Arial", Font.BOLD,
048: 14);
049:
050: /** Creates a new instance of ComponentTextEffect */
051: public ComponentTextEffect(Container container,
052: JComponent component, EffectContainer effectContainer,
053: String text) {
054: super (container, component, effectContainer);
055: this .text = text;
056: }
057:
058: /** Creates a new instance of ComponentTextEffect */
059: public ComponentTextEffect(Container container,
060: JComponent component, EffectContainer effectContainer,
061: String text, TextLocation where) {
062: this (container, component, effectContainer, text);
063: this .side = where;
064: }
065:
066: public String getText() {
067: return text;
068: }
069:
070: public void setText(String text) {
071: this .text = text;
072: }
073:
074: /**
075: * This does nothing, the effect is always determined and painted automagically
076: */
077: public void initializeEffect() {
078:
079: }
080:
081: public Color getBackground() {
082: return background;
083: }
084:
085: public Color getForeground() {
086: return foreground;
087: }
088:
089: public void setBackground(Color background) {
090: this .background = background;
091: }
092:
093: public void setForeground(Color foreground) {
094: this .foreground = foreground;
095: }
096:
097: public void paintEffect(Graphics2D graphics) {
098: // Draw text if there is any...
099: if ((text.length() > 0) && (alpha > 0.0f)) {
100: graphics.setComposite(AlphaComposite.getInstance(
101: AlphaComposite.SRC_OVER, alpha));
102: Rectangle2D bounds = reference.getStringBounds(text,
103: graphics.getFontRenderContext());
104: double scaleFactor = (double) theComponent.getWidth()
105: / theComponent.getPreferredSize().getWidth();
106: scaleFactor = 10000.0;
107: double scaleFactor2 = (double) theComponent.getWidth()
108: / bounds.getWidth();
109: int fontSize = (int) Math.min(25.0 * scaleFactor,
110: 14.0 * scaleFactor2);
111: Font font = new Font("Arial", Font.BOLD, fontSize);
112: graphics.setFont(font);
113:
114: int red = background.getRed();
115: int green = background.getRed();
116: int blue = background.getRed();
117: graphics.setColor(new Color(red, green, blue, 96));
118: FontMetrics fm = graphics.getFontMetrics();
119: Rectangle2D rect = fm.getStringBounds(text, graphics);
120:
121: int dx = theComponent.getX()
122: + (theComponent.getWidth() - (int) font
123: .getStringBounds(text,
124: graphics.getFontRenderContext())
125: .getWidth()) / 2;
126: int dy = theComponent.getY() + theComponent.getHeight()
127: + fm.getAscent() + fm.getDescent();
128:
129: switch (side) {
130: case NORTH:
131: dy = theComponent.getY() + theComponent.getHeight()
132: + fm.getAscent() + fm.getDescent();
133: break;
134: }
135:
136: graphics.fillRoundRect(dx - (int) rect.getHeight() / 2, dy
137: - (int) graphics.getFontMetrics().getAscent(),
138: (int) rect.getWidth() + ((int) rect.getHeight()),
139: fm.getAscent() + fm.getDescent(), (int) rect
140: .getHeight(), (int) rect.getHeight());
141: graphics.setColor(foreground);
142: graphics.drawString(text, dx, dy);
143: }
144: }
145:
146: public float getAlpha() {
147: return alpha;
148: }
149:
150: public void setAlpha(float alpha) {
151: this.alpha = alpha;
152: }
153:
154: }
|