下面列出了javax.swing.plaf.basic.BasicGraphicsUtils#drawString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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 checkNullArgumentsDrawString(JComponent comp, Graphics2D g,
String text) {
float x = 50;
float y = 50;
BasicGraphicsUtils.drawString(null, g, text, x, y);
BasicGraphicsUtils.drawString(comp, g, null, x, y);
try {
BasicGraphicsUtils.drawString(comp, null, text, x, y);
} catch (NullPointerException e) {
return;
}
throw new RuntimeException("NPE is not thrown");
}
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 drawString(Graphics g, String s, int accChar, int textX, int textY) {
UISettings.setupAntialiasing(g);
if (s.indexOf('\n') == -1)
BasicGraphicsUtils.drawString(g, s, accChar, textX, textY);
else {
String[] strs = splitStringByLines(s);
int height = g.getFontMetrics().getHeight();
// Only the first line can have the accel char
BasicGraphicsUtils.drawString(g, strs[0], accChar, textX, textY);
for (int i = 1; i < strs.length; i++) {
g.drawString(strs[i], textX, textY + (height * i));
}
}
}
public static void drawString(JComponent c, Graphics g, String text, int x, int y) {
BasicGraphicsUtils.drawString(c, getGraphics2D(g), text, x, y);
}
public static void drawString(JComponent c, Graphics2D g2d, String text, int x, int y) {
BasicGraphicsUtils.drawString(c, g2d, text, x, y);
}
protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY) {
int accChar = l.getDisplayedMnemonic();
g.setColor(l.getForeground());
BasicGraphicsUtils.drawString(g, s, accChar, textX, textY);
}
protected void paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY) {
int accChar = l.getDisplayedMnemonic();
g.setColor(l.getBackground());
BasicGraphicsUtils.drawString(g, s, accChar, textX, textY);
}