下面列出了javax.swing.text.Utilities#drawTabbedText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void testNPE() {
Graphics g = null;
try {
String test = "\ttest\ttest2";
BufferedImage buffImage = new BufferedImage(
100, 100, BufferedImage.TYPE_INT_RGB);
g = buffImage.createGraphics();
Segment segment = new Segment(test.toCharArray(), 0, test.length());
Utilities.drawTabbedText(segment, 0, 0, g, null, 0);
} finally {
if (g != null) {
g.dispose();
}
}
}
/**
* Draw text. This can directly call the Utilities.drawTabbedText.
* Sub-classes can override this method to provide any other decorations.
* @param segment - the source of the text
* @param x - the X origin >= 0
* @param y - the Y origin >= 0
* @param graphics - the graphics context
* @param e - how to expand the tabs. If this value is null, tabs will be
* expanded as a space character.
* @param startOffset - starting offset of the text in the document >= 0
* @return
*/
public int drawText(Segment segment, int x, int y,
Graphics graphics, TabExpander e, int startOffset) {
graphics.setFont(graphics.getFont().deriveFont(getFontStyle()));
FontMetrics fontMetrics = graphics.getFontMetrics();
int a = fontMetrics.getAscent();
int h = a + fontMetrics.getDescent();
int w = Utilities.getTabbedTextWidth(segment, fontMetrics, 0, e, startOffset);
int rX = x - 1;
int rY = y - a;
int rW = w + 2;
int rH = h;
if ((getFontStyle() & 0x10) != 0) {
graphics.setColor(Color.decode("#EEEEEE"));
graphics.fillRect(rX, rY, rW, rH);
}
graphics.setColor(getColor());
x = Utilities.drawTabbedText(segment, x, y, graphics, e, startOffset);
if ((getFontStyle() & 0x8) != 0) {
graphics.setColor(Color.RED);
graphics.drawRect(rX, rY, rW, rH);
}
return x;
}
public static int drawTabbedText(Segment segment, int x, int y, Graphics g, TabExpander e, int startOffset){
List<Segment> segments=new ArrayList<Segment>();
List<Boolean> unis=new ArrayList<Boolean>();
getSegments(g.getFont(), segment, segments, unis);
Font origFont=g.getFont();
Font uniFont = defaultUniFont.deriveFont(origFont.getStyle(),origFont.getSize2D());
int ret=x;
int pos=0;
for(int i=0;i<segments.size();i++){
Segment seg=segments.get(i);
if(unis.get(i)){
g.setFont(uniFont);
}else{
g.setFont(origFont);
}
ret = Utilities.drawTabbedText(seg, ret, y, g, e, startOffset+pos);
pos += seg.length();
}
g.setFont(origFont);
return ret;
}
/**
* Draw text. This can directly call the Utilities.drawTabbedText.
* Sub-classes can override this method to provide any other decorations.
*
* @param segment - the source of the text
* @param x - the X origin >= 0
* @param y - the Y origin >= 0
* @param graphics - the graphics context
* @param e - how to expand the tabs. If this value is null, tabs will be
* expanded as a space character.
* @param startOffset - starting offset of the text in the document >= 0
* @return
*/
public int drawText(Segment segment, int x, int y,
Graphics graphics, TabExpander e, int startOffset) {
graphics.setFont(graphics.getFont().deriveFont(getFontStyle()));
FontMetrics fontMetrics = graphics.getFontMetrics();
int a = fontMetrics.getAscent();
int h = a + fontMetrics.getDescent();
int w = Utilities.getTabbedTextWidth(segment, fontMetrics, 0, e, startOffset);
int rX = x - 1;
int rY = y - a;
int rW = w + 2;
int rH = h;
if ((getFontStyle() & 0x10) != 0) {
graphics.setColor(Color.decode("#EEEEEE"));
graphics.fillRect(rX, rY, rW, rH);
}
graphics.setColor(getColor());
x = Utilities.drawTabbedText(segment, x, y, graphics, e, startOffset);
if ((getFontStyle() & 0x8) != 0) {
graphics.setColor(Color.RED);
graphics.drawRect(rX, rY, rW, rH);
}
return x;
}
private int drawText(Graphics g,
int x, int y,
int startOffset, int endOffset,
boolean error,
boolean selected,
DocElement docElem) throws BadLocationException {
Segment s = EventQueue.isDispatchThread() ? SEGMENT : new Segment();
s.array = docElem.getChars();
s.offset = startOffset - docElem.offset;
s.count = endOffset - startOffset;
g.setColor(getColor(error, selected));
return Utilities.drawTabbedText(s, x, y, g, this, startOffset);
}
/**
* Paints the specified line onto the graphics context. Note that this method munges the offset
* and count values of the segment.
*
* @param line
* The line segment
* @param tokens
* The token list for the line
* @param styles
* The syntax style list
* @param expander
* The tab expander used to determine tab stops. May be null
* @param gfx
* The graphics context
* @param x
* The x co-ordinate
* @param y
* The y co-ordinate
* @return The x co-ordinate, plus the width of the painted string
*/
public static int paintSyntaxLine(Segment line, Token tokens, SyntaxStyle[] styles, TabExpander expander, Graphics gfx,
int x, int y) {
Font defaultFont = gfx.getFont();
Color defaultColor = gfx.getColor();
for (;;) {
byte id = tokens.id;
if (id == Token.END) {
break;
}
int length = tokens.length;
if (id == Token.NULL) {
if (!defaultColor.equals(gfx.getColor())) {
gfx.setColor(defaultColor);
}
if (!defaultFont.equals(gfx.getFont())) {
gfx.setFont(defaultFont);
}
} else {
styles[id].setGraphicsFlags(gfx, defaultFont);
}
line.count = length;
x = Utilities.drawTabbedText(line, x, y, gfx, expander, 0);
line.offset += length;
tokens = tokens.next;
}
return x;
}
protected void paintPlainLine(Graphics gfx, int line, Font defaultFont, Color defaultColor, int x, int y) {
paintHighlight(gfx, line, y);
textArea.getLineText(line, currentLine);
gfx.setFont(defaultFont);
gfx.setColor(defaultColor);
y += fm.getHeight();
x = Utilities.drawTabbedText(currentLine, x, y, gfx, this, 0);
if (eolMarkers) {
gfx.setColor(eolMarkerColor);
gfx.drawString(".", x, y);
}
}
@Override
protected int drawUnselectedText(Graphics graphics, int x, int y, int p0, int p1) {
Font saveFont = graphics.getFont();
Color saveColor = graphics.getColor();
SyntaxDocument doc = (SyntaxDocument) getDocument();
Segment segment = getLineBuffer();
try {
// Colour the parts
Iterator<Token> tokens = doc.getTokens(p0, p1);
int start = p0;
while (tokens.hasNext()) {
Token t = tokens.next();
// if there is a gap between the next token start and where we
// should be starting (spaces not returned in tokens), then draw
// it in the default type
if (start < t.start) {
SyntaxStyles.getInstance().setGraphicsStyle(graphics,
TokenType.DEFAULT);
doc.getText(start, t.start - start, segment);
x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
}
// t and s are the actual start and length of what we should
// put on the screen. assume these are the whole token....
int l = t.length;
int s = t.start;
// ... unless the token starts before p0:
if (s < p0) {
// token is before what is requested. adgust the length and s
l -= (p0 - s);
s = p0;
}
// if token end (s + l is still the token end pos) is greater
// than p1, then just put up to p1
if (s + l > p1) {
l = p1 - s;
}
doc.getText(s, l, segment);
x = SyntaxStyles.getInstance().drawText(segment, x, y, graphics, this, t);
start = t.start + t.length;
}
// now for any remaining text not tokenized:
if (start < p1) {
SyntaxStyles.getInstance().setGraphicsStyle(graphics, TokenType.DEFAULT);
doc.getText(start, p1 - start, segment);
x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
}
} catch (BadLocationException e) {
System.err.println("Requested: " + e.offsetRequested());
LOG.error(e.getMessage(), e);
} finally {
graphics.setFont(saveFont);
graphics.setColor(saveColor);
}
return x;
}