Java Code Examples for org.jfree.text.TextUtilities#getTextBounds()

The following examples show how to use org.jfree.text.TextUtilities#getTextBounds() . 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: SubCategoryAxis.java    From opensim-gui with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the maximum of the relevant dimension (height or width) of the 
 * subcategory labels.
 * 
 * @param g2  the graphics device.
 * @param edge  the edge.
 * 
 * @return The maximum dimension.
 */
private double getMaxDim(Graphics2D g2, RectangleEdge edge) {
    double result = 0.0;
    g2.setFont(this.subLabelFont);
    FontMetrics fm = g2.getFontMetrics();
    Iterator iterator = this.subCategories.iterator();
    while (iterator.hasNext()) {
        Comparable subcategory = (Comparable) iterator.next();
        String label = subcategory.toString();
        Rectangle2D bounds = TextUtilities.getTextBounds(label, g2, fm);
        double dim = 0.0;
        if (RectangleEdge.isLeftOrRight(edge)) {
            dim = bounds.getWidth();   
        }
        else {  // must be top or bottom
            dim = bounds.getHeight();
        }
        result = Math.max(result, dim);
    }   
    return result;
}
 
Example 2
Source File: SubCategoryAxis.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the maximum of the relevant dimension (height or width) of the
 * subcategory labels.
 *
 * @param g2  the graphics device.
 * @param edge  the edge.
 *
 * @return The maximum dimension.
 */
private double getMaxDim(Graphics2D g2, RectangleEdge edge) {
    double result = 0.0;
    g2.setFont(this.subLabelFont);
    FontMetrics fm = g2.getFontMetrics();
    Iterator iterator = this.subCategories.iterator();
    while (iterator.hasNext()) {
        Comparable subcategory = (Comparable) iterator.next();
        String label = subcategory.toString();
        Rectangle2D bounds = TextUtilities.getTextBounds(label, g2, fm);
        double dim;
        if (RectangleEdge.isLeftOrRight(edge)) {
            dim = bounds.getWidth();
        }
        else {  // must be top or bottom
            dim = bounds.getHeight();
        }
        result = Math.max(result, dim);
    }
    return result;
}
 
Example 3
Source File: ShortTextTitle.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the content size for the title.
 *
 * @param g2  the graphics device.
 * @param widthRange  the width range.
 * @param heightRange  the height range.
 *
 * @return The content size.
 */
@Override
protected Size2D arrangeRR(Graphics2D g2, Range widthRange,
        Range heightRange) {

    g2.setFont(getFont());
    FontMetrics fm = g2.getFontMetrics(getFont());
    Rectangle2D bounds = TextUtilities.getTextBounds(getText(), g2, fm);
    if (bounds.getWidth() <= widthRange.getUpperBound()
            && bounds.getHeight() <= heightRange.getUpperBound()) {
        return new Size2D(bounds.getWidth(), bounds.getHeight());
    }
    else {
        return new Size2D(0.0, 0.0);
    }
}
 
Example 4
Source File: ShortTextTitle.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the content size for the title.
 *
 * @param g2  the graphics device.
 * @param widthRange  the width range.
 * @param heightRange  the height range.
 *
 * @return The content size.
 */
@Override
protected Size2D arrangeRR(Graphics2D g2, Range widthRange,
        Range heightRange) {

    g2.setFont(getFont());
    FontMetrics fm = g2.getFontMetrics(getFont());
    Rectangle2D bounds = TextUtilities.getTextBounds(getText(), g2, fm);
    if (bounds.getWidth() <= widthRange.getUpperBound()
            && bounds.getHeight() <= heightRange.getUpperBound()) {
        return new Size2D(bounds.getWidth(), bounds.getHeight());
    }
    else {
        return new Size2D(0.0, 0.0);
    }
}
 
Example 5
Source File: MarkerAxisBand.java    From opensim-gui 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 6
Source File: CyclicNumberAxis.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Reserve some space on each axis side because we draw a centered label at
 * each extremity.
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param plotArea  the plot area.
 * @param edge  the edge.
 * @param space  the space already reserved.
 *
 * @return The reserved space.
 */
