org.jfree.text.TextUtilities Java Examples

The following examples show how to use org.jfree.text.TextUtilities. 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: DialTextAnnotation.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * Draws the background to the specified graphics device.  If the dial
 * frame specifies a window, the clipping region will already have been
 * set to this window before this method is called.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param plot  the plot (ignored here).
 * @param frame  the dial frame (ignored here).
 * @param view  the view rectangle (<code>null</code> not permitted).
 */
@Override
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame,
        Rectangle2D view) {

    // work out the anchor point
    Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius,
            this.radius);
    Arc2D arc = new Arc2D.Double(f, this.angle, 0.0, Arc2D.OPEN);
    Point2D pt = arc.getStartPoint();
    g2.setPaint(this.paint);
    g2.setFont(this.font);
    TextUtilities.drawAlignedString(this.label, g2, (float) pt.getX(),
            (float) pt.getY(), this.anchor);

}
 
Example #2
Source File: YIntervalRenderer.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Draws an item label.
 *
 * @param g2  the graphics device.
 * @param orientation  the orientation.
 * @param dataset  the dataset.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param x  the x coordinate (in Java2D space).
 * @param y  the y coordinate (in Java2D space).
 */
private void drawAdditionalItemLabel(Graphics2D g2,
        PlotOrientation orientation, XYDataset dataset, int series,
        int item, double x, double y) {

    if (this.additionalItemLabelGenerator == null) {
        return;
    }

    Font labelFont = getItemLabelFont(series, item);
    Paint paint = getItemLabelPaint(series, item);
    g2.setFont(labelFont);
    g2.setPaint(paint);
    String label = this.additionalItemLabelGenerator.generateLabel(dataset,
            series, item);

    ItemLabelPosition position = getNegativeItemLabelPosition(series, item);
    Point2D anchorPoint = calculateLabelAnchorPoint(
            position.getItemLabelAnchor(), x, y, orientation);
    TextUtilities.drawRotatedString(label, g2,
            (float) anchorPoint.getX(), (float) anchorPoint.getY(),
            position.getTextAnchor(), position.getAngle(),
            position.getRotationAnchor());
}
 
Example #3
Source File: MarkerAxisBand.java    From openstock 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 #4
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 #5
Source File: Plot.java    From openstock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Draws a message to state that there is no data to plot.
 *
 * @param g2  the graphics device.
 * @param area  the area within which the plot should be drawn.
 */
protected void drawNoDataMessage(Graphics2D g2, Rectangle2D area) {
    Shape savedClip = g2.getClip();
    g2.clip(area);
    String message = this.noDataMessage;
    if (message != null) {
        g2.setFont(this.noDataMessageFont);
        g2.setPaint(this.noDataMessagePaint);
        TextBlock block = TextUtilities.createTextBlock(
                this.noDataMessage, this.noDataMessageFont,
                this.noDataMessagePaint, 0.9f * (float) area.getWidth(),
                new G2TextMeasurer(g2));
        block.draw(g2, (float) area.getCenterX(),
                (float) area.getCenterY(), TextBlockAnchor.CENTER);
    }
    g2.setClip(savedClip);
}
 
Example #6
Source File: DialTextAnnotation.java    From openstock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Draws the background to the specified graphics device.  If the dial
 * frame specifies a window, the clipping region will already have been
 * set to this window before this method is called.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param plot  the plot (ignored here).
 * @param frame  the dial frame (ignored here).
 * @param view  the view rectangle (<code>null</code> not permitted).
 */
@Override
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame,
        Rectangle2D view) {

    // work out the anchor point
    Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius,
            this.radius);
    Arc2D arc = new Arc2D.Double(f, this.angle, 0.0, Arc2D.OPEN);
    Point2D pt = arc.getStartPoint();
    g2.setPaint(this.paint);
    g2.setFont(this.font);
    TextUtilities.drawAlignedString(this.label, g2, (float) pt.getX(),
            (float) pt.getY(), this.anchor);

}
 
Example #7
Source File: MeterPlot.java    From openstock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Draws the value label just below the center of the dial.
 *
 * @param g2  the graphics device.
 * @param area  the plot area.
 */
protected void drawValueLabel(Graphics2D g2, Rectangle2D area) {
    g2.setFont(this.valueFont);
    g2.setPaint(this.valuePaint);
    String valueStr = "No value";
    if (this.dataset != null) {
        Number n = this.dataset.getValue();
        if (n != null) {
            valueStr = this.tickLabelFormat.format(n.doubleValue()) + " "
                     + this.units;
        }
    }
    float x = (float) area.getCenterX();
    float y = (float) area.getCenterY() + DEFAULT_CIRCLE_SIZE;
    TextUtilities.drawAlignedString(valueStr, g2, x, y,
            TextAnchor.TOP_CENTER);
}
 
