1 package org.telscenter.pas.common.ui.button;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Font;
6 import java.awt.FontMetrics;
7 import java.awt.GradientPaint;
8 import java.awt.Graphics;
9 import java.awt.Graphics2D;
10 import java.awt.Toolkit;
11
12 import javax.swing.Icon;
13 import javax.swing.SwingUtilities;
14
15 public class GradientTextIcon implements Icon {
16
17
18 public static final int VERTICAL = 0;
19 public static final int HORIZONTAL = 1;
20 private Color color;
21 private int width;
22 private int height;
23 private FontMetrics fm;
24 private String text;
25 private Font font;
26 private Color startColor;
27 private Color endColor;
28 private int orientation;
29 private Graphics graphics;
30 private static final int DEFAULT_WIDTH = 10;
31 private static final int DEFAULT_HEIGHT = 10;
32 public GradientTextIcon(Font font,String text,Color startColor, Color endColor, int orietation) {
33
34 this.font = font;
35 this.fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
36 this.text = text;
37 this.width = SwingUtilities.computeStringWidth(fm, text);
38 this.height = fm.getHeight();
39 this.startColor = startColor;
40 this.endColor = endColor;
41 this.orientation = orietation;
42
43 }
44
45 public GradientTextIcon (Color color, int width, int height) {
46 this.color = color;
47 this.width = width;
48 this.height = height;
49 }
50
51 public int getIconHeight() {
52 return height;
53 }
54 public int getIconWidth() {
55 return width;
56 }
57 public Color getIconColor() {
58 return color;
59 }
60 public void paintIcon(Component c, Graphics g, int x, int y) {
61 this.graphics = g;
62 g.translate (x, y);
63 g.setFont(font);
64
65 GradientPaint grad;
66 if( this.orientation == VERTICAL)
67 grad = new GradientPaint(0, 0, startColor, 0, height, endColor, false);
68 else
69 grad = new GradientPaint(x, 0,startColor, width, 0,endColor, false);
70
71 Graphics2D g2 = (Graphics2D) g;
72 g2.setPaint(grad);
73 g.drawString(this.text,0, fm.getLeading() + fm.getAscent());
74 g.translate (-x, -y);
75 }
76 public Graphics getGraphics() {
77 return this.graphics;
78 }
79 }