@Override
public AxisSpace reserveSpace(Graphics2D g2, Plot plot,
        Rectangle2D plotArea, RectangleEdge edge, AxisSpace space) {

    this.internalMarkerCycleBoundTick = null;
    AxisSpace ret = super.reserveSpace(g2, plot, plotArea, edge, space);
    if (this.internalMarkerCycleBoundTick == null) {
        return ret;
    }

    FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
    Rectangle2D r = TextUtilities.getTextBounds(
        this.internalMarkerCycleBoundTick.getText(), g2, fm
    );

    if (RectangleEdge.isTopOrBottom(edge)) {
        if (isVerticalTickLabels()) {
            space.add(r.getHeight() / 2, RectangleEdge.RIGHT);
        }
        else {
            space.add(r.getWidth() / 2, RectangleEdge.RIGHT);
        }
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        if (isVerticalTickLabels()) {
            space.add(r.getWidth() / 2, RectangleEdge.TOP);
        }
        else {
            space.add(r.getHeight() / 2, RectangleEdge.TOP);
        }
    }

    return ret;

}
 
Example 7
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 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 8
Source File: CyclicNumberAxis.java    From SIMVA-SoS with Apache License 2.0 5 votes vote down vote up
/**
 * Reserve some space on each axis side because we draw a centered label at
 * each extremity.
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param plotArea  the plot area.
 * @param edge  the edge.
 * @param space  the space already reserved.
 *
 * @return The reserved space.
 */
@Override
public AxisSpace reserveSpace(Graphics2D g2, Plot plot,
        Rectangle2D plotArea, RectangleEdge edge, AxisSpace space) {

    this.internalMarkerCycleBoundTick = null;
    AxisSpace ret = super.reserveSpace(g2, plot, plotArea, edge, space);
    if (this.internalMarkerCycleBoundTick == null) {
        return ret;
    }

    FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
    Rectangle2D r = TextUtilities.getTextBounds(
        this.internalMarkerCycleBoundTick.getText(), g2, fm
    );

    if (RectangleEdge.isTopOrBottom(edge)) {
        if (isVerticalTickLabels()) {
            space.add(r.getHeight() / 2, RectangleEdge.RIGHT);
        }
        else {
            space.add(r.getWidth() / 2, RectangleEdge.RIGHT);
        }
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        if (isVerticalTickLabels()) {
            space.add(r.getWidth() / 2, RectangleEdge.TOP);
        }
        else {
            space.add(r.getHeight() / 2, RectangleEdge.TOP);
        }
    }

    return ret;

}
 
Example 9
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 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 10
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 11
Source File: Axis.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a rectangle that encloses the axis label.  This is typically
 * used for layout purposes (it gives the maximum dimensions of the label).
 *
 * @param g2  the graphics device.
 * @param edge  the edge of the plot area along which the axis is measuring.
 *
 * @return The enclosing rectangle.
 */
protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
    Rectangle2D result = new Rectangle2D.Double();
    Rectangle2D bounds = null;
    if (this.attributedLabel != null) {
        TextLayout layout = new TextLayout(
                this.attributedLabel.getIterator(), 
                g2.getFontRenderContext());
        bounds = layout.getBounds();
    } else {
        String axisLabel = getLabel();
        if (axisLabel != null && !axisLabel.equals("")) {
            FontMetrics fm = g2.getFontMetrics(getLabelFont());
            bounds = TextUtilities.getTextBounds(axisLabel, g2, fm);
        }
    }
    if (bounds != null) {
        RectangleInsets insets = getLabelInsets();
        bounds = insets.createOutsetRectangle(bounds);
        double angle = getLabelAngle();
        if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
            angle = angle - Math.PI / 2.0;
        }
        double x = bounds.getCenterX();
        double y = bounds.getCenterY();
        AffineTransform transformer
            = AffineTransform.getRotateInstance(angle, x, y);
        Shape labelBounds = transformer.createTransformedShape(bounds);
        result = labelBounds.getBounds2D();
    }
    return result;
}
 
