Search in sources :

Example 96 with FontMetrics

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

the class Utilities method drawTabbedText.

// In addition to the previous method it can extend spaces for
// justification.
//
// all params are the same as in the preious method except the last
// one:
// @param justificationData justificationData for the row.
// if null not justification is needed
static final int drawTabbedText(View view, Segment s, int x, int y, Graphics g, TabExpander e, int startOffset, int[] justificationData) {
    JComponent component = getJComponent(view);
    FontMetrics metrics = SwingUtilities2.getFontMetrics(component, g);
    int nextX = x;
    char[] txt = s.array;
    int txtOffset = s.offset;
    int flushLen = 0;
    int flushIndex = s.offset;
    int spaceAddon = 0;
    int spaceAddonLeftoverEnd = -1;
    int startJustifiableContent = 0;
    int endJustifiableContent = 0;
    if (justificationData != null) {
        int offset = -startOffset + txtOffset;
        View parent = null;
        if (view != null && (parent = view.getParent()) != null) {
            offset += parent.getStartOffset();
        }
        spaceAddon = justificationData[Row.SPACE_ADDON];
        spaceAddonLeftoverEnd = justificationData[Row.SPACE_ADDON_LEFTOVER_END] + offset;
        startJustifiableContent = justificationData[Row.START_JUSTIFIABLE] + offset;
        endJustifiableContent = justificationData[Row.END_JUSTIFIABLE] + offset;
    }
    int n = s.offset + s.count;
    for (int i = txtOffset; i < n; i++) {
        if (txt[i] == '\t' || ((spaceAddon != 0 || i <= spaceAddonLeftoverEnd) && (txt[i] == ' ') && startJustifiableContent <= i && i <= endJustifiableContent)) {
            if (flushLen > 0) {
                nextX = SwingUtilities2.drawChars(component, g, txt, flushIndex, flushLen, x, y);
                flushLen = 0;
            }
            flushIndex = i + 1;
            if (txt[i] == '\t') {
                if (e != null) {
                    nextX = (int) e.nextTabStop((float) nextX, startOffset + i - txtOffset);
                } else {
                    nextX += metrics.charWidth(' ');
                }
            } else if (txt[i] == ' ') {
                nextX += metrics.charWidth(' ') + spaceAddon;
                if (i <= spaceAddonLeftoverEnd) {
                    nextX++;
                }
            }
            x = nextX;
        } else if ((txt[i] == '\n') || (txt[i] == '\r')) {
            if (flushLen > 0) {
                nextX = SwingUtilities2.drawChars(component, g, txt, flushIndex, flushLen, x, y);
                flushLen = 0;
            }
            flushIndex = i + 1;
            x = nextX;
        } else {
            flushLen += 1;
        }
    }
    if (flushLen > 0) {
        nextX = SwingUtilities2.drawChars(component, g, txt, flushIndex, flushLen, x, y);
    }
    return nextX;
}
Also used : FontMetrics(java.awt.FontMetrics) JComponent(javax.swing.JComponent)

Example 97 with FontMetrics

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

the class Blink method paint.

@Override
public void paint(Graphics g) {
    int fontSize = g.getFont().getSize();
    int x = 0, y = fontSize, space;
    int red = (int) (50 * Math.random());
    int green = (int) (50 * Math.random());
    int blue = (int) (256 * Math.random());
    Dimension d = getSize();
    g.setColor(Color.black);
    FontMetrics fm = g.getFontMetrics();
    space = fm.stringWidth(" ");
    for (StringTokenizer t = new StringTokenizer(labelString); t.hasMoreTokens(); ) {
        String word = t.nextToken();
        int w = fm.stringWidth(word) + space;
        if (x + w > d.width) {
            x = 0;
            //move word to next line if it doesn't fit
            y += fontSize;
        }
        if (Math.random() < 0.5) {
            g.setColor(new java.awt.Color((red + y * 30) % 256, (green + x / 3) % 256, blue));
        } else {
            g.setColor(getBackground());
        }
        g.drawString(word, x, y);
        //shift to the right to draw the next word
        x += w;
    }
}
Also used : Color(java.awt.Color) StringTokenizer(java.util.StringTokenizer) FontMetrics(java.awt.FontMetrics) Dimension(java.awt.Dimension)

Example 98 with FontMetrics

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

the class TextComponentPrintable method createPrintShellOnEDT.

