/** * Draws one of the lines on a multiline label/button. * * @param g the Graphics context of the component. * @param fm the font metrics. * @param rect the rectangle that the text needs to be painted in. * @param text the text that needs to be drawn. * @param hAlign the horizontal alignment. * @param y the y position of the text. * @param mnemonic the mnemonic. * * @return the x starting position of the text */ public static int drawLine( Graphics g, FontMetrics fm, Rectangle rect, String text, int hAlign, int y, int mnemonic) { int x = rect.x; if ( text != null) { int width = SwingUtilities.computeStringWidth( fm, text); if ( hAlign == SwingConstants.CENTER) { x = rect.x + ((rect.width - width) / 2); } else if ( hAlign == SwingConstants.RIGHT) { x = rect.x + (rect.width - width); } BasicGraphicsUtils.drawString( g, text, mnemonic, x, y); } return x; }
public static void drawStringUnderlineCharAt(JComponent c, Graphics g, String text, int underlinedIndex, int x, int y) { Graphics2D g2D = (Graphics2D) g; Object savedRenderingHint = null; if (AbstractLookAndFeel.getTheme().isTextAntiAliasingOn()) { savedRenderingHint = g2D.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING); g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, AbstractLookAndFeel.getTheme().getTextAntiAliasingHint()); } if (getJavaVersion() >= 1.6) { try { Class swingUtilities2Class = Class.forName("sun.swing.SwingUtilities2"); Class classParams[] = {JComponent.class, Graphics.class, String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE}; Method m = swingUtilities2Class.getMethod("drawStringUnderlineCharAt", classParams); Object methodParams[] = {c, g, text, new Integer(underlinedIndex), new Integer(x), new Integer(y)}; m.invoke(null, methodParams); } catch (Exception ex) { BasicGraphicsUtils.drawString(g, text, underlinedIndex, x, y); } } else if (getJavaVersion() >= 1.4) { BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, underlinedIndex, x, y); } else { BasicGraphicsUtils.drawString(g, text, underlinedIndex, x, y); } if (AbstractLookAndFeel.getTheme().isTextAntiAliasingOn()) { g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, savedRenderingHint); } }
@Override protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) { AbstractButton b = (AbstractButton) c; ButtonModel model = b.getModel(); FontMetrics fm = g.getFontMetrics(); /* Draw the Text */ if (model.isEnabled()) { /** * * paint the text normally */ g.setColor(b.getForeground()); drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent()); } else { /** * * paint the text disabled ** */ // g.setColor(getDisabledTextColor()); g.setColor(b.getForeground().brighter()); drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent()); } }
protected void paintText(Graphics g) { FontMetrics fm = g.getFontMetrics(); String toPaint = this.commandButton.getText(); // compute the insets int fullInsets = this.commandButton.getInsets().left; int pw = this.getPreferredSize(this.commandButton).width; int mw = this.getMinimumSize(this.commandButton).width; int w = this.commandButton.getWidth(); int h = this.commandButton.getHeight(); int insets = fullInsets - (pw - w) * (fullInsets - 2) / (pw - mw); // and the text rectangle Rectangle textRect = new Rectangle(insets, 1 + (h - fm.getHeight()) / 2, w - 2 * insets, fm.getHeight()); // show the first characters that fit into the available text rectangle while (true) { if (toPaint.length() == 0) break; int strWidth = fm.stringWidth(toPaint); if (strWidth <= textRect.width) break; toPaint = toPaint.substring(0, toPaint.length() - 1); } BasicGraphicsUtils.drawString(g, toPaint, -1, textRect.x, textRect.y + fm.getAscent()); }
BasicGraphicsUtils.drawString(g2d, titleToShow, -1, startX + 5, yOffset); } else { BasicGraphicsUtils.drawString(g2d, titleToShow, -1, startX + width - 5 - fm.stringWidth(titleToShow),
for (CommandButtonLayoutManager.TextLayoutInfo mainTextLayoutInfo : layoutInfo.textLayoutInfoList) { if (mainTextLayoutInfo.text != null) { BasicGraphicsUtils.drawString(g, mainTextLayoutInfo.text, -1, mainTextLayoutInfo.textRect.x, mainTextLayoutInfo.textRect.y + fm.getAscent()); for (CommandButtonLayoutManager.TextLayoutInfo extraTextLayoutInfo : layoutInfo.extraTextLayoutInfoList) { if (extraTextLayoutInfo.text != null) { BasicGraphicsUtils.drawString(g, extraTextLayoutInfo.text, -1, extraTextLayoutInfo.textRect.x, extraTextLayoutInfo.textRect.y + fm.getAscent());
BasicGraphicsUtils.drawString( g, acceleratorText, BasicGraphicsUtils.drawString( g, acceleratorText, acceleratorRect.y + fmAccel.getAscent()); g.setColor(b.getBackground().darker()); BasicGraphicsUtils.drawString( g, acceleratorText, BasicGraphicsUtils.drawString( g, acceleratorText,
BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent()); BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x + 1, textRect.y + fm.getAscent() + 1); g.setColor(b.getBackground().darker()); BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x, BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x,
BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x, BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x,