Example 12
Source File: ShortTextTitle.java    From ECG-Viewer with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Arranges the content for this title assuming a fixed width and no bounds
 * on the height, and returns the required size.  This will reflect the
 * fact that a text title positioned on the left or right of a chart will
 * be rotated by 90 degrees.
 *
 * @param g2  the graphics target.
 * @param w  the width.
 *
 * @return The content size.
 */
@Override
protected Size2D arrangeFN(Graphics2D g2, double w) {
    g2.setFont(getFont());
    FontMetrics fm = g2.getFontMetrics(getFont());
    Rectangle2D bounds = TextUtilities.getTextBounds(getText(), g2, fm);
    if (bounds.getWidth() <= w) {
        return new Size2D(w, bounds.getHeight());
    }
    else {
        return new Size2D(0.0, 0.0);
    }
}
 
Example 13
Source File: CyclicNumberAxis.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Reserve some space on each axis side because we draw a centered label at
 * each extremity. 
 * 
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param plotArea  the plot area.
 * @param edge  the edge.
 * @param space  the space already reserved.
 * 
 * @return The reserved space.
 */
public AxisSpace reserveSpace(Graphics2D g2, 
                              Plot plot, 
                              Rectangle2D plotArea, 
                              RectangleEdge edge, 
                              AxisSpace space) {
    
    this.internalMarkerCycleBoundTick = null;
    AxisSpace ret = super.reserveSpace(g2, plot, plotArea, edge, space);
    if (this.internalMarkerCycleBoundTick == null) {
        return ret;
    }

    FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
    Rectangle2D r = TextUtilities.getTextBounds(
        this.internalMarkerCycleBoundTick.getText(), g2, fm
    );

    if (RectangleEdge.isTopOrBottom(edge)) {
        if (isVerticalTickLabels()) {
            space.add(r.getHeight() / 2, RectangleEdge.RIGHT);
        }
        else {
            space.add(r.getWidth() / 2, RectangleEdge.RIGHT);
        }
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        if (isVerticalTickLabels()) {
            space.add(r.getWidth() / 2, RectangleEdge.TOP);
        }
        else {
            space.add(r.getHeight() / 2, RectangleEdge.TOP);
        }
    }
    
    return ret;
    
}
 
Example 14
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 15
Source File: SymbolAxis.java    From opensim-gui with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates the positions of the tick labels for the axis, storing the 
 * results in the tick label list (ready for drawing).
 *
 * @param g2  the graphics device.
 * @param dataArea  the area in which the data should be drawn.
 * @param edge  the location of the axis.
 * 
 * @return The ticks.
 */
protected List refreshTicksHorizontal(Graphics2D g2,
                                      Rectangle2D dataArea,
                                      RectangleEdge edge) {

    List ticks = new java.util.ArrayList();

    Font tickLabelFont = getTickLabelFont();
    g2.setFont(tickLabelFont);

    double size = getTickUnit().getSize();
    int count = calculateVisibleTickCount();
    double lowestTickValue = calculateLowestVisibleTickValue();

    double previousDrawnTickLabelPos = 0.0;         
    double previousDrawnTickLabelLength = 0.0;              

    if (count <= ValueAxis.MAXIMUM_TICK_COUNT) {
        for (int i = 0; i < count; i++) {
            double currentTickValue = lowestTickValue + (i * size);
            double xx = valueToJava2D(currentTickValue, dataArea, edge);
            String tickLabel;
            NumberFormat formatter = getNumberFormatOverride();
            if (formatter != null) {
                tickLabel = formatter.format(currentTickValue);
            }
            else {
                tickLabel = valueToString(currentTickValue);
            }
            
            // avoid to draw overlapping tick labels
            Rectangle2D bounds = TextUtilities.getTextBounds(
                tickLabel, g2, g2.getFontMetrics()
            );
            double tickLabelLength = isVerticalTickLabels() 
                ? bounds.getHeight() : bounds.getWidth();
            boolean tickLabelsOverlapping = false;
            if (i > 0) {
                double avgTickLabelLength = (previousDrawnTickLabelLength 
                    + tickLabelLength) / 2.0;
                if (Math.abs(xx - previousDrawnTickLabelPos) 
                        < avgTickLabelLength) {
                    tickLabelsOverlapping = true;
                }
            }
            if (tickLabelsOverlapping) {
                tickLabel = ""; // don't draw this tick label
            }
            else {
                // remember these values for next comparison
                previousDrawnTickLabelPos = xx;
                previousDrawnTickLabelLength = tickLabelLength;         
            } 
            
            TextAnchor anchor = null;
            TextAnchor rotationAnchor = null;
            double angle = 0.0;
            if (isVerticalTickLabels()) {
                anchor = TextAnchor.CENTER_RIGHT;
                rotationAnchor = TextAnchor.CENTER_RIGHT;
                if (edge == RectangleEdge.TOP) {
                    angle = Math.PI / 2.0;
                }
                else {
                    angle = -Math.PI / 2.0;
                }
            }
            else {
                if (edge == RectangleEdge.TOP) {
                    anchor = TextAnchor.BOTTOM_CENTER;
                    rotationAnchor = TextAnchor.BOTTOM_CENTER;
                }
                else {
                    anchor = TextAnchor.TOP_CENTER;
                    rotationAnchor = TextAnchor.TOP_CENTER;
                }
            }
            Tick tick = new NumberTick(
                new Double(currentTickValue), tickLabel, anchor, 
                rotationAnchor, angle
            );
            ticks.add(tick);
        }
    }
    return ticks;

}
 