private JTextComponent createPrintShellOnEDT(final JTextComponent textComponent) {
    assert SwingUtilities.isEventDispatchThread();
    JTextComponent ret = null;
    if (textComponent instanceof JPasswordField) {
        ret = new JPasswordField() {

            {
                setEchoChar(((JPasswordField) textComponent).getEchoChar());
                setHorizontalAlignment(((JTextField) textComponent).getHorizontalAlignment());
            }

            @Override
            public FontMetrics getFontMetrics(Font font) {
                return (frc.get() == null) ? super.getFontMetrics(font) : FontDesignMetrics.getMetrics(font, frc.get());
            }
        };
    } else if (textComponent instanceof JTextField) {
        ret = new JTextField() {

            {
                setHorizontalAlignment(((JTextField) textComponent).getHorizontalAlignment());
            }

            @Override
            public FontMetrics getFontMetrics(Font font) {
                return (frc.get() == null) ? super.getFontMetrics(font) : FontDesignMetrics.getMetrics(font, frc.get());
            }
        };
    } else if (textComponent instanceof JTextArea) {
        ret = new JTextArea() {

            {
                JTextArea textArea = (JTextArea) textComponent;
                setLineWrap(textArea.getLineWrap());
                setWrapStyleWord(textArea.getWrapStyleWord());
                setTabSize(textArea.getTabSize());
            }

            @Override
            public FontMetrics getFontMetrics(Font font) {
                return (frc.get() == null) ? super.getFontMetrics(font) : FontDesignMetrics.getMetrics(font, frc.get());
            }
        };
    } else if (textComponent instanceof JTextPane) {
        ret = new JTextPane() {

            @Override
            public FontMetrics getFontMetrics(Font font) {
                return (frc.get() == null) ? super.getFontMetrics(font) : FontDesignMetrics.getMetrics(font, frc.get());
            }

            @Override
            public EditorKit getEditorKit() {
                if (getDocument() == textComponent.getDocument()) {
                    return ((JTextPane) textComponent).getEditorKit();
                } else {
                    return super.getEditorKit();
                }
            }
        };
    } else if (textComponent instanceof JEditorPane) {
        ret = new JEditorPane() {

            @Override
            public FontMetrics getFontMetrics(Font font) {
                return (frc.get() == null) ? super.getFontMetrics(font) : FontDesignMetrics.getMetrics(font, frc.get());
            }

            @Override
            public EditorKit getEditorKit() {
                if (getDocument() == textComponent.getDocument()) {
                    return ((JEditorPane) textComponent).getEditorKit();
                } else {
                    return super.getEditorKit();
                }
            }
        };
    }
    //want to occupy the whole width and height by text
    ret.setBorder(null);
    //set properties from the component to print
    ret.setOpaque(textComponent.isOpaque());
    ret.setEditable(textComponent.isEditable());
    ret.setEnabled(textComponent.isEnabled());
    ret.setFont(textComponent.getFont());
    ret.setBackground(textComponent.getBackground());
    ret.setForeground(textComponent.getForeground());
    ret.setComponentOrientation(textComponent.getComponentOrientation());
    if (ret instanceof JEditorPane) {
        ret.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, textComponent.getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES));
        ret.putClientProperty(JEditorPane.W3C_LENGTH_UNITS, textComponent.getClientProperty(JEditorPane.W3C_LENGTH_UNITS));
        ret.putClientProperty("charset", textComponent.getClientProperty("charset"));
    }
    ret.setDocument(textComponent.getDocument());
    return ret;
}
Also used : EditorKit(javax.swing.text.EditorKit) FontMetrics(java.awt.FontMetrics) JTextComponent(javax.swing.text.JTextComponent) Font(java.awt.Font)

Example 99 with FontMetrics

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

the class CardinalTextField method paint.

@Override
public void paint(Graphics g) {
    int w = getSize().width;
    int h = getSize().height;
    if (img == null) {
        super.paint(g);
        g.setColor(Color.black);
        FontMetrics fm = g.getFontMetrics();
        int x = (w - fm.stringWidth(calcString)) / 2;
        int y = h / 2;
        g.drawString(calcString, x, y);
    } else {
        g.drawImage(img, 0, 0, w, h, this);
    }
}
Also used : FontMetrics(java.awt.FontMetrics)

Example 100 with FontMetrics

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

the class KerningLeak method leak.

private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0)
            System.out.println("Starting iter " + (i + 1));
        for (int j = 0; j < 1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
Also used : HashMap(java.util.HashMap) TextAttribute(java.awt.font.TextAttribute) FontMetrics(java.awt.FontMetrics) JLabel(javax.swing.JLabel) Font(java.awt.Font)

Aggregations

FontMetrics (java.awt.FontMetrics)116 Font (java.awt.Font)43 Graphics2D (java.awt.Graphics2D)32 Point (java.awt.Point)29 Dimension (java.awt.Dimension)21 Rectangle2D (java.awt.geom.Rectangle2D)20 Color (java.awt.Color)18 Insets (java.awt.Insets)18 Rectangle (java.awt.Rectangle)17 GradientPaint (java.awt.GradientPaint)9 BufferedImage (java.awt.image.BufferedImage)9 Graphics (java.awt.Graphics)8 BasicStroke (java.awt.BasicStroke)6 Shape (java.awt.Shape)5 JLabel (javax.swing.JLabel)5 Canvas (java.awt.Canvas)4 BadLocationException (javax.swing.text.BadLocationException)4 Polygon (java.awt.Polygon)3 FontRenderContext (java.awt.font.FontRenderContext)3 ArrayList (java.util.ArrayList)3