Search in sources :

Example 51 with FontMetrics

use of java.awt.FontMetrics in project jdk8u_jdk by JetBrains.

the class CompositionArea method getCaretRectangle.

// returns a 0-width rectangle
private Rectangle getCaretRectangle(TextHitInfo caret) {
    int caretLocation = 0;
    TextLayout layout = composedTextLayout;
    if (layout != null) {
        caretLocation = Math.round(layout.getCaretInfo(caret)[0]);
    }
    Graphics g = getGraphics();
    FontMetrics metrics = null;
    try {
        metrics = g.getFontMetrics();
    } finally {
        g.dispose();
    }
    return new Rectangle(TEXT_ORIGIN_X + caretLocation, TEXT_ORIGIN_Y - metrics.getAscent(), 0, metrics.getAscent() + metrics.getDescent());
}
Also used : Graphics(java.awt.Graphics) FontMetrics(java.awt.FontMetrics) Rectangle(java.awt.Rectangle) Point(java.awt.Point) TextLayout(java.awt.font.TextLayout)

Example 52 with FontMetrics

use of java.awt.FontMetrics in project jdk8u_jdk by JetBrains.

the class LWTextComponentPeer method getMinimumSize.

public Dimension getMinimumSize(final int rows, final int columns) {
    final Insets insets;
    synchronized (getDelegateLock()) {
        insets = getTextComponent().getInsets();
    }
    final int borderHeight = insets.top + insets.bottom;
    final int borderWidth = insets.left + insets.right;
    final FontMetrics fm = getFontMetrics(getFont());
    return new Dimension(fm.charWidth(WIDE_CHAR) * columns + borderWidth, fm.getHeight() * rows + borderHeight);
}
Also used : Insets(java.awt.Insets) FontMetrics(java.awt.FontMetrics) Dimension(java.awt.Dimension)

Example 53 with FontMetrics

use of java.awt.FontMetrics in project jdk8u_jdk by JetBrains.

the class MotifGraphicsUtils method paintMenuItem.

/**
   * This method is not being used to paint menu item since
   * 6.0 This code left for compatibility only. Do not use or
   * override it, this will not cause any visible effect.
   */
public static void paintMenuItem(Graphics g, JComponent c, Icon checkIcon, Icon arrowIcon, Color background, Color foreground, int defaultTextIconGap) {
    JMenuItem b = (JMenuItem) c;
    ButtonModel model = b.getModel();
    Dimension size = b.getSize();
    Insets i = c.getInsets();
    Rectangle viewRect = new Rectangle(size);
    viewRect.x += i.left;
    viewRect.y += i.top;
    viewRect.width -= (i.right + viewRect.x);
    viewRect.height -= (i.bottom + viewRect.y);
    Rectangle iconRect = new Rectangle();
    Rectangle textRect = new Rectangle();
    Rectangle acceleratorRect = new Rectangle();
    Rectangle checkRect = new Rectangle();
    Rectangle arrowRect = new Rectangle();
    Font holdf = g.getFont();
    Font f = c.getFont();
    g.setFont(f);
    FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);
    FontMetrics fmAccel = SwingUtilities2.getFontMetrics(c, g, UIManager.getFont("MenuItem.acceleratorFont"));
    if (c.isOpaque()) {
        if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
            g.setColor(background);
        } else {
            g.setColor(c.getBackground());
        }
        g.fillRect(0, 0, size.width, size.height);
    }
    // get Accelerator text
    KeyStroke accelerator = b.getAccelerator();
    String acceleratorText = "";
    if (accelerator != null) {
        int modifiers = accelerator.getModifiers();
        if (modifiers > 0) {
            acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
            acceleratorText += "+";
        }
        acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode());
    }
    // layout the text and icon
    String text = layoutMenuItem(c, fm, b.getText(), fmAccel, acceleratorText, b.getIcon(), checkIcon, arrowIcon, b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, acceleratorRect, checkRect, arrowRect, b.getText() == null ? 0 : defaultTextIconGap, defaultTextIconGap);
    // Paint the Check
    Color holdc = g.getColor();
    if (checkIcon != null) {
        if (model.isArmed() || (c instanceof JMenu && model.isSelected()))
            g.setColor(foreground);
        checkIcon.paintIcon(c, g, checkRect.x, checkRect.y);
        g.setColor(holdc);
    }
    // Paint the Icon
    if (b.getIcon() != null) {
        Icon icon;
        if (!model.isEnabled()) {
            icon = b.getDisabledIcon();
        } else if (model.isPressed() && model.isArmed()) {
            icon = b.getPressedIcon();
            if (icon == null) {
                // Use default icon
                icon = b.getIcon();
            }
        } else {
            icon = b.getIcon();
        }
        if (icon != null) {
            icon.paintIcon(c, g, iconRect.x, iconRect.y);
        }
    }
    // Draw the Text
    if (text != null && !text.equals("")) {
        // Once BasicHTML becomes public, use BasicHTML.propertyKey
        // instead of the hardcoded string below!
        View v = (View) c.getClientProperty("html");
        if (v != null) {
            v.paint(g, textRect);
        } else {
            int mnemIndex = b.getDisplayedMnemonicIndex();
            if (!model.isEnabled()) {
                // *** paint the text disabled
                g.setColor(b.getBackground().brighter());
                SwingUtilities2.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x, textRect.y + fmAccel.getAscent());
                g.setColor(b.getBackground().darker());
                SwingUtilities2.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x - 1, textRect.y + fmAccel.getAscent() - 1);
            } else {
                // *** paint the text normally
                if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
                    g.setColor(foreground);
                } else {
                    g.setColor(b.getForeground());
                }
                SwingUtilities2.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent());
            }
        }
    }
    // Draw the Accelerator Text
    if (acceleratorText != null && !acceleratorText.equals("")) {
        //Get the maxAccWidth from the parent to calculate the offset.
        int accOffset = 0;
        Container parent = b.getParent();
        if (parent != null && parent instanceof JComponent) {
            JComponent p = (JComponent) parent;
            Integer maxValueInt = (Integer) p.getClientProperty(MotifGraphicsUtils.MAX_ACC_WIDTH);
            int maxValue = maxValueInt != null ? maxValueInt.intValue() : acceleratorRect.width;
            //Calculate the offset, with which the accelerator texts will be drawn with.
            accOffset = maxValue - acceleratorRect.width;
        }
        g.setFont(UIManager.getFont("MenuItem.acceleratorFont"));
        if (!model.isEnabled()) {
            // *** paint the acceleratorText disabled
            g.setColor(b.getBackground().brighter());
            SwingUtilities2.drawString(c, g, acceleratorText, acceleratorRect.x - accOffset, acceleratorRect.y + fm.getAscent());
            g.setColor(b.getBackground().darker());
            SwingUtilities2.drawString(c, g, acceleratorText, acceleratorRect.x - accOffset - 1, acceleratorRect.y + fm.getAscent() - 1);
        } else {
            // *** paint the acceleratorText normally
            if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
                g.setColor(foreground);
            } else {
                g.setColor(b.getForeground());
            }
            SwingUtilities2.drawString(c, g, acceleratorText, acceleratorRect.x - accOffset, acceleratorRect.y + fmAccel.getAscent());
        }
    }
    // Paint the Arrow
    if (arrowIcon != null) {
        if (model.isArmed() || (c instanceof JMenu && model.isSelected()))
            g.setColor(foreground);
        if (!(b.getParent() instanceof JMenuBar))
            arrowIcon.paintIcon(c, g, arrowRect.x, arrowRect.y);
    }
    g.setColor(holdc);
    g.setFont(holdf);
}
Also used : Insets(java.awt.Insets) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) View(javax.swing.text.View) Font(java.awt.Font) Container(java.awt.Container) FontMetrics(java.awt.FontMetrics)

