public class GradientTextPane extends JTextPane {
private transient Position.Bias[] bias = new Position.Bias[1];
/**
* Creates new form GradientLabel
*/
public GradientTextPane() {
initComponents();
}
public void setBgColor(Color bgColor) {
start = bgColor;
repaint();
}
@Override
public String getToolTipText(MouseEvent e) {
String title = super.getToolTipText(e);
JTextPane editor = (JTextPane) e.getSource();
if (!editor.isEditable()) {
Point pt = e.getPoint();
int pos = editor.getUI().viewToModel(editor, pt, bias);
if (bias[0] == Position.Bias.Backward && pos > 0) {
pos--;
}
if (pos >= 0 && (editor.getDocument() instanceof HTMLDocument)) {
HTMLDocument hdoc = (HTMLDocument) editor.getDocument();
Element elem = hdoc.getCharacterElement(pos);
if (elem != null) {
AttributeSet a = elem.getAttributes();
//AttributeSet span = (AttributeSet) a.getAttribute(HTML.Tag.SPAN);
if (a != null) {
title = (String) a.getAttribute(HTML.Attribute.TITLE);
}
}
}
}
return title;
}
@Override
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
colors[0] = start;
colors[1] = start.brighter();
colors[2] = start;
// Create the gradient paint
//GradientPaint paint = new GradientPaint(0, 0, start, width, height, end, true);
LinearGradientPaint paint = new LinearGradientPaint(0, 0, 0, getHeight(), dist, colors);
// we need to cast to Graphics2D for this operation
Graphics2D g2d = (Graphics2D) g;
// save the old paint
Paint oldPaint = g2d.getPaint();
// set the paint to use for this operation
g2d.setPaint(paint);
// fill the background using the paint
g2d.fillRect(0, 0, width, height);
// restore the original paint
g2d.setPaint(oldPaint);
super.paint(g);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
setOpaque(true);
}//
//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
private Color start;
float[] dist = {0.0f, 0.5f, 1.0f};
private Color[] colors = new Color[3];
}
No comments:
Post a Comment