java.awt.font.LineMetrics Java Examples

The following examples show how to use java.awt.font.LineMetrics. 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: StandardGlyphVector.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example #2
Source File: MarkerAxisBand.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * A utility method that draws a string inside a rectangle.
 *
 * @param g2  the graphics device.
 * @param bounds  the rectangle.
 * @param font  the font.
 * @param text  the text.
 */
private void drawStringInRect(Graphics2D g2, Rectangle2D bounds, Font font,
                              String text) {

    g2.setFont(font);
    FontMetrics fm = g2.getFontMetrics(font);
    Rectangle2D r = TextUtilities.getTextBounds(text, g2, fm);
    double x = bounds.getX();
    if (r.getWidth() < bounds.getWidth()) {
        x = x + (bounds.getWidth() - r.getWidth()) / 2;
    }
    LineMetrics metrics = font.getLineMetrics(
        text, g2.getFontRenderContext()
    );
    g2.drawString(
        text, (float) x, (float) (bounds.getMaxY()
            - this.bottomInnerGap - metrics.getDescent())
    );
}
 
Example #3
Source File: StandardGlyphVector.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example #4
Source File: UnderlineTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void paintComponent(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, fpd.width, fpd.height);

    g.setColor(Color.RED);
    FontRenderContext frc = ((Graphics2D)g).getFontRenderContext();
    LineMetrics lm = f.getLineMetrics(fps, frc);
    int h = (int)(fpd.height - 20 - lm.getAscent());
    g.drawLine(20, h, fpd.width - 20, h);
    h = fpd.height - 20;
    g.drawLine(20, h, fpd.width - 20, h);
    h = (int)(fpd.height - 20 + lm.getDescent());
    g.drawLine(20, h, fpd.width - 20, h);

    g.setColor(Color.BLACK);
    g.setFont(f);
    g.drawString(fps, 50, fpd.height - 20);
}
 
Example #5
Source File: StandardGlyphVector.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example #6
Source File: StandardGlyphVector.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example #7
Source File: MarkerAxisBand.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * A utility method that draws a string inside a rectangle.
 *
 * @param g2  the graphics device.
 * @param bounds  the rectangle.
 * @param font  the font.
 * @param text  the text.
 */
private void drawStringInRect(Graphics2D g2, Rectangle2D bounds, Font font,
                              String text) {

    g2.setFont(font);
    FontMetrics fm = g2.getFontMetrics(font);
    Rectangle2D r = TextUtilities.getTextBounds(text, g2, fm);
    double x = bounds.getX();
    if (r.getWidth() < bounds.getWidth()) {
        x = x + (bounds.getWidth() - r.getWidth()) / 2;
    }
    LineMetrics metrics = font.getLineMetrics(
        text, g2.getFontRenderContext()
    );
    g2.drawString(
        text, (float) x, (float) (bounds.getMaxY()
            - this.bottomInnerGap - metrics.getDescent())
    );
}
 
Example #8
Source File: MarkerAxisBand.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * A utility method that draws a string inside a rectangle.
 *
 * @param g2  the graphics device.
 * @param bounds  the rectangle.
 * @param font  the font.
 * @param text  the text.
 */
private void drawStringInRect(Graphics2D g2, Rectangle2D bounds, Font font,
                              String text) {

    g2.setFont(font);
    FontMetrics fm = g2.getFontMetrics(font);
    Rectangle2D r = TextUtilities.getTextBounds(text, g2, fm);
    double x = bounds.getX();
    if (r.getWidth() < bounds.getWidth()) {
        x = x + (bounds.getWidth() - r.getWidth()) / 2;
    }
    LineMetrics metrics = font.getLineMetrics(
        text, g2.getFontRenderContext()
    );
    g2.drawString(
        text, (float) x, (float) (bounds.getMaxY()
            - this.bottomInnerGap - metrics.getDescent())
    );
}
 
Example #9
Source File: StandardGlyphVector.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example #10
Source File: TextUtilities.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the bounds for the specified text.
 * 
 * @param text  the text (<code>null</code> permitted).
 * @param g2  the graphics context (not <code>null</code>).
 * @param fm  the font metrics (not <code>null</code>).
 * 
 * @return The text bounds (<code>null</code> if the <code>text</code> 
 *         argument is <code>null</code>).
 */