Example 54 with FontMetrics

use of java.awt.FontMetrics in project jdk8u_jdk by JetBrains.

the class MotifGraphicsUtils method drawStringInRect.

static void drawStringInRect(JComponent c, Graphics g, String aString, int x, int y, int width, int height, int justification) {
    FontMetrics fontMetrics;
    int drawWidth, startX, startY, delta;
    if (g.getFont() == null) {
        //            throw new InconsistencyException("No font set");
        return;
    }
    fontMetrics = SwingUtilities2.getFontMetrics(c, g);
    if (fontMetrics == null) {
        //            throw new InconsistencyException("No metrics for Font " + font());
        return;
    }
    if (justification == CENTER) {
        drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
        if (drawWidth > width) {
            drawWidth = width;
        }
        startX = x + (width - drawWidth) / 2;
    } else if (justification == RIGHT) {
        drawWidth = SwingUtilities2.stringWidth(c, fontMetrics, aString);
        if (drawWidth > width) {
            drawWidth = width;
        }
        startX = x + width - drawWidth;
    } else {
        startX = x;
    }
    delta = (height - fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
    if (delta < 0) {
        delta = 0;
    }
    startY = y + height - delta - fontMetrics.getDescent();
    SwingUtilities2.drawString(c, g, aString, startX, startY);
}
Also used : FontMetrics(java.awt.FontMetrics)

Example 55 with FontMetrics

use of java.awt.FontMetrics in project jdk8u_jdk by JetBrains.

the class MotifPopupMenuUI method getPreferredSize.

/* This has to deal with the fact that the title may be wider than
       the widest child component.
       */
public Dimension getPreferredSize(JComponent c) {
    LayoutManager layout = c.getLayout();
    Dimension d = layout.preferredLayoutSize(c);
    String title = ((JPopupMenu) c).getLabel();
    if (titleFont == null) {
        UIDefaults table = UIManager.getLookAndFeelDefaults();
        titleFont = table.getFont("PopupMenu.font");
    }
    FontMetrics fm = c.getFontMetrics(titleFont);
    int stringWidth = 0;
    if (title != null) {
        stringWidth += SwingUtilities2.stringWidth(c, fm, title);
    }
    if (d.width < stringWidth) {
        d.width = stringWidth + 8;
        Insets i = c.getInsets();
        if (i != null) {
            d.width += i.left + i.right;
        }
        if (border != null) {
            i = border.getBorderInsets(c);
            d.width += i.left + i.right;
        }
        return d;
    }
    return null;
}
Also used : Insets(java.awt.Insets) LayoutManager(java.awt.LayoutManager) FontMetrics(java.awt.FontMetrics) Dimension(java.awt.Dimension) Point(java.awt.Point)

Aggregations

FontMetrics (java.awt.FontMetrics)179 Font (java.awt.Font)61 Graphics2D (java.awt.Graphics2D)35 Point (java.awt.Point)34 Dimension (java.awt.Dimension)29 Color (java.awt.Color)23 Rectangle (java.awt.Rectangle)21 Rectangle2D (java.awt.geom.Rectangle2D)21 Insets (java.awt.Insets)20 Graphics (java.awt.Graphics)16 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)14 GradientPaint (java.awt.GradientPaint)14 MPart (gov.sandia.n2a.eqset.MPart)13 NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)12 Paint (java.awt.Paint)10 JTree (javax.swing.JTree)10 BasicStroke (java.awt.BasicStroke)9 BufferedImage (java.awt.image.BufferedImage)9 PanelModel (gov.sandia.n2a.ui.eq.PanelModel)8 JLabel (javax.swing.JLabel)8