Java Code Examples for java.awt.font.LineMetrics#getHeight()

The following examples show how to use java.awt.font.LineMetrics#getHeight() . 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: ValueAxis.java    From ECG-Viewer with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A utility method for determining the width of the widest 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 width of the tallest tick label.
 */
protected double findMaximumTickLabelWidth(List ticks, Graphics2D g2,
        Rectangle2D drawArea, boolean vertical) {

    RectangleInsets insets = getTickLabelInsets();
    Font font = getTickLabelFont();
    double maxWidth = 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.getLeft()
                    + insets.getRight() > maxWidth) {
                maxWidth = labelBounds.getWidth()
                           + insets.getLeft() + insets.getRight();
            }
        }
    } else {
        LineMetrics metrics = font.getLineMetrics("ABCxyz",
                g2.getFontRenderContext());
        maxWidth = metrics.getHeight()
                   + insets.getTop() + insets.getBottom();
    }
    return maxWidth;

}
 
Example 2
Source File: ValueAxis.java    From SIMVA-SoS with Apache License 2.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 3
Source File: MarkerAxisBand.java    From SIMVA-SoS 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 4
Source File: DateAxis.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.
 */
private double estimateMaximumTickLabelHeight(Graphics2D g2,
                                              DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getTop() + tickLabelInsets.getBottom();

    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 5
Source File: ValueAxis.java    From SIMVA-SoS with Apache License 2.0 5 votes vote down vote up
/**
 * A utility method for determining the width of the widest 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 width of the tallest tick label.
 */
protected double findMaximumTickLabelWidth(List ticks, Graphics2D g2,
        Rectangle2D drawArea, boolean vertical) {

    RectangleInsets insets = getTickLabelInsets();
    Font font = getTickLabelFont();
    double maxWidth = 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.getLeft()
                    + insets.getRight() > maxWidth) {
                maxWidth = labelBounds.getWidth()
                           + insets.getLeft() + insets.getRight();
            }
        }
    } else {
        LineMetrics metrics = font.getLineMetrics("ABCxyz",
                g2.getFontRenderContext());
        maxWidth = metrics.getHeight()
                   + insets.getTop() + insets.getBottom();
    }
    return maxWidth;

}
 
Example 6
Source File: LogAxis.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.
 *
 * @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...
        Range range = getRange();
        double lower = range.getLowerBound();
        double upper = range.getUpperBound();
        AttributedString lowerStr = createTickLabel(lower);
        AttributedString upperStr = createTickLabel(upper);
        double w1 = AttrStringUtils.getTextBounds(lowerStr, g2).getWidth();
        double w2 = AttrStringUtils.getTextBounds(upperStr, g2).getWidth();
        result += Math.max(w1, w2);
    }
    return result;
}
 
Example 7
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 8
Source File: TextPainter.java    From consulo with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: ValueAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A utility method for determining the width of the widest 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 width of the tallest tick label.
 */
protected double findMaximumTickLabelWidth(List ticks,
                                           Graphics2D g2,
                                           Rectangle2D drawArea,
                                           boolean vertical) {

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

}
 
Example 10
Source File: DateAxis.java    From ECG-Viewer 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.
 */