Example 16
Source File: SymbolAxis.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates the positions of the tick labels for the axis, storing the
 * results in the tick label list (ready for drawing).
 *
 * @param g2  the graphics device.
 * @param dataArea  the area in which the data should be drawn.
 * @param edge  the location of the axis.
 *
 * @return The ticks.
 */
@Override
protected List refreshTicksHorizontal(Graphics2D g2, Rectangle2D dataArea,
        RectangleEdge edge) {

    List ticks = new java.util.ArrayList();

    Font tickLabelFont = getTickLabelFont();
    g2.setFont(tickLabelFont);

    double size = getTickUnit().getSize();
    int count = calculateVisibleTickCount();
    double lowestTickValue = calculateLowestVisibleTickValue();

    double previousDrawnTickLabelPos = 0.0;
    double previousDrawnTickLabelLength = 0.0;

    if (count <= ValueAxis.MAXIMUM_TICK_COUNT) {
        for (int i = 0; i < count; i++) {
            double currentTickValue = lowestTickValue + (i * size);
            double xx = valueToJava2D(currentTickValue, dataArea, edge);
            String tickLabel;
            NumberFormat formatter = getNumberFormatOverride();
            if (formatter != null) {
                tickLabel = formatter.format(currentTickValue);
            }
            else {
                tickLabel = valueToString(currentTickValue);
            }

            // avoid to draw overlapping tick labels
            Rectangle2D bounds = TextUtilities.getTextBounds(tickLabel, g2,
                    g2.getFontMetrics());
            double tickLabelLength = isVerticalTickLabels()
                    ? bounds.getHeight() : bounds.getWidth();
            boolean tickLabelsOverlapping = false;
            if (i > 0) {
                double avgTickLabelLength = (previousDrawnTickLabelLength
                        + tickLabelLength) / 2.0;
                if (Math.abs(xx - previousDrawnTickLabelPos)
                        < avgTickLabelLength) {
                    tickLabelsOverlapping = true;
                }
            }
            if (tickLabelsOverlapping) {
                tickLabel = ""; // don't draw this tick label
            }
            else {
                // remember these values for next comparison
                previousDrawnTickLabelPos = xx;
                previousDrawnTickLabelLength = tickLabelLength;
            }

            TextAnchor anchor;
            TextAnchor rotationAnchor;
            double angle = 0.0;
            if (isVerticalTickLabels()) {
                anchor = TextAnchor.CENTER_RIGHT;
                rotationAnchor = TextAnchor.CENTER_RIGHT;
                if (edge == RectangleEdge.TOP) {
                    angle = Math.PI / 2.0;
                }
                else {
                    angle = -Math.PI / 2.0;
                }
            }
            else {
                if (edge == RectangleEdge.TOP) {
                    anchor = TextAnchor.BOTTOM_CENTER;
                    rotationAnchor = TextAnchor.BOTTOM_CENTER;
                }
                else {
                    anchor = TextAnchor.TOP_CENTER;
                    rotationAnchor = TextAnchor.TOP_CENTER;
                }
            }
            Tick tick = new NumberTick(new Double(currentTickValue),
                    tickLabel, anchor, rotationAnchor, angle);
            ticks.add(tick);
        }
    }
    return ticks;

}
 
