01: /*
02: * StrokedLabel.java
03: *
04: * Created on December 7, 2006, 10:16 AM
05: *
06: * Copyright 2006-2007 Nigel Hughes
07: *
08: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
09: * in compliance with the License. You may obtain a copy of the License at http://www.apache.org/
10: * licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
13: * governing permissions and limitations under the License.
14: */
15:
16: package com.blogofbug.swing.components;
17:
18: import java.awt.BasicStroke;
19: import java.awt.Color;
20: import java.awt.Dimension;
21: import java.awt.Font;
22: import java.awt.Graphics;
23: import java.awt.Graphics2D;
24: import java.awt.Rectangle;
25: import java.awt.RenderingHints;
26: import java.awt.Shape;
27: import java.awt.font.FontRenderContext;
28: import java.awt.font.TextLayout;
29: import java.awt.geom.AffineTransform;
30: import javax.swing.JLabel;
31:
32: /**
33: * A text label that draws it's text with a fill color and an outline drawn around the outside. Created for DockPanels but useful elsewhere as well
34: * @author nigel
35: */
36: public class StrokedLabel extends JLabel {
37:
38: /**
39: * Creates a new instance of stroked label with no content
40: */
41: public StrokedLabel() {
42:
43: }
44:
45: /**
46: * Creates a new instance of StrokedLabel
47: * @param text The text to display in the label
48: */
49: public StrokedLabel(String text) {
50: super (text);
51: }
52:
53: /**
54: * Get the prefered size of the component
55: * @return The prefered size
56: */
57: public Dimension getPreferredSize() {
58: Dimension d = super .getPreferredSize();
59:
60: d.width += 8;
61: d.height += 8;
62: return d;
63: }
64:
65: /**
66: * Paints the component
67: * @param g The graphics context
68: */
69: public void paintComponent(Graphics g) {
70: Graphics2D g2 = (Graphics2D) g;
71: g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
72: RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
73: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
74: RenderingHints.VALUE_ANTIALIAS_ON);
75: g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
76: RenderingHints.VALUE_INTERPOLATION_BILINEAR);
77: FontRenderContext frc = g2.getFontRenderContext();
78: TextLayout tl = new TextLayout(getText(), getFont(), frc);
79: float sw = (float) tl.getBounds().getWidth();
80: AffineTransform transform = new AffineTransform();
81: transform.setToTranslation(4, getFont().getSize() + 4);
82: Shape shape = tl.getOutline(transform);
83:
84: g2.setStroke(new BasicStroke(3));
85: g2.setColor(Color.BLACK);
86: g2.draw(shape);
87:
88: g2.setColor(Color.WHITE);
89: g2.drawString(getText(), 4, getFont().getSize() + 4);
90: }
91: }
|