Example #8
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 #9
Source File: ShortTextTitle.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * Draws the title using the current font and paint.
 *
 * @param g2  the graphics target.
 * @param area  the title area.
 * @param params  optional parameters (ignored here).
 *
 * @return <code>null</code>.
 */
@Override
public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
    if (area.isEmpty()) {
        return null;
    }
    area = trimMargin(area);
    drawBorder(g2, area);
    area = trimBorder(area);
    area = trimPadding(area);
    g2.setFont(getFont());
    g2.setPaint(getPaint());
    TextUtilities.drawAlignedString(getText(), g2, (float) area.getMinX(),
            (float) area.getMinY(), TextAnchor.TOP_LEFT);
    return null;
}
 
Example #10
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 #11
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 #12
Source File: ShortTextTitle.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Draws the title using the current font and paint.
 *
 * @param g2  the graphics target.
 * @param area  the title area.
 * @param params  optional parameters (ignored here).
 *
 * @return <code>null</code>.
 */
@Override
public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
    if (area.isEmpty()) {
        return null;
    }
    area = trimMargin(area);
    drawBorder(g2, area);
    area = trimBorder(area);
    area = trimPadding(area);
    g2.setFont(getFont());
    g2.setPaint(getPaint());
    TextUtilities.drawAlignedString(getText(), g2, (float) area.getMinX(),
            (float) area.getMinY(), TextAnchor.TOP_LEFT);
    return null;
}
 
Example #13
Source File: SubCategoryAxis.java    From ECG-Viewer with GNU General Public License v2.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 #14
Source File: YIntervalRenderer.java    From openstock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Draws an item label.
 *
 * @param g2  the graphics device.
 * @param orientation  the orientation.
 * @param dataset  the dataset.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param x  the x coordinate (in Java2D space).
 * @param y  the y coordinate (in Java2D space).
 */
private void drawAdditionalItemLabel(Graphics2D g2,
        PlotOrientation orientation, XYDataset dataset, int series,
        int item, double x, double y) {

    if (this.additionalItemLabelGenerator == null) {
        return;
    }

    Font labelFont = getItemLabelFont(series, item);
    Paint paint = getItemLabelPaint(series, item);
    g2.setFont(labelFont);
    g2.setPaint(paint);
    String label = this.additionalItemLabelGenerator.generateLabel(dataset,
            series, item);

    ItemLabelPosition position = getNegativeItemLabelPosition(series, item);
    Point2D anchorPoint = calculateLabelAnchorPoint(
            position.getItemLabelAnchor(), x, y, orientation);
    TextUtilities.drawRotatedString(label, g2,
            (float) anchorPoint.getX(), (float) anchorPoint.getY(),
            position.getTextAnchor(), position.getAngle(),
            position.getRotationAnchor());
}
 
Example #15
Source File: SubCategoryAxis.java    From openstock 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 #16
Source File: MarkerAxisBand.java    From ECG-Viewer with GNU General Public License v2.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 #17
Source File: ShortTextTitle.java    From ccu-historian with GNU General Public License v3.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 #18
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 #19
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 #20
Source File: AbstractCategoryItemRenderer.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws an item label.
 *
 * @param g2  the graphics device.
 * @param orientation  the orientation.
 * @param dataset  the dataset.
 * @param row  the row.
 * @param column  the column.
 * @param x  the x coordinate (in Java2D space).
 * @param y  the y coordinate (in Java2D space).
 * @param negative  indicates a negative value (which affects the item
 *                  label position).
 */
