Here is a hacker example. It is not as difficult as others, but easier to understand, and it behaves like JLabel.
public class OutlineLabel extends JLabel { private Color outlineColor = Color.WHITE; private boolean isPaintingOutline = false; private boolean forceTransparent = false; public OutlineLabel() { super(); } public OutlineLabel(String text) { super(text); } public OutlineLabel(String text, int horizontalAlignment) { super(text, horizontalAlignment); } public Color getOutlineColor() { return outlineColor; } public void setOutlineColor(Color outlineColor) { this.outlineColor = outlineColor; this.invalidate(); } @Override public Color getForeground() { if ( isPaintingOutline ) { return outlineColor; } else { return super.getForeground(); } } @Override public boolean isOpaque() { if ( forceTransparent ) { return false; } else { return super.isOpaque(); } } @Override public void paint(Graphics g) { String text = getText(); if ( text == null || text.length() == 0 ) { super.paint(g); return; }
Booyah johnson
source share