private double estimateMaximumTickLabelHeight(Graphics2D g2,
        DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getTop() + tickLabelInsets.getBottom();

    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 11
Source File: DateAxis.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.
 */
private double estimateMaximumTickLabelHeight(Graphics2D g2, 
                                              DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getTop() + tickLabelInsets.getBottom();

    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 12
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 13
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 14
Source File: DateAxis.java    From buffer_bci 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 15
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 16
Source File: MarkerAxisBand.java    From openstock 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 17
Source File: NumberAxis.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.
 */
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 18
Source File: ValueAxis.java    From buffer_bci 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 19
Source File: SurveyScale.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void drawScaleValues( final Graphics2D g2, final Rectangle2D area ) {
  g2.setPaint( getScaleValuePaint() );
  final Font valueFont = getScaleValueFont();
  if ( valueFont != null ) {
    g2.setFont( valueFont );
  } else if ( styleSheet != null ) {
    final String fontName = (String) styleSheet.getStyleProperty( TextStyleKeys.FONT );
    final int fontSize = styleSheet.getIntStyleProperty( TextStyleKeys.FONTSIZE, 10 );
    final boolean bold = styleSheet.getBooleanStyleProperty( TextStyleKeys.BOLD );
    final boolean italic = styleSheet.getBooleanStyleProperty( TextStyleKeys.ITALIC );
    int style = 0;
    if ( bold ) {
      style |= Font.BOLD;
    }
    if ( italic ) {
      style |= Font.ITALIC;
    }
    g2.setFont( new Font( fontName, style, fontSize ) );
  }

  final Font f = g2.getFont();
  final FontMetrics fm = g2.getFontMetrics( f );
  final FontRenderContext frc = g2.getFontRenderContext();
  final double y = area.getCenterY();

  final int highest = getHighest();
  for ( int i = getLowest(); i <= highest; i++ ) {
    final double x = valueToJava2D( i, area );
    final String text = String.valueOf( i );

    final float width;
    if ( useFontMetricsGetStringBounds ) {
      final Rectangle2D 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
      width = (float) bounds.getWidth();
    } else {
      width = fm.stringWidth( text );
    }

    final LineMetrics metrics = f.getLineMetrics( text, frc );
    final float descent = metrics.getDescent();
    final float leading = metrics.getLeading();
    final float yAdj = -descent - leading + (float) ( metrics.getHeight() / 2.0 );
    final float xAdj = -width / 2.0f;
    g2.drawString( text, (float) ( x + xAdj ), (float) ( y + yAdj ) );
  }
}
 
Example 20
Source File: NumberAxis3D.java    From orson-charts with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws the axis to the supplied graphics target ({@code g2}, with the
 * specified starting and ending points for the line.  This method is used
 * internally, you should not need to call it directly.
 *
 * @param g2  the graphics target ({@code null} not permitted).
 * @param pt0  the starting point ({@code null} not permitted).
 * @param pt1  the ending point ({@code null} not permitted).
 * @param opposingPt  an opposing point (to determine which side of the 
 *     axis line the labels should appear, {@code null} not permitted).
 * @param tickData  tick details ({@code null} not permitted).
 * @param info  an object to be populated with rendering info 
 *     ({@code null} permitted).
 * @param hinting  perform element hinting?
 */
@Override
public void draw(Graphics2D g2, Point2D pt0, Point2D pt1, 
        Point2D opposingPt, List<TickData> tickData, RenderingInfo info,
        boolean hinting) {
    
    if (!isVisible()) {
        return;
    }
    if (pt0.equals(pt1)) {
        return;
    }
    
    // draw a line for the axis
    g2.setStroke(getLineStroke());
    g2.setPaint(getLineColor());
    Line2D axisLine = new Line2D.Float(pt0, pt1);  
    g2.draw(axisLine);
    
    // draw the tick marks and labels
    g2.setFont(getTickLabelFont());
    // we track the max width or height of the labels to know how far to
    // offset the axis label when we draw it later
    double maxTickLabelDim = 0.0;
    if (getTickLabelOrientation().equals(LabelOrientation.PARALLEL)) {
        LineMetrics lm = g2.getFontMetrics().getLineMetrics("123", g2);
        maxTickLabelDim = lm.getHeight();
    }
    double tickMarkLength = getTickMarkLength();
    double tickLabelOffset = getTickLabelOffset();
    g2.setPaint(getTickMarkPaint());
    g2.setStroke(getTickMarkStroke());
    for (TickData t : tickData) {
        if (tickMarkLength > 0.0) {
            Line2D tickLine = Utils2D.createPerpendicularLine(axisLine, 
                   t.getAnchorPt(), tickMarkLength, opposingPt);
            g2.draw(tickLine);
        }
        String tickLabel = this.tickLabelFormatter.format(t.getDataValue());
        if (getTickLabelOrientation().equals(
                LabelOrientation.PERPENDICULAR)) {
            maxTickLabelDim = Math.max(maxTickLabelDim, 
                    g2.getFontMetrics().stringWidth(tickLabel));
        }
    }
        
    if (getTickLabelsVisible()) {
        g2.setPaint(getTickLabelColor());
        if (getTickLabelOrientation().equals(
                LabelOrientation.PERPENDICULAR)) {
            drawPerpendicularTickLabels(g2, axisLine, opposingPt, tickData,
                    info, hinting);
        } else {
            drawParallelTickLabels(g2, axisLine, opposingPt, tickData, 
                    info, hinting);
        }
    } else {
        maxTickLabelDim = 0.0;
    }

    // draw the axis label (if any)...
    if (getLabel() != null) {
        Shape labelBounds = drawAxisLabel(getLabel(), g2, axisLine, 
                opposingPt, maxTickLabelDim + tickMarkLength 
                + tickLabelOffset + getLabelOffset(), info, hinting);
    }
}