public static Rectangle2D getTextBounds(String text, Graphics2D g2, 
        FontMetrics fm) {
    
    final Rectangle2D bounds;
    if (TextUtilities.useFontMetricsGetStringBounds) {
        bounds = fm.getStringBounds(text, g2);
        // getStringBounds() can return incorrect height for some Unicode
        // characters...see bug parade 6183356, let's replace it with 
        // something correct
        LineMetrics lm = fm.getFont().getLineMetrics(text,
                g2.getFontRenderContext());
        bounds.setRect(bounds.getX(), bounds.getY(), bounds.getWidth(),
                lm.getHeight());
    }
    else {
        double width = fm.stringWidth(text);
        double height = fm.getHeight();
        bounds = new Rectangle2D.Double(0.0, -fm.getAscent(), width, 
                height);
    }
    return bounds;
}
 
Example #11
Source File: StandardGlyphVector.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example #12
Source File: StandardGlyphVector.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example #13
Source File: ValueAxis.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * A utility method for determining the height of the tallest tick label.
 *
 * @param ticks  the ticks.
 * @param g2  the graphics device.
 * @param drawArea  the area within which the plot and axes should be drawn.
 * @param vertical  a flag that indicates whether or not the tick labels
 *                  are 'vertical'.
 *
 * @return The height of the tallest tick label.
 */
protected double findMaximumTickLabelHeight(List ticks, Graphics2D g2,
        Rectangle2D drawArea, boolean vertical) {

    RectangleInsets insets = getTickLabelInsets();
    Font font = getTickLabelFont();
    g2.setFont(font);
    double maxHeight = 0.0;
    if (vertical) {
        FontMetrics fm = g2.getFontMetrics(font);
        Iterator iterator = ticks.iterator();
        while (iterator.hasNext()) {
            Tick tick = (Tick) iterator.next();
            Rectangle2D labelBounds = null;
            if (tick instanceof LogTick) {
                LogTick lt = (LogTick) tick;
                if (lt.getAttributedLabel() != null) {
                    labelBounds = AttrStringUtils.getTextBounds(
                            lt.getAttributedLabel(), g2);
                }
            } else if (tick.getText() != null) {
                labelBounds = TextUtilities.getTextBounds(
                        tick.getText(), g2, fm);
            }
            if (labelBounds != null && labelBounds.getWidth() 
                    + insets.getTop() + insets.getBottom() > maxHeight) {
                maxHeight = labelBounds.getWidth()
                            + insets.getTop() + insets.getBottom();
            }
        }
    } else {
        LineMetrics metrics = font.getLineMetrics("ABCxyz",
                g2.getFontRenderContext());
        maxHeight = metrics.getHeight()
                    + insets.getTop() + insets.getBottom();
    }
    return maxHeight;

}
 
Example #14
Source File: SpiderWebPlot.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the label for one axis.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value,
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();

    String label;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }

    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent,
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(),
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example #15
Source File: SpiderWebPlot.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws the label for one axis.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value,
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();

    String label = null;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }

    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent,
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(),
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example #16
Source File: MarkerAxisBand.java    From ECG-Viewer with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the height of the band.
 *
 * @param g2  the graphics device.
 *
 * @return The height of the band.
 */
public double getHeight(Graphics2D g2) {

    double result = 0.0;
    if (this.markers.size() > 0) {
        LineMetrics metrics = this.font.getLineMetrics(
            "123g", g2.getFontRenderContext()
        );
        result = this.topOuterGap + this.topInnerGap + metrics.getHeight()
                 + this.bottomInnerGap + this.bottomOuterGap;
    }
    return result;

}
 
Example #17
Source File: LogAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified 
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we 
 * just look at two values: the lower bound and the upper bound for the 
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 *
 * @since 1.0.7
 */
protected double estimateMaximumTickLabelWidth(Graphics2D g2, 
                                               TickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of the 
        // font)...
        FontRenderContext frc = g2.getFontRenderContext();
        LineMetrics lm = getTickLabelFont().getLineMetrics("0", frc);
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
        Range range = getRange();
        double lower = range.getLowerBound();
        double upper = range.getUpperBound();
        String lowerStr = "";
        String upperStr = "";
        NumberFormat formatter = getNumberFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.valueToString(lower);
            upperStr = unit.valueToString(upper);                
        }
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #18
Source File: MarkerAxisBand.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the height of the band.
 *
 * @param g2  the graphics device.
 *
 * @return The height of the band.
 */
