Java Code Examples for javax.swing.plaf.basic.BasicGraphicsUtils#drawStringUnderlineCharAt()

The following examples show how to use javax.swing.plaf.basic.BasicGraphicsUtils#drawStringUnderlineCharAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XCheckboxPeer.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 2
Source File: XCheckboxPeer.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 3
Source File: EmphasizedLabelUI.java    From pumpernickel with MIT License 6 votes vote down vote up
@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();
}
 
Example 4
Source File: XCheckboxPeer.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 5
Source File: MaterialButtonUI.java    From material-ui-swing with MIT License 6 votes vote down vote up
@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());
	}
}
 
Example 6
Source File: XCheckboxPeer.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 7
Source File: bug8132119.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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");
}
 
Example 8
Source File: bug8132119.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 9
Source File: XCheckboxPeer.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 10
Source File: XCheckboxPeer.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 11
Source File: XCheckboxPeer.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 12
Source File: XCheckboxPeer.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 13
Source File: bug8132119.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: ButtonUI.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 15
Source File: CloseTabPaneEnhancedUI.java    From iBioSim with Apache License 2.0 5 votes vote down vote up
@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);

		}
	}
}
 
Example 16
Source File: MaterialTabbedPaneUI.java    From material-ui-swing with MIT License 5 votes vote down vote up
@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());
	}
}
 
Example 17
Source File: MaterialLabelUI.java    From material-ui-swing with MIT License 5 votes vote down vote up
@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
}
 
Example 18
Source File: SubstanceTextUtilities.java    From radiance with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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();
}
 
Example 19
Source File: IconPackagerButtonBarUI.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
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);
}
 
Example 20
Source File: SeaGlassGraphicsUtils.java    From seaglass with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}