Example 17
Source File: MeterPlot.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
/**
 * Draws a tick on the dial.
 *
 * @param g2  the graphics device.
 * @param meterArea  the meter area.
 * @param value  the tick value.
 * @param label  a flag that controls whether or not a value label is drawn.
 */
protected void drawTick(Graphics2D g2, Rectangle2D meterArea,
                        double value, boolean label) {

    double valueAngle = valueToAngle(value);

    double meterMiddleX = meterArea.getCenterX();
    double meterMiddleY = meterArea.getCenterY();

    g2.setPaint(this.tickPaint);
    g2.setStroke(new BasicStroke(2.0f));

    double valueP2X;
    double valueP2Y;

    double radius = (meterArea.getWidth() / 2) + DEFAULT_BORDER_SIZE;
    double radius1 = radius - 15;

    double valueP1X = meterMiddleX
            + (radius * Math.cos(Math.PI * (valueAngle / 180)));
    double valueP1Y = meterMiddleY
            - (radius * Math.sin(Math.PI * (valueAngle / 180)));

    valueP2X = meterMiddleX
            + (radius1 * Math.cos(Math.PI * (valueAngle / 180)));
    valueP2Y = meterMiddleY
            - (radius1 * Math.sin(Math.PI * (valueAngle / 180)));

    Line2D.Double line = new Line2D.Double(valueP1X, valueP1Y, valueP2X,
            valueP2Y);
    g2.draw(line);

    if (this.tickLabelsVisible && label) {

        String tickLabel =  this.tickLabelFormat.format(value);
        g2.setFont(this.tickLabelFont);
        g2.setPaint(this.tickLabelPaint);

        FontMetrics fm = g2.getFontMetrics();
        Rectangle2D tickLabelBounds
            = TextUtilities.getTextBounds(tickLabel, g2, fm);

        double x = valueP2X;
        double y = valueP2Y;
        if (valueAngle == 90 || valueAngle == 270) {
            x = x - tickLabelBounds.getWidth() / 2;
        }
        else if (valueAngle < 90 || valueAngle > 270) {
            x = x - tickLabelBounds.getWidth();
        }
        if ((valueAngle > 135 && valueAngle < 225)
                || valueAngle > 315 || valueAngle < 45) {
            y = y - tickLabelBounds.getHeight() / 2;
        }
        else {
            y = y + tickLabelBounds.getHeight() / 2;
        }
        g2.drawString(tickLabel, (float) x, (float) y);
    }
}
 
Example 18
Source File: MeterPlot.java    From ccu-historian with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws a tick on the dial.
 *
 * @param g2  the graphics device.
 * @param meterArea  the meter area.
 * @param value  the tick value.
 * @param label  a flag that controls whether or not a value label is drawn.
 */