public double getHeight(Graphics2D g2) {

    double result = 0.0;
    if (this.markers.size() > 0) {
        LineMetrics metrics = this.font.getLineMetrics(
            "123g", g2.getFontRenderContext()
        );
        result = this.topOuterGap + this.topInnerGap + metrics.getHeight()
                 + this.bottomInnerGap + this.bottomOuterGap;
    }
    return result;

}
 
Example #19
Source File: MarkerAxisBand.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the height of the band.
 *
 * @param g2  the graphics device.
 *
 * @return The height of the band.
 */
public double getHeight(Graphics2D g2) {

    double result = 0.0;
    if (this.markers.size() > 0) {
        LineMetrics metrics = this.font.getLineMetrics(
            "123g", g2.getFontRenderContext()
        );
        result = this.topOuterGap + this.topInnerGap + metrics.getHeight()
                 + this.bottomInnerGap + this.bottomOuterGap;
    }
    return result;

}
 
Example #20
Source File: NumberAxis.java    From SIMVA-SoS with Apache License 2.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
protected double estimateMaximumTickLabelWidth(Graphics2D g2,
                                               TickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of the
        // font)...
        FontRenderContext frc = g2.getFontRenderContext();
        LineMetrics lm = getTickLabelFont().getLineMetrics("0", frc);
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
        Range range = getRange();
        double lower = range.getLowerBound();
        double upper = range.getUpperBound();
        String lowerStr, upperStr;
        NumberFormat formatter = getNumberFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.valueToString(lower);
            upperStr = unit.valueToString(upper);
        }
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #21
Source File: DateAxis.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified 
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the 
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelWidth(Graphics2D g2, 
                                             DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of 
        // the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr = null;
        String upperStr = null;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #22
Source File: TextPainter.java    From consulo with Apache License 2.0 5 votes vote down vote up
private double drawHeader(Graphics2D g, Rectangle2D clip) {
  LineMetrics lineMetrics = getHeaderFooterLineMetrics(g);
  double w = clip.getWidth();
  double x = clip.getX();
  double y = clip.getY();
  double h = 0;
  boolean wasDrawn = false;

  String headerText1 = myPrintSettings.FOOTER_HEADER_TEXT1;
  if (headerText1 != null && headerText1.length() > 0 &&
      PrintSettings.HEADER.equals(myPrintSettings.FOOTER_HEADER_PLACEMENT1)) {
    h = drawHeaderOrFooterLine(g, x, y, w, headerText1, myPrintSettings.FOOTER_HEADER_ALIGNMENT1);
    wasDrawn = true;
    y += h;
  }

  String headerText2 = myPrintSettings.FOOTER_HEADER_TEXT2;
  if (headerText2 != null && headerText2.length() > 0 &&
      PrintSettings.HEADER.equals(myPrintSettings.FOOTER_HEADER_PLACEMENT2)) {
    if (PrintSettings.LEFT.equals(myPrintSettings.FOOTER_HEADER_ALIGNMENT1) &&
        PrintSettings.RIGHT.equals(myPrintSettings.FOOTER_HEADER_ALIGNMENT2) &&
        wasDrawn) {
      y -= h;
    }
    h = drawHeaderOrFooterLine(g, x, y, w, headerText2, myPrintSettings.FOOTER_HEADER_ALIGNMENT2);
    y += h;
    wasDrawn = true;
  }
  return wasDrawn ? y - clip.getY() + lineMetrics.getHeight() / 3 : 0;
}
 
Example #23
Source File: MarkerAxisBand.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the height of the band.
 *
 * @param g2  the graphics device.
 *
 * @return The height of the band.
 */
public double getHeight(Graphics2D g2) {

    double result = 0.0;
    if (this.markers.size() > 0) {
        LineMetrics metrics = this.font.getLineMetrics(
            "123g", g2.getFontRenderContext()
        );
        result = this.topOuterGap + this.topInnerGap + metrics.getHeight()
                 + this.bottomInnerGap + this.bottomOuterGap;
    }
    return result;

}
 
Example #24
Source File: SpiderWebPlot.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Draws the label for one axis.
 * 
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value, 
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();
 
    String label = null;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }
 
    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent, 
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(), 
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example #25
Source File: LogAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 *
 * @since 1.0.7
 */
