Search in sources :

Example 6 with LineMetrics

use of java.awt.font.LineMetrics in project intellij-community by JetBrains.

the class TextPainter method drawHeaderOrFooterLine.

private double drawHeaderOrFooterLine(Graphics2D g, double x, double y, double w, String headerText, String alignment) {
    FontRenderContext fontRenderContext = g.getFontRenderContext();
    LineMetrics lineMetrics = getHeaderFooterLineMetrics(g);
    float lineHeight = lineMetrics.getHeight();
    if (myPerformActualDrawing) {
        headerText = convertHeaderText(headerText);
        g.setFont(myHeaderFont);
        g.setColor(Color.black);
        float descent = lineMetrics.getDescent();
        double width = myHeaderFont.getStringBounds(headerText, fontRenderContext).getWidth() + getCharWidth(g);
        float yPos = (float) (lineHeight - descent + y);
        if (PrintSettings.LEFT.equals(alignment)) {
            drawStringToGraphics(g, headerText, x, yPos);
        } else if (PrintSettings.CENTER.equals(alignment)) {
            drawStringToGraphics(g, headerText, (float) (x + (w - width) / 2), yPos);
        } else if (PrintSettings.RIGHT.equals(alignment)) {
            drawStringToGraphics(g, headerText, (float) (x + w - width), yPos);
        }
    }
    return lineHeight;
}
Also used : FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics)

Example 7 with LineMetrics

use of java.awt.font.LineMetrics in project intellij-community by JetBrains.

the class TextPainter method drawFooter.

private double drawFooter(Graphics2D g, Rectangle2D clip) {
    LineMetrics lineMetrics = getHeaderFooterLineMetrics(g);
    double w = clip.getWidth();
    double x = clip.getX();
    double y = clip.getY() + clip.getHeight();
    boolean wasDrawn = false;
    double h = 0;
    y -= lineMetrics.getHeight();
    String headerText2 = myPrintSettings.FOOTER_HEADER_TEXT2;
    if (headerText2 != null && headerText2.length() > 0 && PrintSettings.FOOTER.equals(myPrintSettings.FOOTER_HEADER_PLACEMENT2)) {
        h = drawHeaderOrFooterLine(g, x, y, w, headerText2, myPrintSettings.FOOTER_HEADER_ALIGNMENT2);
        wasDrawn = true;
    }
    String headerText1 = myPrintSettings.FOOTER_HEADER_TEXT1;
    if (headerText1 != null && headerText1.length() > 0 && PrintSettings.FOOTER.equals(myPrintSettings.FOOTER_HEADER_PLACEMENT1)) {
        y -= lineMetrics.getHeight();
        if (PrintSettings.LEFT.equals(myPrintSettings.FOOTER_HEADER_ALIGNMENT1) && PrintSettings.RIGHT.equals(myPrintSettings.FOOTER_HEADER_ALIGNMENT2) && wasDrawn) {
            y += h;
        }
        drawHeaderOrFooterLine(g, x, y, w, headerText1, myPrintSettings.FOOTER_HEADER_ALIGNMENT1);
        wasDrawn = true;
    }
    return wasDrawn ? clip.getY() + clip.getHeight() - y + lineMetrics.getHeight() / 4 : 0;
}
Also used : LineMetrics(java.awt.font.LineMetrics)

Example 8 with LineMetrics

use of java.awt.font.LineMetrics in project jdk8u_jdk by JetBrains.

the class ExtendedTextSourceLabel method finishInit.

private void finishInit() {
    font = source.getFont();
    Map<TextAttribute, ?> atts = font.getAttributes();
    baseTX = AttributeValues.getBaselineTransform(atts);
    if (baseTX == null) {
        cm = source.getCoreMetrics();
    } else {
        AffineTransform charTX = AttributeValues.getCharTransform(atts);
        if (charTX == null) {
            charTX = new AffineTransform();
        }
        font = font.deriveFont(charTX);
        LineMetrics lm = font.getLineMetrics(source.getChars(), source.getStart(), source.getStart() + source.getLength(), source.getFRC());
        cm = CoreMetrics.get(lm);
    }
}
Also used : TextAttribute(java.awt.font.TextAttribute) AffineTransform(java.awt.geom.AffineTransform) LineMetrics(java.awt.font.LineMetrics)

