下面列出了怎么用javax.swing.plaf.basic.BasicGraphicsUtils的API类实例代码及写法,或者点击链接到github查看源代码。
private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
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 void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
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 void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
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 void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
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 void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
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 void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
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 testStringWidth() {
String str = "12345678910\u036F";
JComponent comp = createComponent(str);
Font font = comp.getFont();
FontMetrics fontMetrics = comp.getFontMetrics(font);
float stringWidth = BasicGraphicsUtils.getStringWidth(comp, fontMetrics, str);
if (stringWidth == fontMetrics.stringWidth(str)) {
throw new RuntimeException("Numeric shaper is not used!");
}
if (stringWidth != getLayoutWidth(str, font, NUMERIC_SHAPER)) {
throw new RuntimeException("Wrong text width!");
}
}
private static void testStringClip() {
String str = "1234567890";
JComponent comp = createComponent(str);
FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());
int width = (int) BasicGraphicsUtils.getStringWidth(comp, fontMetrics, str);
String clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, width);
checkClippedString(str, clip, str);
clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, width + 1);
checkClippedString(str, clip, str);
clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, -1);
checkClippedString(str, clip, "...");
clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, 0);
checkClippedString(str, clip, "...");
clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics,
str, width - width / str.length());
int endIndex = str.length() - 3;
checkClippedString(str, clip, str.substring(0, endIndex) + "...");
}
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 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");
}
private static void checkNullArgumentsGetClippedString(
JComponent comp, String text) {
FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());
BasicGraphicsUtils.getClippedString(null, fontMetrics, text, 1);
String result = BasicGraphicsUtils.getClippedString(comp, fontMetrics, null, 1);
if (!"".equals(result)) {
throw new RuntimeException("Empty string is not returned!");
}
try {
BasicGraphicsUtils.getClippedString(comp, null, text, 1);
} catch (NullPointerException e) {
return;
}
throw new RuntimeException("NPE is not thrown");
}
private static void checkNullArgumentsGetStringWidth(JComponent comp,
String text) {
FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());
BasicGraphicsUtils.getStringWidth(null, fontMetrics, text);
float result = BasicGraphicsUtils.getStringWidth(comp, fontMetrics, null);
if (result != 0) {
throw new RuntimeException("The string length is not 0");
}
try {
BasicGraphicsUtils.getStringWidth(comp, null, text);
} catch (NullPointerException e) {
return;
}
throw new RuntimeException("NPE is not thrown");
}
private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
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 void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
private void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
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 void paintFocus(Graphics g, int x, int y, int w, int h, Color notColor) {
Color bsColor = getBorderSelectionColor();
if (bsColor != null && (selected || !drawDashedFocusIndicator)) {
g.setColor(bsColor);
g.drawRect(x, y, w - 1, h - 1);
}
if (drawDashedFocusIndicator && notColor != null) {
if (treeBGColor != notColor) {
treeBGColor = notColor;
focusBGColor = new Color(~notColor.getRGB());
}
g.setColor(focusBGColor);
BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
}
}
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 paintFocus(
Graphics g,
AbstractButton b,
Rectangle viewRect,
Rectangle textRect,
Rectangle iconRect) {
if (b.getParent() instanceof JToolBar) {
// Windows doesn't draw the focus rect for buttons in a toolbar.
return;
}
// focus painted same color as text
int width = b.getWidth();
int height = b.getHeight();
g.setColor(getFocusColor());
BasicGraphicsUtils.drawDashedRect(
g,
dashedRectGapX,
dashedRectGapY,
width - dashedRectGapWidth,
height - dashedRectGapHeight);
}