下面列出了怎么用javax.swing.text.LayeredHighlighter的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Adds a highlight to the view. Returns a tag that can be used
* to refer to the highlight.
*
* @param p0 the start offset of the range to highlight >= 0
* @param p1 the end offset of the range to highlight >= p0
* @param p the painter to use to actually render the highlight
* @return an object that can be used as a tag
* to refer to the highlight
* @exception BadLocationException if the specified location is invalid
*/
public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter p) throws BadLocationException {
Document doc = component.getDocument();
HighlightInfo i = (getDrawsLayeredHighlights() &&
(p instanceof LayeredHighlighter.LayerPainter)) ?
new LayeredHighlightInfo() : new HighlightInfo();
i.painter = p;
i.p0 = doc.createPosition(p0);
i.p1 = doc.createPosition(p1);
// For snippets, we want to make sure selection is layered ON TOP
// since we add transparency to the selection color; so rather
// than append the highlight, we insert it in the front.
highlights.insertElementAt(i, 0);
safeDamageRange(p0, p1);
return i;
}
/**
* Adds a highlight to the view. Returns a tag that can be used
* to refer to the highlight.
*
* @param p0 the start offset of the range to highlight >= 0
* @param p1 the end offset of the range to highlight >= p0
* @param p the painter to use to actually render the highlight
* @return an object that can be used as a tag
* to refer to the highlight
* @exception BadLocationException if the specified location is invalid
*/
public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter p) throws BadLocationException {
Document doc = component.getDocument();
HighlightInfo i = (getDrawsLayeredHighlights() &&
(p instanceof LayeredHighlighter.LayerPainter)) ?
new LayeredHighlightInfo() : new HighlightInfo();
i.painter = p;
i.p0 = doc.createPosition(p0);
i.p1 = doc.createPosition(p1);
// For snippets, we want to make sure selection is layered ON TOP
// since we add transparency to the selection color; so rather
// than append the highlight, we insert it in the front.
highlights.insertElementAt(i, 0);
safeDamageRange(p0, p1);
return i;
}
private void paintHighlights(Graphics g, Shape shape) {
if (container instanceof JTextComponent) {
JTextComponent tc = (JTextComponent)container;
Highlighter h = tc.getHighlighter();
if (h instanceof LayeredHighlighter) {
((LayeredHighlighter)h).paintLayeredHighlights
(g, getStartOffset(), getEndOffset(), shape, tc, this);
}
}
}
private void paintHighlights(Graphics g, Shape shape) {
if (container instanceof JTextComponent) {
JTextComponent tc = (JTextComponent)container;
Highlighter h = tc.getHighlighter();
if (h instanceof LayeredHighlighter) {
((LayeredHighlighter)h).paintLayeredHighlights
(g, getStartOffset(), getEndOffset(), shape, tc, this);
}
}
}
/**
* Restricts the region based on the receivers offsets and messages
* the painter to paint the region.
*/
void paintLayeredHighlights(Graphics g, int p0, int p1,
Shape viewBounds, JTextComponent editor,
View view) {
int start = getStartOffset();
int end = getEndOffset();
// Restrict the region to what we represent
p0 = Math.max(start, p0);
p1 = Math.min(end, p1);
// Paint the appropriate region using the painter and union
// the effected region with our bounds.
union(((LayeredHighlighter.LayerPainter)painter).paintLayer
(g, p0, p1, viewBounds, editor, view));
}
/**
* Restricts the region based on the receivers offsets and messages
* the painter to paint the region.
*/
void paintLayeredHighlights(Graphics g, int p0, int p1,
Shape viewBounds, JTextComponent editor,
View view) {
int start = getStartOffset();
int end = getEndOffset();
// Restrict the region to what we represent
p0 = Math.max(start, p0);
p1 = Math.min(end, p1);
// Paint the appropriate region using the painter and union
// the effected region with our bounds.
union(((LayeredHighlighter.LayerPainter)painter).paintLayer
(g, p0, p1, viewBounds, editor, view));
}
private void paintHighlights(Graphics g, Shape shape)
{
if (container instanceof JTextComponent)
{
JTextComponent tc = (JTextComponent) container;
Highlighter h = tc.getHighlighter();
if (h instanceof LayeredHighlighter)
{
((LayeredHighlighter) h).paintLayeredHighlights(g,
getStartOffset(), getEndOffset(), shape, tc, this);
}
}
}
private void paintHighlights(Graphics g, Shape shape)
{
if (container instanceof JTextComponent)
{
JTextComponent tc = (JTextComponent) container;
Highlighter h = tc.getHighlighter();
if (h instanceof LayeredHighlighter)
{
((LayeredHighlighter) h).paintLayeredHighlights(g,
getStartOffset(), getEndOffset(), shape, tc, this);
}
}
}
/**
* Process paint.
*
* @param gg
* the graphics context
* @param a
* the allocation
*/
protected void processPaint(Graphics gg, Shape a)
{
Graphics2D g = (Graphics2D) gg;
AffineTransform tmpTransform = g.getTransform();
if (!tmpTransform.equals(transform))
{
transform = tmpTransform;
invalidateTextLayout();
}
Component c = container;
int p0 = getStartOffset();
int p1 = getEndOffset();
Color fg = getForeground();
if (c instanceof JTextComponent)
{
JTextComponent tc = (JTextComponent) c;
if (!tc.isEnabled())
{
fg = tc.getDisabledTextColor();
}
// javax.swing.plaf.basic.BasicTextUI $ BasicHighlighter
// >> DefaultHighlighter
// >> DefaultHighlightPainter
Highlighter highLighter = tc.getHighlighter();
if (highLighter instanceof LayeredHighlighter)
{
((LayeredHighlighter) highLighter).paintLayeredHighlights(g, p0, p1, box.getAbsoluteContentBounds(), tc, this);
// (g, p0, p1, a, tc, this);
}
}
// nothing is selected
if (!box.isEmpty() && !getText().isEmpty())
renderContent(g, a, fg, p0, p1);
}