protected void drawTick(Graphics2D g2, Rectangle2D meterArea,
                        double value, boolean label) {

    double valueAngle = valueToAngle(value);

    double meterMiddleX = meterArea.getCenterX();
    double meterMiddleY = meterArea.getCenterY();

    g2.setPaint(this.tickPaint);
    g2.setStroke(new BasicStroke(2.0f));

    double valueP2X;
    double valueP2Y;

    double radius = (meterArea.getWidth() / 2) + DEFAULT_BORDER_SIZE;
    double radius1 = radius - 15;

    double valueP1X = meterMiddleX
            + (radius * Math.cos(Math.PI * (valueAngle / 180)));
    double valueP1Y = meterMiddleY
            - (radius * Math.sin(Math.PI * (valueAngle / 180)));

    valueP2X = meterMiddleX
            + (radius1 * Math.cos(Math.PI * (valueAngle / 180)));
    valueP2Y = meterMiddleY
            - (radius1 * Math.sin(Math.PI * (valueAngle / 180)));

    Line2D.Double line = new Line2D.Double(valueP1X, valueP1Y, valueP2X,
            valueP2Y);
    g2.draw(line);

    if (this.tickLabelsVisible && label) {

        String tickLabel =  this.tickLabelFormat.format(value);
        g2.setFont(this.tickLabelFont);
        g2.setPaint(this.tickLabelPaint);

        FontMetrics fm = g2.getFontMetrics();
        Rectangle2D tickLabelBounds
            = TextUtilities.getTextBounds(tickLabel, g2, fm);

        double x = valueP2X;
        double y = valueP2Y;
        if (valueAngle == 90 || valueAngle == 270) {
            x = x - tickLabelBounds.getWidth() / 2;
        }
        else if (valueAngle < 90 || valueAngle > 270) {
            x = x - tickLabelBounds.getWidth();
        }
        if ((valueAngle > 135 && valueAngle < 225)
                || valueAngle > 315 || valueAngle < 45) {
            y = y - tickLabelBounds.getHeight() / 2;
        }
        else {
            y = y + tickLabelBounds.getHeight() / 2;
        }
        g2.drawString(tickLabel, (float) x, (float) y);
    }
}
 
Example 19
Source File: SymbolAxis.java    From opensim-gui with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates the positions of the tick labels for the axis, storing the 
 * results in the tick label list (ready for drawing).
 *
 * @param g2  the graphics device.
 * @param dataArea  the area in which the plot should be drawn.
 * @param edge  the location of the axis.
 * 
 * @return The ticks.
 */
protected List refreshTicksVertical(Graphics2D g2,
                                    Rectangle2D dataArea,
                                    RectangleEdge edge) {

    List ticks = new java.util.ArrayList();

    Font tickLabelFont = getTickLabelFont();
    g2.setFont(tickLabelFont);

    double size = getTickUnit().getSize();
    int count = calculateVisibleTickCount();
    double lowestTickValue = calculateLowestVisibleTickValue();

    double previousDrawnTickLabelPos = 0.0;         
    double previousDrawnTickLabelLength = 0.0;              

    if (count <= ValueAxis.MAXIMUM_TICK_COUNT) {
        for (int i = 0; i < count; i++) {
            double currentTickValue = lowestTickValue + (i * size);
            double yy = valueToJava2D(currentTickValue, dataArea, edge);
            String tickLabel;
            NumberFormat formatter = getNumberFormatOverride();
            if (formatter != null) {
                tickLabel = formatter.format(currentTickValue);
            }
            else {
                tickLabel = valueToString(currentTickValue);
            }

            // avoid to draw overlapping tick labels
            Rectangle2D bounds = TextUtilities.getTextBounds(
                tickLabel, g2, g2.getFontMetrics()
            );
            double tickLabelLength = isVerticalTickLabels() 
                ? bounds.getWidth() : bounds.getHeight();
            boolean tickLabelsOverlapping = false;
            if (i > 0) {
                double avgTickLabelLength = 
                    (previousDrawnTickLabelLength + tickLabelLength) / 2.0;
                if (Math.abs(yy - previousDrawnTickLabelPos) 
                        < avgTickLabelLength) {
                    tickLabelsOverlapping = true;    
                }
                if (tickLabelsOverlapping) {
                    tickLabel = ""; // don't draw this tick label
                }
                else {
                    // remember these values for next comparison
                    previousDrawnTickLabelPos = yy;
                    previousDrawnTickLabelLength = tickLabelLength;         
                } 
            }
            TextAnchor anchor = null;
            TextAnchor rotationAnchor = null;
            double angle = 0.0;
            if (isVerticalTickLabels()) {
                anchor = TextAnchor.BOTTOM_CENTER;
                rotationAnchor = TextAnchor.BOTTOM_CENTER;
                if (edge == RectangleEdge.LEFT) {
                    angle = -Math.PI / 2.0;
                }
                else {
                    angle = Math.PI / 2.0;
                }                    
            }
            else {
                if (edge == RectangleEdge.LEFT) {
                    anchor = TextAnchor.CENTER_RIGHT;
                    rotationAnchor = TextAnchor.CENTER_RIGHT;
                }
                else {
                    anchor = TextAnchor.CENTER_LEFT;
                    rotationAnchor = TextAnchor.CENTER_LEFT;
                }
            }
            Tick tick = new NumberTick(
                new Double(currentTickValue), tickLabel, anchor, 
                rotationAnchor, angle
            );
            ticks.add(tick);
        }
    }
    return ticks;
    
}
 
