01: package com.memoire.vainstall.xui;
02:
03: import java.awt.*;
04: import javax.swing.*;
05: import javax.swing.border.*;
06:
07: public class XuiTitle extends JComponent {
08: public final static int LEFT = 0;
09: public final static int CENTER = 1;
10: public final static int RIGHT = 2;
11:
12: private String text_;
13: private int alignment_;
14:
15: public XuiTitle(String _text) {
16: this (_text, CENTER);
17: }
18:
19: public XuiTitle(String _text, int _alignment) {
20: text_ = _text;
21: alignment_ = _alignment;
22:
23: setForeground(new Color(255, 255, 255, 254));
24: setBackground(new Color(0, 0, 0, 96));
25: setFont(new Font("SansSerif", Font.PLAIN, 48));
26: setBorder(new EmptyBorder(4, 4, 4, 4));
27: setOpaque(false);
28: }
29:
30: public Dimension getPreferredSize() {
31: FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(
32: getFont());
33: int h = fm.getAscent() + fm.getDescent(); // getFont().getSize();
34: return new Dimension(1, h + 5);
35: }
36:
37: public void paint(Graphics _g) {
38: ((Graphics2D) _g).setRenderingHint(
39: RenderingHints.KEY_ANTIALIASING,
40: RenderingHints.VALUE_ANTIALIAS_ON);
41: super .paint(_g);
42: }
43:
44: public void paintComponent(Graphics _g) {
45: Font ft = getFont();
46: FontMetrics fm;
47:
48: while (((fm = _g.getFontMetrics(ft)).stringWidth(text_) + 2 > getWidth())
49: && (ft.getSize() >= 8))
50: ft = new Font(ft.getFamily(), ft.getStyle(),
51: ft.getSize() - 5);
52:
53: int x, y;
54:
55: switch (alignment_) {
56: case 0:
57: x = 2;
58: break;
59: case 2:
60: x = getWidth() - 2 - fm.stringWidth(text_);
61: break;
62: default:
63: x = (getWidth() - fm.stringWidth(text_)) / 2;
64: break;
65: }
66:
67: y = (getHeight() - fm.getAscent() - fm.getDescent()) / 2;
68: y += fm.getAscent();
69:
70: Graphics2D g = (Graphics2D) _g;
71: g.setFont(ft);
72:
73: //g.setColor(getForeground());
74: //g.drawString(text_,x,y);
75:
76: g.setColor(getBackground());
77: g.drawString(text_, x - 1, y - 1);
78: g.drawString(text_, x + 1, y - 1);
79: g.drawString(text_, x - 1, y + 1);
80: g.drawString(text_, x + 1, y + 1);
81: g.setColor(getForeground());
82: g.drawString(text_, x, y);
83: }
84: }
|