本文整理匯總了Java中javax.swing.text.Utilities.getTabbedTextWidth方法的典型用法代碼示例。如果您正苦於以下問題:Java Utilities.getTabbedTextWidth方法的具體用法?Java Utilities.getTabbedTextWidth怎麽用?Java Utilities.getTabbedTextWidth使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.text.Utilities
的用法示例。
在下文中一共展示了Utilities.getTabbedTextWidth方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: drawText
import javax.swing.text.Utilities; //導入方法依賴的package包/類
/**
* 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;
}
示例2: getTabbedTextWidth
import javax.swing.text.Utilities; //導入方法依賴的package包/類
public static int getTabbedTextWidth(Graphics g,Segment segment,int x,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=0;
int pos=0;
for(int i=0;i<segments.size();i++){
Segment seg=segments.get(i);
ret += Utilities.getTabbedTextWidth(seg, g.getFontMetrics(unis.get(i)?uniFont:origFont), 0, e, startOffset+pos);
pos += seg.length();
}
return ret;
}
示例3: getPreferredSize
import javax.swing.text.Utilities; //導入方法依賴的package包/類
public Dimension getPreferredSize(JComponent c) {
FontMetrics metrics = c.getFontMetrics(c.getFont());
String tipText = ((JToolTip)c).getTipText();
if(tipText == null) tipText = "";
ArrayList linesa = new ArrayList();
String[] strs = Strings.split(tipText, '\n');
for(int i = 0; i < strs.length; i++) {
Segment s = new Segment(strs[i].toCharArray(), 0, strs[i].length());
for(int brk = 0; ;) {
if(Utilities.getTabbedTextWidth(s, metrics, 0, null, 0) < maxAllowedWidth) {
linesa.add(new String(s.array, s.offset, s.array.length - s.offset));
break;
}
brk = Utilities.getBreakLocation(s, metrics, 0, maxAllowedWidth, null, 0);
linesa.add(new String(s.array, s.offset, brk));
s.offset += brk;
s.count -= brk;
}
}
lines = (String[])linesa.toArray(new String[linesa.size()]);
maxWidth = 0;
for(int i = 0; i < lines.length; i++)
maxWidth = Math.max(maxWidth, SwingUtilities.computeStringWidth(metrics, lines[i]));
return new Dimension(maxWidth + 6, metrics.getHeight() * lines.length + 2);
}
示例4: _offsetToX
import javax.swing.text.Utilities; //導入方法依賴的package包/類
/**
* Converts an offset in a line into an x co-ordinate. This is a fast version that should only
* be used if no changes were made to the text since the last repaint.
*
* @param line
* The line
* @param offset
* The offset, from the start of the line
*/
public int _offsetToX(int line, int offset) {
TokenMarker tokenMarker = getTokenMarker();
/* Use painter's cached info for speed */
FontMetrics fm = painter.getFontMetrics();
getLineText(line, lineSegment);
int segmentOffset = lineSegment.offset;
int x = horizontalOffset;
/* If syntax coloring is disabled, do simple translation */
if (tokenMarker == null) {
lineSegment.count = offset;
return x + Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
}
/*
* If syntax coloring is enabled, we have to do this because tokens can vary in width
*/
else {
Token tokens;
if (painter.currentLineIndex == line && painter.currentLineTokens != null) {
tokens = painter.currentLineTokens;
} else {
painter.currentLineIndex = line;
tokens = painter.currentLineTokens = tokenMarker.markTokens(lineSegment, line);
}
Font defaultFont = painter.getFont();
SyntaxStyle[] styles = painter.getStyles();
for (;;) {
byte id = tokens.id;
if (id == Token.END) {
return x;
}
if (id == Token.NULL) {
fm = painter.getFontMetrics();
} else {
fm = styles[id].getFontMetrics(defaultFont);
}
int length = tokens.length;
if (offset + segmentOffset < lineSegment.offset + length) {
lineSegment.count = offset - (lineSegment.offset - segmentOffset);
return x + Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
} else {
lineSegment.count = length;
x += Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
lineSegment.offset += length;
}
tokens = tokens.next;
}
}
}
示例5: listOffsetToView
import javax.swing.text.Utilities; //導入方法依賴的package包/類
@Override
public Rectangle listOffsetToView(RSyntaxTextArea textArea, TabExpander e,
int pos, int x0, Rectangle rect) {
int stableX = x0; // Cached ending x-coord. of last tab or token.
TokenImpl token = this;
FontMetrics fm = null;
Segment s = new Segment();
while (token != null && token.isPaintable()) {
fm = textArea.getFontMetricsForTokenType(token.getType());
if (fm == null) {
return rect; // Don't return null as things'll error.
}
char[] text = token.text;
int start = token.textOffset;
int end = start + token.textCount;
// If this token contains the position for which to get the
// bounding box...
if (token.containsPosition(pos)) {
s.array = token.text;
s.offset = token.textOffset;
s.count = pos - token.getOffset();
// Must use this (actually fm.charWidth()), and not
// fm.charsWidth() for returned value to match up with where
// text is actually painted on OS X!
int w = Utilities.getTabbedTextWidth(s, fm, stableX, e,
token.getOffset());
rect.x = stableX + w;
end = token.documentToToken(pos);
if (text[end] == '\t') {
rect.width = fm.charWidth(' ');
}
else {
rect.width = fm.charWidth(text[end]);
}
return rect;
}
// If this token does not contain the position for which to get
// the bounding box...
else {
s.array = token.text;
s.offset = token.textOffset;
s.count = token.textCount;
stableX += Utilities.getTabbedTextWidth(s, fm, stableX, e,
token.getOffset());
}
token = (TokenImpl)token.getNextToken();
}
// If we didn't find anything, we're at the end of the line. Return
// a width of 1 (so selection highlights don't extend way past line's
// text). A ConfigurableCaret will know to paint itself with a larger
// width.
rect.x = stableX;
rect.width = 1;
return rect;
}
示例6: listOffsetToView
import javax.swing.text.Utilities; //導入方法依賴的package包/類
public Rectangle listOffsetToView(RSyntaxTextArea textArea, TabExpander e,
int pos, int x0, Rectangle rect) {
int stableX = x0; // Cached ending x-coord. of last tab or token.
TokenImpl token = this;
FontMetrics fm = null;
Segment s = new Segment();
while (token != null && token.isPaintable()) {
fm = textArea.getFontMetricsForTokenType(token.getType());
if (fm == null) {
return rect; // Don't return null as things'll error.
}
char[] text = token.text;
int start = token.textOffset;
int end = start + token.textCount;
// If this token contains the position for which to get the
// bounding box...
if (token.containsPosition(pos)) {
s.array = token.text;
s.offset = token.textOffset;
s.count = pos - token.getOffset();
// Must use this (actually fm.charWidth()), and not
// fm.charsWidth() for returned value to match up with where
// text is actually painted on OS X!
int w = Utilities.getTabbedTextWidth(s, fm, stableX, e,
token.getOffset());
rect.x = stableX + w;
end = token.documentToToken(pos);
if (text[end] == '\t') {
rect.width = fm.charWidth(' ');
}
else {
rect.width = fm.charWidth(text[end]);
}
return rect;
}
// If this token does not contain the position for which to get
// the bounding box...
else {
s.array = token.text;
s.offset = token.textOffset;
s.count = token.textCount;
stableX += Utilities.getTabbedTextWidth(s, fm, stableX, e,
token.getOffset());
}
token = (TokenImpl)token.getNextToken();
}
// If we didn't find anything, we're at the end of the line. Return
// a width of 1 (so selection highlights don't extend way past line's
// text). A ConfigurableCaret will know to paint itself with a larger
// width.
rect.x = stableX;
rect.width = 1;
return rect;
}
示例7: _offsetToX
import javax.swing.text.Utilities; //導入方法依賴的package包/類
/**
* Converts an offset in a line into an x co-ordinate. This is a
* fast version that should only be used if no changes were made
* to the text since the last repaint.
* @param line The line
* @param offset The offset, from the start of the line
*/
public int _offsetToX(int line, int offset)
{
TokenMarker tokenMarker = getTokenMarker();
/* Use painter's cached info for speed */
FontMetrics fm = painter.getFontMetrics();
getLineText(line,lineSegment);
int segmentOffset = lineSegment.offset;
int x = horizontalOffset;
/* If syntax coloring is disabled, do simple translation */
if(tokenMarker == null)
{
lineSegment.count = offset;
return x + Utilities.getTabbedTextWidth(lineSegment,
fm,x,painter,0);
}
/* If syntax coloring is enabled, we have to do this because
* tokens can vary in width */
else
{
Token tokens;
if(painter.currentLineIndex == line
&& painter.currentLineTokens != null)
tokens = painter.currentLineTokens;
else
{
painter.currentLineIndex = line;
tokens = painter.currentLineTokens
= tokenMarker.markTokens(lineSegment,line);
}
// Toolkit toolkit =
painter.getToolkit();
Font defaultFont = painter.getFont();
SyntaxStyle[] styles = painter.getStyles();
for(;;)
{
byte id = tokens.id;
if(id == Token.END)
{
return x;
}
if(id == Token.NULL)
fm = painter.getFontMetrics();
else
fm = styles[id].getFontMetrics(defaultFont);
int length = tokens.length;
if(offset + segmentOffset < lineSegment.offset + length)
{
lineSegment.count = offset - (lineSegment.offset - segmentOffset);
return x + Utilities.getTabbedTextWidth(
lineSegment,fm,x,painter,0);
}
else
{
lineSegment.count = length;
x += Utilities.getTabbedTextWidth(
lineSegment,fm,x,painter,0);
lineSegment.offset += length;
}
tokens = tokens.next;
}
}
}
示例8: _offsetToX
import javax.swing.text.Utilities; //導入方法依賴的package包/類
/**
* Converts an offset in a line into an x co-ordinate. This is a fast version that should only be used if no changes were made to the text since the last repaint.
*
* @param line
* The line
* @param offset
* The offset, from the start of the line
*/
public int _offsetToX(int line, int offset) {
TokenMarker tokenMarker = getTokenMarker();
/* Use painter's cached info for speed */
FontMetrics fm = painter.getFontMetrics();
getLineText(line, lineSegment);
int segmentOffset = lineSegment.offset;
int x = horizontalOffset;
/* If syntax coloring is disabled, do simple translation */
if (tokenMarker == null) {
lineSegment.count = offset;
return x + Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
}
/*
* If syntax coloring is enabled, we have to do this because tokens can vary in width
*/
else {
Token tokens;
if (painter.currentLineIndex == line && painter.currentLineTokens != null)
tokens = painter.currentLineTokens;
else {
painter.currentLineIndex = line;
tokens = painter.currentLineTokens = tokenMarker.markTokens(lineSegment, line);
}
Font defaultFont = painter.getFont();
SyntaxStyle[] styles = painter.getStyles();
for (;;) {
byte id = tokens.id;
if (id == Token.END) {
return x;
}
if (id == Token.NULL)
fm = painter.getFontMetrics();
else
fm = styles[id].getFontMetrics(defaultFont);
int length = tokens.length;
if (offset + segmentOffset < lineSegment.offset + length) {
lineSegment.count = offset - (lineSegment.offset - segmentOffset);
return x + Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
} else {
lineSegment.count = length;
x += Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
lineSegment.offset += length;
}
tokens = tokens.next;
}
}
}
示例9: _offsetToX
import javax.swing.text.Utilities; //導入方法依賴的package包/類
/**
* Converts an offset in a line into an x co-ordinate. This is a fast
* version that should only be used if no changes were made to the text
* since the last repaint.
*
* @param line
* The line
* @param offset
* The offset, from the start of the line
*/
public int _offsetToX(int line, int offset) {
TokenMarker tokenMarker = getTokenMarker();
/* Use painter's cached info for speed */
FontMetrics fm = painter.getFontMetrics();
getLineText(line, lineSegment);
int segmentOffset = lineSegment.offset;
int x = horizontalOffset;
/* If syntax coloring is disabled, do simple translation */
if (tokenMarker == null) {
lineSegment.count = offset;
return x + Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
}
/*
* If syntax coloring is enabled, we have to do this because tokens can
* vary in width
*/
else {
Token tokens;
if (painter.currentLineIndex == line && painter.currentLineTokens != null)
tokens = painter.currentLineTokens;
else {
painter.currentLineIndex = line;
tokens = painter.currentLineTokens = tokenMarker.markTokens(lineSegment, line);
}
Font defaultFont = painter.getFont();
SyntaxStyle[] styles = painter.getStyles();
for (;;) {
byte id = tokens.id;
if (id == Token.END) {
return x;
}
if (id == Token.NULL)
fm = painter.getFontMetrics();
else
fm = styles[id].getFontMetrics(defaultFont);
int length = tokens.length;
if (offset + segmentOffset < lineSegment.offset + length) {
lineSegment.count = offset - (lineSegment.offset - segmentOffset);
return x + Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
} else {
lineSegment.count = length;
x += Utilities.getTabbedTextWidth(lineSegment, fm, x, painter, 0);
lineSegment.offset += length;
}
tokens = tokens.next;
}
}
}