protected void drawItemLabel(Graphics2D g2, PlotOrientation orientation,
        CategoryDataset dataset, int row, int column,
        double x, double y, boolean negative) {

    CategoryItemLabelGenerator generator = getItemLabelGenerator(row,
            column);
    if (generator != null) {
        Font labelFont = getItemLabelFont(row, column);
        Paint paint = getItemLabelPaint(row, column);
        g2.setFont(labelFont);
        g2.setPaint(paint);
        String label = generator.generateLabel(dataset, row, column);
        ItemLabelPosition position;
        if (!negative) {
            position = getPositiveItemLabelPosition(row, column);
        }
        else {
            position = getNegativeItemLabelPosition(row, column);
        }
        Point2D anchorPoint = calculateLabelAnchorPoint(
                position.getItemLabelAnchor(), x, y, orientation);
        TextUtilities.drawRotatedString(label, g2,
                (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(),
                position.getAngle(), position.getRotationAnchor());
    }

}
 
Example #21
Source File: LabelBlock.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new label block.
 *
 * @param text  the text for the label (<code>null</code> not permitted).
 * @param font  the font (<code>null</code> not permitted).
 * @param paint the paint (<code>null</code> not permitted).
 */
public LabelBlock(String text, Font font, Paint paint) {
    this.text = text;
    this.paint = paint;
    this.label = TextUtilities.createTextBlock(text, font, this.paint);
    this.font = font;
    this.toolTipText = null;
    this.urlText = null;
    this.contentAlignmentPoint = TextBlockAnchor.CENTER;
    this.textAnchor = RectangleAnchor.CENTER;
}
 
Example #22
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 #23
Source File: CategoryTextAnnotation.java    From SIMVA-SoS with Apache License 2.0 5 votes vote down vote up
/**
 * Draws the annotation.
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param dataArea  the data area.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 */
@Override
public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea,
        CategoryAxis domainAxis, ValueAxis rangeAxis) {

    CategoryDataset dataset = plot.getDataset();
    int catIndex = dataset.getColumnIndex(this.category);
    int catCount = dataset.getColumnCount();

    float anchorX = 0.0f;
    float anchorY = 0.0f;
    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
            plot.getDomainAxisLocation(), orientation);
    RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
            plot.getRangeAxisLocation(), orientation);

    if (orientation == PlotOrientation.HORIZONTAL) {
        anchorY = (float) domainAxis.getCategoryJava2DCoordinate(
                this.categoryAnchor, catIndex, catCount, dataArea,
                domainEdge);
        anchorX = (float) rangeAxis.valueToJava2D(this.value, dataArea,
                rangeEdge);
    }
    else if (orientation == PlotOrientation.VERTICAL) {
        anchorX = (float) domainAxis.getCategoryJava2DCoordinate(
                this.categoryAnchor, catIndex, catCount, dataArea,
                domainEdge);
        anchorY = (float) rangeAxis.valueToJava2D(this.value, dataArea,
                rangeEdge);
    }
    g2.setFont(getFont());
    g2.setPaint(getPaint());
    TextUtilities.drawRotatedString(getText(), g2, anchorX, anchorY,
            getTextAnchor(), getRotationAngle(), getRotationAnchor());

}
 
Example #24
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 #25
Source File: Axis.java    From ccu-historian 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 #26
Source File: AbstractXYItemRenderer.java    From ECG-Viewer with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws an item label.
 *
 * @param g2  the graphics device.
 * @param orientation  the orientation.
 * @param dataset  the dataset.
 * @param series  the series index (zero-based).
 * @param item  the item index (zero-based).
 * @param x  the x coordinate (in Java2D space).
 * @param y  the y coordinate (in Java2D space).
 * @param negative  indicates a negative value (which affects the item
 *                  label position).
 */
protected void drawItemLabel(Graphics2D g2, PlotOrientation orientation,
        XYDataset dataset, int series, int item, double x, double y,
        boolean negative) {

    XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
    if (generator != null) {
        Font labelFont = getItemLabelFont(series, item);
        Paint paint = getItemLabelPaint(series, item);
        g2.setFont(labelFont);
        g2.setPaint(paint);
        String label = generator.generateLabel(dataset, series, item);

        // get the label position..
        ItemLabelPosition position;
        if (!negative) {
            position = getPositiveItemLabelPosition(series, item);
        }
        else {
            position = getNegativeItemLabelPosition(series, item);
        }

        // work out the label anchor point...
        Point2D anchorPoint = calculateLabelAnchorPoint(
                position.getItemLabelAnchor(), x, y, orientation);
        TextUtilities.drawRotatedString(label, g2,
                (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(),
                position.getRotationAnchor());
    }

}
 
Example #27
Source File: ValueAxis.java    From ccu-historian 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 #28
Source File: ShortTextTitle.java    From ccu-historian with GNU General Public License v3.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 #29
Source File: CategoryTextAnnotation.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the annotation.
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param dataArea  the data area.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 */
@Override
public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea,
        CategoryAxis domainAxis, ValueAxis rangeAxis) {

    CategoryDataset dataset = plot.getDataset();
    int catIndex = dataset.getColumnIndex(this.category);
    int catCount = dataset.getColumnCount();

    float anchorX = 0.0f;
    float anchorY = 0.0f;
    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
            plot.getDomainAxisLocation(), orientation);
    RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
            plot.getRangeAxisLocation(), orientation);

    if (orientation == PlotOrientation.HORIZONTAL) {
        anchorY = (float) domainAxis.getCategoryJava2DCoordinate(
                this.categoryAnchor, catIndex, catCount, dataArea,
                domainEdge);
        anchorX = (float) rangeAxis.valueToJava2D(this.value, dataArea,
                rangeEdge);
    }
    else if (orientation == PlotOrientation.VERTICAL) {
        anchorX = (float) domainAxis.getCategoryJava2DCoordinate(
                this.categoryAnchor, catIndex, catCount, dataArea,
                domainEdge);
        anchorY = (float) rangeAxis.valueToJava2D(this.value, dataArea,
                rangeEdge);
    }
    g2.setFont(getFont());
    g2.setPaint(getPaint());
    TextUtilities.drawRotatedString(getText(), g2, anchorX, anchorY,
            getTextAnchor(), getRotationAngle(), getRotationAnchor());

}
 
Example #30
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;
    
}