下面列出了javax.swing.plaf.basic.BasicGraphicsUtils#drawStringUnderlineCharAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void paintText(Graphics g, Rectangle textRect, String text) {
FontMetrics fm = g.getFontMetrics();
int mnemonicIndex = -1;
if(isEnabled()) {
/*** paint the text normally */
g.setColor(getPeerForeground());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
}
else {
/*** paint the text disabled ***/
g.setColor(getPeerBackground().brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x, textRect.y + fm.getAscent());
g.setColor(getPeerBackground().darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x - 1, textRect.y + fm.getAscent() - 1);
}
}
protected void paintText(Graphics g, Rectangle textRect, String text) {
FontMetrics fm = g.getFontMetrics();
int mnemonicIndex = -1;
if(isEnabled()) {
/*** paint the text normally */
g.setColor(getPeerForeground());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
}
else {
/*** paint the text disabled ***/
g.setColor(getPeerBackground().brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x, textRect.y + fm.getAscent());
g.setColor(getPeerBackground().darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x - 1, textRect.y + fm.getAscent() - 1);
}
}
protected void paintText(Graphics g, Rectangle textRect, String text) {
FontMetrics fm = g.getFontMetrics();
int mnemonicIndex = -1;
if(isEnabled()) {
/*** paint the text normally */
g.setColor(getPeerForeground());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
}
else {
/*** paint the text disabled ***/
g.setColor(getPeerBackground().brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x, textRect.y + fm.getAscent());
g.setColor(getPeerBackground().darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x - 1, textRect.y + fm.getAscent() - 1);
}
}
protected void paintText(Graphics g, Rectangle textRect, String text) {
FontMetrics fm = g.getFontMetrics();
int mnemonicIndex = -1;
if(isEnabled()) {
/*** paint the text normally */
g.setColor(getPeerForeground());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
}
else {
/*** paint the text disabled ***/
g.setColor(getPeerBackground().brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x, textRect.y + fm.getAscent());
g.setColor(getPeerBackground().darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x - 1, textRect.y + fm.getAscent() - 1);
}
}
protected void paintText(Graphics g, Rectangle textRect, String text) {
FontMetrics fm = g.getFontMetrics();
int mnemonicIndex = -1;
if(isEnabled()) {
/*** paint the text normally */
g.setColor(getPeerForeground());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
}
else {
/*** paint the text disabled ***/
g.setColor(getPeerBackground().brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x, textRect.y + fm.getAscent());
g.setColor(getPeerBackground().darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x - 1, textRect.y + fm.getAscent() - 1);
}
}
private static void testDrawEmptyString() {
JLabel label = new JLabel();
BufferedImage buffImage = createBufferedImage(50, 50);
Graphics2D g2 = buffImage.createGraphics();
g2.setColor(DRAW_COLOR);
BasicGraphicsUtils.drawString(null, g2, null, 0, 0);
BasicGraphicsUtils.drawString(label, g2, null, 0, 0);
BasicGraphicsUtils.drawString(null, g2, "", 0, 0);
BasicGraphicsUtils.drawString(label, g2, "", 0, 0);
BasicGraphicsUtils.drawStringUnderlineCharAt(null, g2, null, 3, 0, 0);
BasicGraphicsUtils.drawStringUnderlineCharAt(label, g2, null, 3, 0, 0);
BasicGraphicsUtils.drawStringUnderlineCharAt(null, g2, "", 3, 0, 0);
BasicGraphicsUtils.drawStringUnderlineCharAt(label, g2, "", 3, 0, 0);
g2.dispose();
checkImageIsEmpty(buffImage);
}
private static void checkNullArgumentsDrawStringUnderlineCharAt(
JComponent comp, Graphics2D g, String text) {
int x = 50;
int y = 50;
BasicGraphicsUtils.drawStringUnderlineCharAt(null, g, text, 1, x, y);
BasicGraphicsUtils.drawStringUnderlineCharAt(comp, g, null, 1, x, y);
try {
BasicGraphicsUtils.drawStringUnderlineCharAt(comp, null, text, 1, x, y);
} catch (NullPointerException e) {
return;
}
throw new RuntimeException("NPE is not thrown");
}
protected void paintText(Graphics g, Rectangle textRect, String text) {
FontMetrics fm = g.getFontMetrics();
int mnemonicIndex = -1;
if(isEnabled()) {
/*** paint the text normally */
g.setColor(getPeerForeground());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
}
else {
/*** paint the text disabled ***/
g.setColor(getPeerBackground().brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x, textRect.y + fm.getAscent());
g.setColor(getPeerBackground().darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x - 1, textRect.y + fm.getAscent() - 1);
}
}
@Override
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
int mnemonicIndex = b.getDisplayedMnemonicIndex();
if (model.isEnabled()) {
g.setColor(b.getForeground());
BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemonicIndex,
textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset());
} else {
g.setColor(disabledForeground);
BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemonicIndex,
textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset());
}
}
protected void paintText(Graphics g, Rectangle textRect, String text) {
FontMetrics fm = g.getFontMetrics();
int mnemonicIndex = -1;
if(isEnabled()) {
/*** paint the text normally */
g.setColor(getPeerForeground());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
}
else {
/*** paint the text disabled ***/
g.setColor(getPeerBackground().brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x, textRect.y + fm.getAscent());
g.setColor(getPeerBackground().darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x - 1, textRect.y + fm.getAscent() - 1);
}
}
@Override
protected void paintEnabledText(JLabel label, Graphics g, String s,
int textX, int textY) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
if (fShadowColor != null) {
g2.setColor(fShadowColor);
g2.setFont(label.getFont());
BasicGraphicsUtils.drawStringUnderlineCharAt(g2, s, -1, textX,
textY + 1);
}
g2.setColor(isParentWindowFocused(label) ? fFocusedTextColor
: fUnfocusedTextColor);
BasicGraphicsUtils.drawStringUnderlineCharAt(g2, s, -1, textX, textY);
g2.dispose();
}
protected void paintText(Graphics g, Rectangle textRect, String text) {
FontMetrics fm = g.getFontMetrics();
int mnemonicIndex = -1;
if(isEnabled()) {
/*** paint the text normally */
g.setColor(getPeerForeground());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
}
else {
/*** paint the text disabled ***/
g.setColor(getPeerBackground().brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x, textRect.y + fm.getAscent());
g.setColor(getPeerBackground().darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
textRect.x - 1, textRect.y + fm.getAscent() - 1);
}
}
/**
* Paints the specified text.
*
* @param g
* Graphics context.
* @param comp
* Component.
* @param textRect
* Text rectangle.
* @param text
* Text to paint.
* @param mnemonicIndex
* Mnemonic index.
* @param font
* Font to use.
* @param color
* Color to use.
* @param clip
* Optional clip. Can be <code>null</code>.
* @param transform
* Optional transform to apply. Can be <code>null</code>.
*/
private static void paintText(Graphics g, JComponent comp, Rectangle textRect, String text,
int mnemonicIndex, java.awt.Font font, java.awt.Color color, java.awt.Rectangle clip,
java.awt.geom.AffineTransform transform) {
if ((text == null) || (text.length() == 0)) {
return;
}
Graphics2D g2d = (Graphics2D) g.create();
g2d.setFont(font);
g2d.setColor(color);
// Important - it appears that there's a bug in OpenJDK where the FontMetrics reports
// ascent=0 after applying an AffineTransform. This is why the computation of offsets is
// done before applying the transform ¯\_(ツ)_/¯
int dx = textRect.x;
int dy = textRect.y + g2d.getFontMetrics().getAscent();
if (clip != null) {
// Use clip() instead of setClip() to respect the currently set clip shape
g2d.clip(clip);
}
if (transform != null) {
// Use transform() instead of setTransform() to chain transforms instead of wiping
g2d.transform(transform);
}
BasicGraphicsUtils.drawStringUnderlineCharAt(g2d, text, mnemonicIndex, dx, dy);
g2d.dispose();
}
private static void testDrawString(boolean underlined) {
String str = "AOB";
JComponent comp = createComponent(str);
BufferedImage buffImage = createBufferedImage(WIDTH, HEIGHT);
Graphics2D g2 = buffImage.createGraphics();
g2.setColor(DRAW_COLOR);
g2.setFont(comp.getFont());
FontMetrics fontMetrices = comp.getFontMetrics(comp.getFont());
float width = BasicGraphicsUtils.getStringWidth(comp, fontMetrices, str);
float x = (WIDTH - width) / 2;
int y = 3 * HEIGHT / 4;
if (underlined) {
BasicGraphicsUtils.drawStringUnderlineCharAt(comp, g2, str, 1, x, y);
} else {
BasicGraphicsUtils.drawString(comp, g2, str, x, y);
}
g2.dispose();
float xx = BasicGraphicsUtils.getStringWidth(comp, fontMetrices, "A") +
BasicGraphicsUtils.getStringWidth(comp, fontMetrices, "O")/2;
checkImageContainsSymbol(buffImage, (int) xx, underlined ? 3 : 2);
}
protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
FontMetrics fm = g.getFontMetrics();
int mnemIndex = b.getDisplayedMnemonicIndex();
if (model.isEnabled()) {
g.setColor(b.getForeground());
} else {
g.setColor(getDisabledTextColor());
}
BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent());
}
@Override
protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect,
boolean isSelected) {
g.setFont(font);
View v = getTextViewForTab(tabIndex);
if (v != null) {
// html
v.paint(g, textRect);
}
else {
// plain text
int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) {
if (isSelected)
g.setColor(whiteColor);
else
g.setColor(tabPane.getForegroundAt(tabIndex));
BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex, textRect.x, textRect.y + metrics.getAscent());
}
else { // tab disabled
g.setColor(tabPane.getBackgroundAt(tabIndex).brighter());
BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex, textRect.x, textRect.y + metrics.getAscent());
g.setColor(tabPane.getBackgroundAt(tabIndex).darker());
BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex, textRect.x - 1, textRect.y + metrics.getAscent() - 1);
}
}
}
@Override
protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) {
super.paintText(g, tabPlacement, font, metrics, tabIndex, title, textRect, isSelected);
int mnemIndex = this.tabPane.getDisplayedMnemonicIndexAt(tabIndex);
if(!tabPane.isEnabledAt(tabIndex)){
g.setColor(disabledForeground);
BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, mnemIndex,
textRect.x,textRect.y + metrics.getAscent());
}
}
@Override
protected void paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY) {
int mnemIndex = l.getDisplayedMnemonicIndex();
g.setColor(UIManager.getColor("Label.disabledForeground"));
// SwingUtilities2.drawStringUnderlineCharAt(l, g, s, mnemIndex, textX, textY);
BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX, textY); //This isn't deprecated
}
protected void paintText(
Graphics g,
AbstractButton b,
Rectangle textRect,
String text) {
ButtonModel model = b.getModel();
FontMetrics fm = g.getFontMetrics();
int mnemonicIndex = b.getDisplayedMnemonicIndex();
Color oldColor = g.getColor();
/* Draw the Text */
if (model.isEnabled()) {
/** * paint the text normally */
if (model.isSelected()) {
g.setColor(selectedForeground);
} else {
g.setColor(unselectedForeground);
}
} else {
g.setColor(unselectedForeground.darker());
}
//
BasicGraphicsUtils.drawStringUnderlineCharAt(
g,
text,
mnemonicIndex,
textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset());
//
// } else {
// g.setColor(b.getParent().getBackground().brighter());
// BasicGraphicsUtils.drawStringUnderlineCharAt(
// g,
// text,
// mnemonicIndex,
// textRect.x,
// textRect.y + fm.getAscent());
// g.setColor(b.getParent().getBackground().darker());
// BasicGraphicsUtils.drawStringUnderlineCharAt(
// g,
// text,
// mnemonicIndex,
// textRect.x - 1,
// textRect.y + fm.getAscent() - 1);
// }
g.setColor(oldColor);
}
/**
* Draw text with an emphasized background.
*
* @param g
* the Graphics context to draw with.
* @param foreground
* the foreground color.
* @param emphasis
* the emphasis color.
* @param s
* the text to draw.
* @param underlinedIndex
* the index to underline.
* @param x
* the x coordinate to draw at.
* @param y
* the y coordinate to draw at.
*/
public void drawEmphasizedText(Graphics g, Color foreground, Color emphasis, String s, int underlinedIndex, int x, int y) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setColor(emphasis);
BasicGraphicsUtils.drawStringUnderlineCharAt(g2d, s, underlinedIndex, x, y + 1);
g2d.setColor(foreground);
BasicGraphicsUtils.drawStringUnderlineCharAt(g2d, s, underlinedIndex, x, y);
}