
Over at JHLabs there are examples of how to create e.g., glowing text:
Or chrome like text:
To get a simmlar effect on a JLabel just replace the JLabel with a JHLabsLabel e.g.;
JLabel label = new JHLabsLabel("Hello World", new ChromeFilter(), new ShadowFilter(5, 5, 2, .7f));
Here is the code for JHLabsLabel:
/**
* Notice that this is not perfect wrt alignment to see just uncomment super.paintComponent(g)
* Maybe somebody with BasicLabelUI knowledge could help?
*
*/
public static final class JHLabsLabel extends JLabel {
private int textX;
private int textY;
private AbstractBufferedImageOp[] filters;
public JHLabsLabel(String string, AbstractBufferedImageOp... filters) {
super(string);
this.filters = filters;
}
@Override
public void paintComponent(Graphics g) {
// super.paintComponent(g);
BufferedImage img = createTextImage(getText(), getFont());
for (AbstractBufferedImageOp f : filters) {
img = f.filter(img, null);
}
// g.drawImage(f.filter(img, null), textX, textY, null);
g.drawImage(img, textX, textY, null);
}
private BufferedImage createTextImage(String text, Font font) {
Rectangle paintIconR = new Rectangle();
Rectangle paintTextR = new Rectangle();
Rectangle paintViewR = new Rectangle();
Insets paintViewInsets = new Insets(0, 0, 0, 0);
paintViewInsets = getInsets(paintViewInsets);
paintViewR.x = paintViewInsets.left;
paintViewR.y = paintViewInsets.top;
paintViewR.width = getWidth()
- (paintViewInsets.left + paintViewInsets.right);
paintViewR.height = getHeight()
- (paintViewInsets.top + paintViewInsets.bottom);
String clippedText = SwingUtilities.layoutCompoundLabel(
(JComponent) this, getFontMetrics(getFont()), text,
getIcon(), getVerticalAlignment(),
getHorizontalAlignment(), getVerticalTextPosition(),
getHorizontalTextPosition(), paintViewR, paintIconR,
paintTextR, getIconTextGap());
boolean isAntiAliased = true;
boolean usesFractionalMetrics = false;
FontRenderContext frc = new FontRenderContext(null, isAntiAliased,
usesFractionalMetrics);
TextLayout layout = new TextLayout(clippedText, font, frc);
Rectangle2D bounds = layout.getBounds();
int w = (int) Math.ceil(bounds.getWidth());
int h = (int) Math.ceil(bounds.getHeight());
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(new Color(0, 0, 0, 0));
g.fillRect(0, 0, w, h);
g.setColor(getForeground());
g.setFont(font);
Object antiAliased = isAntiAliased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
: RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
antiAliased);
Object fractionalMetrics = usesFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON
: RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
fractionalMetrics);
g.drawString(clippedText, (float) -bounds.getX(), (float) -bounds
.getY());
// g.drawString(clippedText, (float) 0, (float) 0);
g.dispose();
textX = paintTextR.x;
textY = paintTextR.y;// + getFontMetrics(font).getAscent() / 2;
System.out.println(String.format("X=%d Y=%d, w=%d h=%d", textX,
textY, w, h));
return image;
}
}
I also could not resist to play to the JHLabs texture filters as background painters e.g.,
JXPanel woodPanel = new JXPanel();
woodPanel.setBackgroundPainter(new JHTexturePainter<JXPanel>(new CausticsFilter(), new SparkleFilter()));
Creates the initial background for the demo.
Here is the code for JHTexturePainter:
/**
*
* Paint the background using a combination of the JHLabs image filters
*
* @param <T>
*/
static final class JHTexturePainter<T> implements Painter<T> {
private AbstractBufferedImageOp[] filters;
public JHTexturePainter(AbstractBufferedImageOp... filters) {
this.filters = filters;
}
@Override
public void paint(Graphics2D g2d, T arg1, int w, int h) {
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(new Color(0, 0, 0, 0));
g.fillRect(0, 0, w, h);
for (AbstractBufferedImageOp f : filters) {
image = f.filter(image, null);
}
g.dispose();
g2d.drawImage(image, 0, 0, null);
}
}