protected double estimateMaximumTickLabelWidth(Graphics2D g2,
                                               TickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of the
        // font)...
        FontRenderContext frc = g2.getFontRenderContext();
        LineMetrics lm = getTickLabelFont().getLineMetrics("0", frc);
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
        Range range = getRange();
        double lower = range.getLowerBound();
        double upper = range.getUpperBound();
        String lowerStr = "";
        String upperStr = "";
        NumberFormat formatter = getNumberFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.valueToString(lower);
            upperStr = unit.valueToString(upper);
        }
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #26
Source File: SpiderWebPlot.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the label for one axis.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value,
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();

    String label;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }

    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent,
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(),
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example #27
Source File: DateAxis.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelWidth(Graphics2D g2, 
        DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of
        // the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr, upperStr;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #28
Source File: FontMetricsCache.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private synchronized void initStrikethrough(Graphics g) {
    LineMetrics lm = font.getLineMetrics("aAyY", ((Graphics2D)g).getFontRenderContext()); // NOI18N
    strikethroughOffset = lm.getStrikethroughOffset();
    strikethroughThickness = lm.getStrikethroughThickness();
    inited |= ST_INITED;
}
 
Example #29
Source File: StandardCategoryAxis3D.java    From orson-charts with GNU General Public License v3.0 4 votes vote down vote up
private double drawParallelTickLabels(Graphics2D g2, Line2D axisLine,
        Point2D opposingPt, List<TickData> tickData, 
        double maxTickLabelWidth, RenderingInfo info, boolean hinting) {
    int levels = 1;
    LineMetrics lm = g2.getFontMetrics().getLineMetrics("123", g2);
    double height = lm.getHeight();
    if (tickData.size() > 1) {
    
        // work out how many offset levels we need to display the 
        // categories without overlapping
        Point2D p0 = tickData.get(0).getAnchorPt();
        Point2D pN = tickData.get(tickData.size() - 1).getAnchorPt();
        double availableWidth = pN.distance(p0) 
                * tickData.size() / (tickData.size() - 1);
        int labelsPerLevel = (int) Math.floor(availableWidth / 
                (maxTickLabelWidth * tickLabelFactor));
        int levelsRequired = this.maxTickLabelLevels;
        if (labelsPerLevel > 0) {
            levelsRequired = this.categories.size() / labelsPerLevel + 1;
        }
        levels = Math.min(levelsRequired, this.maxTickLabelLevels);
    }
    
    int index = 0;
    for (TickData t : tickData) {
        int level = index % levels;
        double adj = height * (level + 0.5);
        Line2D perpLine = Utils2D.createPerpendicularLine(axisLine, 
                t.getAnchorPt(), this.tickMarkLength 
                + this.tickLabelOffset + adj, opposingPt);
        double axisTheta = Utils2D.calculateTheta(axisLine);
        TextAnchor textAnchor = TextAnchor.CENTER;
        if (axisTheta >= Math.PI / 2.0) {
            axisTheta = axisTheta - Math.PI;
        } else if (axisTheta <= -Math.PI / 2) {
            axisTheta = axisTheta + Math.PI;  
        }
        String tickLabel = t.getKeyLabel();
        if (hinting) {
            Map<String, String> m = new HashMap<>();
            m.put("ref", "{\"type\": \"categoryTickLabel\", \"axis\": \"" 
                    + axisStr() + "\", \"key\": \"" 
                    + t.getKey() + "\"}");
            g2.setRenderingHint(Chart3DHints.KEY_BEGIN_ELEMENT, m);
        }

        Shape bounds = TextUtils.drawRotatedString(tickLabel, g2, 
                (float) perpLine.getX2(), (float) perpLine.getY2(), 
                textAnchor, axisTheta, textAnchor);
        if (hinting) {
            g2.setRenderingHint(Chart3DHints.KEY_END_ELEMENT, true);
        }
        if (info != null) {
            RenderedElement tickLabelElement = new RenderedElement(
                    InteractiveElementType.CATEGORY_AXIS_TICK_LABEL, bounds);
            tickLabelElement.setProperty("label", tickLabel);
            tickLabelElement.setProperty("axis", axisStr());
            info.addOffsetElement(tickLabelElement);
        }
        index++;
    }
    return height * levels;
}
 
Example #30
Source File: TextUtils.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param text  the text.
 * @param anchor  the anchor point.
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor) {

    float[] result = new float[2];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = getTextBounds(text, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isVerticalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;

}