Example 20
Source File: SymbolAxis.java    From ECG-Viewer with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calculates the positions of the tick labels for the axis, storing the
 * results in the tick label list (ready for drawing).
 *
 * @param g2  the graphics device.
 * @param dataArea  the area in which the plot should be drawn.
 * @param edge  the location of the axis.
 *
 * @return The ticks.
 */
@Override
protected List refreshTicksVertical(Graphics2D g2, Rectangle2D dataArea,
        RectangleEdge edge) {

    List ticks = new java.util.ArrayList();

    Font tickLabelFont = getTickLabelFont();
    g2.setFont(tickLabelFont);

    double size = getTickUnit().getSize();
    int count = calculateVisibleTickCount();
    double lowestTickValue = calculateLowestVisibleTickValue();

    double previousDrawnTickLabelPos = 0.0;
    double previousDrawnTickLabelLength = 0.0;

    if (count <= ValueAxis.MAXIMUM_TICK_COUNT) {
        for (int i = 0; i < count; i++) {
            double currentTickValue = lowestTickValue + (i * size);
            double yy = valueToJava2D(currentTickValue, dataArea, edge);
            String tickLabel;
            NumberFormat formatter = getNumberFormatOverride();
            if (formatter != null) {
                tickLabel = formatter.format(currentTickValue);
            }
            else {
                tickLabel = valueToString(currentTickValue);
            }

            // avoid to draw overlapping tick labels
            Rectangle2D bounds = TextUtilities.getTextBounds(tickLabel, g2,
                    g2.getFontMetrics());
            double tickLabelLength = isVerticalTickLabels()
                ? bounds.getWidth() : bounds.getHeight();
            boolean tickLabelsOverlapping = false;
            if (i > 0) {
                double avgTickLabelLength = (previousDrawnTickLabelLength
                        + tickLabelLength) / 2.0;
                if (Math.abs(yy - previousDrawnTickLabelPos)
                        < avgTickLabelLength) {
                    tickLabelsOverlapping = true;
                }
            }
            if (tickLabelsOverlapping) {
                tickLabel = ""; // don't draw this tick label
            }
            else {
                // remember these values for next comparison
                previousDrawnTickLabelPos = yy;
                previousDrawnTickLabelLength = tickLabelLength;
            }

            TextAnchor anchor;
            TextAnchor rotationAnchor;
            double angle = 0.0;
            if (isVerticalTickLabels()) {
                anchor = TextAnchor.BOTTOM_CENTER;
                rotationAnchor = TextAnchor.BOTTOM_CENTER;
                if (edge == RectangleEdge.LEFT) {
                    angle = -Math.PI / 2.0;
                }
                else {
                    angle = Math.PI / 2.0;
                }
            }
            else {
                if (edge == RectangleEdge.LEFT) {
                    anchor = TextAnchor.CENTER_RIGHT;
                    rotationAnchor = TextAnchor.CENTER_RIGHT;
                }
                else {
                    anchor = TextAnchor.CENTER_LEFT;
                    rotationAnchor = TextAnchor.CENTER_LEFT;
                }
            }
            Tick tick = new NumberTick(new Double(currentTickValue),
                    tickLabel, anchor, rotationAnchor, angle);
            ticks.add(tick);
        }
    }
    return ticks;

}