Example 9 with LineMetrics

use of java.awt.font.LineMetrics in project JMRI by JMRI.

the class GraphPane method drawGraph.

protected void drawGraph(Graphics g) {
    if (!(g instanceof Graphics2D)) {
        throw new IllegalArgumentException("Graphics object passed is not the correct type");
    }
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    int w = getWidth();
    int h = getHeight();
    // Draw ordinate (y-axis).
    g2.draw(new Line2D.Double(PAD, PAD, PAD, h - PAD));
    // Draw abcissa (x-axis).
    g2.draw(new Line2D.Double(PAD, h - PAD, w - PAD, h - PAD));
    // Draw labels.
    Font font = g2.getFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = font.getLineMetrics("0", frc);
    float[] dash1 = { 1.0f };
    BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
    BasicStroke plain = new BasicStroke(1.0f);
    float sh = lm.getAscent() + lm.getDescent();
    // Ordinate (y-axis) label.
    float sy = PAD + ((h - 2 * PAD) - yLabel.length() * sh) / 2 + lm.getAscent();
    g2.setPaint(Color.green.darker());
    for (int i = 0; i < yLabel.length(); i++) {
        String letter = String.valueOf(yLabel.charAt(i));
        float sw = (float) font.getStringBounds(letter, frc).getWidth();
        float sx = (PAD / 2 - sw) / 2;
        g2.drawString(letter, sx, sy);
        sy += sh;
    }
    // Abcissa (x-axis) label.
    sy = h - PAD / 2 + (PAD / 2 - sh) / 2 + lm.getAscent();
    float sw = (float) font.getStringBounds(xLabel, frc).getWidth();
    float sx = (w - sw) / 2;
    g2.drawString(xLabel, sx, sy);
    // find the maximum of all profiles
    float maxSpeed = 0;
    for (int i = 0; i < _sp.length; i++) {
        maxSpeed = Math.max(_sp[i].getMax(), maxSpeed);
    }
    // Used to scale values into drawing area
    float scale = (h - 2 * PAD) / maxSpeed;
    // space between values along the ordinate (y-axis)
    // start with an increment of 1
    // Plot a grid line every two
    // Plot a label every ten
    float yInc = scale;
    int yMod = 10;
    int gridMod = 2;
    if (units == Speed.MPH) {
        // need inverse transform here
        yInc = Speed.mphToKph(yInc);
    }
    if ((units == Speed.KPH) && (maxSpeed > 100) || (units == Speed.MPH) && (maxSpeed > 160)) {
        log.debug("Adjusting Y axis spacing for max speed");
        yMod *= 2;
        gridMod *= 2;
    }
    String ordString;
    // Draw lines
    for (int i = 0; i <= (h - 2 * PAD) / yInc; i++) {
        g2.setPaint(Color.green.darker());
        g2.setStroke(plain);
        float y1 = h - PAD - i * yInc;
        if ((i % yMod) == 0) {
            g2.draw(new Line2D.Double(7 * PAD / 8, y1, PAD, y1));
            ordString = Integer.toString(i);
            sw = (float) font.getStringBounds(ordString, frc).getWidth();
            sx = 7 * PAD / 8 - sw;
            sy = y1 + lm.getAscent() / 2;
            g2.drawString(ordString, sx, sy);
        }
        if (_grid && (i > 0) && ((i % gridMod) == 0)) {
            // Horizontal grid lines
            g2.setPaint(Color.LIGHT_GRAY);
            if ((i % yMod) != 0) {
                g2.setStroke(dashed);
            }
            g2.draw(new Line2D.Double(PAD, y1, w - PAD, y1));
        }
    }
    if (_grid) {
        // Close the top
        g2.setPaint(Color.LIGHT_GRAY);
        g2.setStroke(dashed);
        g2.draw(new Line2D.Double(PAD, PAD, w - PAD, PAD));
    }
    // The space between values along the abcissa (x-axis).
    float xInc = (float) (w - 2 * PAD) / (_sp[0].getLength() - 1);
    String abString;
    // for each point in a profile
    for (int i = 0; i < _sp[0].getLength(); i++) {
        g2.setPaint(Color.green.darker());
        g2.setStroke(plain);
        float x1 = 0.0F;
        // for each profile in the array
        for (int j = 0; j < _sp.length; j++) {
            x1 = PAD + i * xInc;
            float y1 = h - PAD - scale * _sp[j].getPoint(i);
            float x2 = PAD + (i + 1) * xInc;
            float y2 = h - PAD - scale * _sp[j].getPoint(i + 1);
            // if it's a valid data point
            if (i <= _sp[j].getLast() - 1) {
                g2.draw(new Line2D.Double(x1, y1, x2, y2));
            }
        }
        // tick marks along abcissa
        g2.draw(new Line2D.Double(x1, h - 7 * PAD / 8, x1, h - PAD));
        if (((i % 5) == 0) || (i == _sp[0].getLength() - 1)) {
            // abcissa labels every 5 ticks
            abString = Integer.toString(i);
            sw = (float) font.getStringBounds(abString, frc).getWidth();
            sx = x1 - sw / 2;
            sy = h - PAD + (PAD / 2 - sh) / 2 + lm.getAscent();
            g2.drawString(abString, sx, sy);
        }
        if (_grid && (i > 0)) {
            // Verical grid line
            g2.setPaint(Color.LIGHT_GRAY);
            if ((i % 5) != 0) {
                g2.setStroke(dashed);
            }
            g2.draw(new Line2D.Double(x1, PAD, x1, h - PAD));
        }
    }
    g2.setStroke(plain);
    // for each point in a profile
    for (int i = 0; i <= _sp[0].getLength(); i++) {
        // for each profile in the array
        for (int j = 0; j < _sp.length; j++) {
            g2.setPaint(colors[j]);
            float x = PAD + i * xInc;
            float y = h - PAD - scale * _sp[j].getPoint(i);
            // if it's a valid data point
            if (i <= _sp[j].getLast()) {
                g2.fill(new Ellipse2D.Double(x - 2, y - 2, 4, 4));
            }
        }
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Line2D(java.awt.geom.Line2D) Font(java.awt.Font) Ellipse2D(java.awt.geom.Ellipse2D) Graphics2D(java.awt.Graphics2D) FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics)

Example 10 with LineMetrics

use of java.awt.font.LineMetrics in project jgnash by ccavanaugh.

the class PrintableCheckLayout method drawPayee.

private void drawPayee(final Graphics2D g2, final CheckObject object, final float offset, final String payee) {
    float payeeX = object.getX();
    float payeeY = object.getY() + offset;
    if (payee != null && !payee.isEmpty()) {
        TextLayout textLayout = new TextLayout(payee, font, frc);
        textLayout.draw(g2, payeeX, payeeY);
    }
    if (testPrint) {
        TextLayout payeeText = new TextLayout("ORDER OF", testPrintFont, frc);
        double width = payeeText.getBounds().getWidth();
        payeeText.draw(g2, (float) (payeeX - width - space), payeeY);
        LineMetrics metrics = testPrintFont.getLineMetrics("PAY TO THE", frc);
        float y = payeeY - (float) payeeText.getBounds().getHeight() - metrics.getDescent() - metrics.getLeading();
        payeeText = new TextLayout("PAY TO THE", testPrintFont, frc);
        payeeText.draw(g2, (float) (payeeX - width - space), y);
    }
}
Also used : LineMetrics(java.awt.font.LineMetrics) TextLayout(java.awt.font.TextLayout)

Aggregations

LineMetrics (java.awt.font.LineMetrics)10 FontRenderContext (java.awt.font.FontRenderContext)5 BasicStroke (java.awt.BasicStroke)1 Font (java.awt.Font)1 Graphics2D (java.awt.Graphics2D)1 TextAttribute (java.awt.font.TextAttribute)1 TextLayout (java.awt.font.TextLayout)1 AffineTransform (java.awt.geom.AffineTransform)1 Ellipse2D (java.awt.geom.Ellipse2D)1 Line2D (java.awt.geom.Line2D)1 Rectangle2D (java.awt.geom.Rectangle2D)1