Example usage for java.awt.geom Area add

List of usage examples for java.awt.geom Area add

Introduction

In this page you can find the example usage for java.awt.geom Area add.

Prototype

public void add(Area rhs) 

Source Link

Document

Adds the shape of the specified Area to the shape of this Area .

Usage

From source file:dk.dma.epd.common.prototype.gui.notification.ChatServicePanel.java

/**
 * {@inheritDoc}//from   w w  w  .  j  a  va 2 s  .c  o m
 */
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHints(GraphicsUtil.ANTIALIAS_HINT);

    // Define the content rectangle
    int x0 = pointerLeft ? pad + pointerWidth : pad;
    RoundRectangle2D.Double content = new RoundRectangle2D.Double(x0, pad, width - 2 * pad - pointerWidth,
            height - 2 * pad, cornerRadius, cornerRadius);

    // Define the pointer triangle
    int xp = pointerLeft ? pad + pointerWidth : width - pad - pointerWidth;
    int yp = pad + height - pointerFromBottom;
    Polygon pointer = new Polygon();
    pointer.addPoint(xp, yp);
    pointer.addPoint(xp, yp - pointerHeight);
    pointer.addPoint(xp + pointerWidth * (pointerLeft ? -1 : 1), yp - pointerHeight / 2);

    // Combine content rectangle and pointer into one area
    Area area = new Area(content);
    area.add(new Area(pointer));

    // Fill the pop-up background
    Color col = pointerLeft ? c.getBackground().darker() : c.getBackground().brighter();
    g2.setColor(col);
    g2.fill(area);

    if (message.getSeverity() == MaritimeTextingNotificationSeverity.WARNING
            || message.getSeverity() == MaritimeTextingNotificationSeverity.ALERT
            || message.getSeverity() == MaritimeTextingNotificationSeverity.SAFETY) {
        g2.setStroke(new BasicStroke(2.0f));

        switch (message.getSeverity()) {
        case WARNING:
            g2.setColor(WARN_COLOR);
        case ALERT:
            g2.setColor(ALERT_COLOR);
        case SAFETY:
            g2.setColor(SAFETY_COLOR);
        default:
            g2.setColor(WARN_COLOR);
        }

        // g2.setColor(message.getSeverity() == MaritimeTextingNotificationSeverity.WARNING ? WARN_COLOR : ALERT_COLOR);

        g2.draw(area);
    }
}

From source file:tilt.image.page.Line.java

/**
 * Get the overall bounding box of this line based on its shapes
 * @return a rectangle//from  w ww. j  av  a2  s . co m
 */
public Rectangle getShapesBounds() {
    Area region = new Area();
    for (int i = 0; i < shapes.size(); i++) {
        Polygon shape = shapes.get(i);
        region.add(new Area(shape));
    }
    return region.getBounds();
}

From source file:VASSAL.counters.Labeler.java

/**
 * Return the Shape of the counter by adding the shape of this label to the shape of all inner traits.
 * Minimize generation of new Area objects.
 *//*  w  w w .  j av a 2s  .  com*/
public Shape getShape() {
    Shape innerShape = piece.getShape();

    // If the label has a Control key, then the image of the label is NOT included in the selectable area of the
    // counter
    if (!labelKey.isNull()) {
        return innerShape;
    } else {
        final Rectangle r = new Rectangle(getLabelPosition(), imagePainter.getImageSize());

        // If the label is completely enclosed in the current counter shape, then we can just return
        // the current shape
        if (innerShape.contains(r.x, r.y, r.width, r.height)) {
            return innerShape;
        } else {
            final Area a = new Area(innerShape);

            // Cache the Area object generated. Only recreate if the label position or size has changed
            if (!r.equals(lastRect)) {
                lastShape = new Area(r);
                lastRect = new Rectangle(r);
            }
            a.add(lastShape);
            return a;
        }
    }
}

From source file:VASSAL.build.module.map.MapShader.java

protected void checkPiece(Area area, GamePiece piece) {
    if (piece instanceof Stack) {
        Stack s = (Stack) piece;
        for (int i = 0; i < s.getPieceCount(); i++) {
            checkPiece(area, s.getPieceAt(i));
        }/*from w w w.ja v  a  2s  . c  o m*/
    } else {
        ShadedPiece shaded = (ShadedPiece) Decorator.getDecorator(piece, ShadedPiece.class);
        if (shaded != null) {
            Area shape = shaded.getArea(this);
            if (shape != null) {
                if (type.equals(FG_TYPE)) {
                    area.add(shape);
                } else {
                    area.subtract(shape);
                }
            }
        }
    }
}

From source file:de.laures.cewolf.jfree.ThermometerPlot.java

/**
 * Draws the plot on a Java 2D graphics device (such as the screen or a printer).
 *
 * @param g2  the graphics device.//from   w  w  w  .ja v a 2 s. c o  m
 * @param area  the area within which the plot should be drawn.
 * @param anchor  the anchor point (<code>null</code> permitted).
 * @param parentState  the state from the parent plot, if there is one.
 * @param info  collects info about the drawing.
 */
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState,
        PlotRenderingInfo info) {

    RoundRectangle2D outerStem = new RoundRectangle2D.Double();
    RoundRectangle2D innerStem = new RoundRectangle2D.Double();
    RoundRectangle2D mercuryStem = new RoundRectangle2D.Double();
    Ellipse2D outerBulb = new Ellipse2D.Double();
    Ellipse2D innerBulb = new Ellipse2D.Double();
    String temp = null;
    FontMetrics metrics = null;
    if (info != null) {
        info.setPlotArea(area);
    }

    // adjust for insets...
    RectangleInsets insets = getInsets();
    insets.trim(area);
    drawBackground(g2, area);

    // adjust for padding...
    Rectangle2D interior = (Rectangle2D) area.clone();
    this.padding.trim(interior);
    int midX = (int) (interior.getX() + (interior.getWidth() / 2));
    int midY = (int) (interior.getY() + (interior.getHeight() / 2));
    int stemTop = (int) (interior.getMinY() + getBulbRadius());
    int stemBottom = (int) (interior.getMaxY() - getBulbDiameter());
    Rectangle2D dataArea = new Rectangle2D.Double(midX - getColumnRadius(), stemTop, getColumnRadius(),
            stemBottom - stemTop);

    outerBulb.setFrame(midX - getBulbRadius(), stemBottom, getBulbDiameter(), getBulbDiameter());

    outerStem.setRoundRect(midX - getColumnRadius(), interior.getMinY(), getColumnDiameter(),
            stemBottom + getBulbDiameter() - stemTop, getColumnDiameter(), getColumnDiameter());

    Area outerThermometer = new Area(outerBulb);
    Area tempArea = new Area(outerStem);
    outerThermometer.add(tempArea);

    innerBulb.setFrame(midX - getBulbRadius() + getGap(), stemBottom + getGap(),
            getBulbDiameter() - getGap() * 2, getBulbDiameter() - getGap() * 2);

    innerStem.setRoundRect(midX - getColumnRadius() + getGap(), interior.getMinY() + getGap(),
            getColumnDiameter() - getGap() * 2, stemBottom + getBulbDiameter() - getGap() * 2 - stemTop,
            getColumnDiameter() - getGap() * 2, getColumnDiameter() - getGap() * 2);

    Area innerThermometer = new Area(innerBulb);
    tempArea = new Area(innerStem);
    innerThermometer.add(tempArea);

    if ((this.dataset != null) && (this.dataset.getValue() != null)) {
        double current = this.dataset.getValue().doubleValue();
        double ds = this.rangeAxis.valueToJava2D(current, dataArea, RectangleEdge.LEFT);

        int i = getColumnDiameter() - getGap() * 2; // already calculated
        int j = getColumnRadius() - getGap(); // already calculated
        int l = (i / 2);
        int k = (int) Math.round(ds);
        if (k < (getGap() + interior.getMinY())) {
            k = (int) (getGap() + interior.getMinY());
            l = getBulbRadius();
        }

        Area mercury = new Area(innerBulb);

        if (k < (stemBottom + getBulbRadius())) {
            mercuryStem.setRoundRect(midX - j, k, i, (stemBottom + getBulbRadius()) - k, l, l);
            tempArea = new Area(mercuryStem);
            mercury.add(tempArea);
        }

        g2.setPaint(getCurrentPaint());
        g2.fill(mercury);

        // draw range indicators...
        if (this.subrangeIndicatorsVisible) {
            g2.setStroke(this.subrangeIndicatorStroke);
            Range range = this.rangeAxis.getRange();

            // draw start of normal range
            double value = this.subrangeInfo[NORMAL][RANGE_LOW];
            if (range.contains(value)) {
                double x = midX + getColumnRadius() + 2;
                double y = this.rangeAxis.valueToJava2D(value, dataArea, RectangleEdge.LEFT);
                Line2D line = new Line2D.Double(x, y, x + 10, y);
                g2.setPaint(this.subrangePaint[NORMAL]);
                g2.draw(line);
            }

            // draw start of warning range
            value = this.subrangeInfo[WARNING][RANGE_LOW];
            if (range.contains(value)) {
                double x = midX + getColumnRadius() + 2;
                double y = this.rangeAxis.valueToJava2D(value, dataArea, RectangleEdge.LEFT);
                Line2D line = new Line2D.Double(x, y, x + 10, y);
                g2.setPaint(this.subrangePaint[WARNING]);
                g2.draw(line);
            }

            // draw start of critical range
            value = this.subrangeInfo[CRITICAL][RANGE_LOW];
            if (range.contains(value)) {
                double x = midX + getColumnRadius() + 2;
                double y = this.rangeAxis.valueToJava2D(value, dataArea, RectangleEdge.LEFT);
                Line2D line = new Line2D.Double(x, y, x + 10, y);
                g2.setPaint(this.subrangePaint[CRITICAL]);
                g2.draw(line);
            }
        }

        // draw the axis...
        if ((this.rangeAxis != null) && (this.axisLocation != NONE)) {
            int drawWidth = AXIS_GAP;
            Rectangle2D drawArea;
            double cursor = 0;

            switch (this.axisLocation) {
            case RIGHT:
                cursor = midX + getColumnRadius();
                drawArea = new Rectangle2D.Double(cursor, stemTop, drawWidth, (stemBottom - stemTop + 1));
                this.rangeAxis.draw(g2, cursor, area, drawArea, RectangleEdge.RIGHT, null);
                break;

            case LEFT:
            default:
                //cursor = midX - COLUMN_RADIUS - AXIS_GAP;
                cursor = midX - getColumnRadius();
                drawArea = new Rectangle2D.Double(cursor, stemTop, drawWidth, (stemBottom - stemTop + 1));
                this.rangeAxis.draw(g2, cursor, area, drawArea, RectangleEdge.LEFT, null);
                break;
            }

        }

        // draw text value on screen
        g2.setFont(this.valueFont);
        g2.setPaint(this.valuePaint);
        metrics = g2.getFontMetrics();
        switch (this.valueLocation) {
        case RIGHT:
            g2.drawString(this.valueFormat.format(current), midX + getColumnRadius() + getGap(), midY);
            break;
        case LEFT:
            String valueString = this.valueFormat.format(current);
            int stringWidth = metrics.stringWidth(valueString);
            g2.drawString(valueString, midX - getColumnRadius() - getGap() - stringWidth, midY);
            break;
        case BULB:
            temp = this.valueFormat.format(current);
            i = metrics.stringWidth(temp) / 2;
            g2.drawString(temp, midX - i, stemBottom + getBulbRadius() + getGap());
            break;
        default:
        }
        /***/
    }

    g2.setPaint(this.thermometerPaint);
    g2.setFont(this.valueFont);

    //  draw units indicator
    metrics = g2.getFontMetrics();
    int tickX1 = midX - getColumnRadius() - getGap() * 2 - metrics.stringWidth(UNITS[this.units]);
    if (tickX1 > area.getMinX()) {
        g2.drawString(UNITS[this.units], tickX1, (int) (area.getMinY() + 20));
    }

    // draw thermometer outline
    g2.setStroke(this.thermometerStroke);
    g2.draw(outerThermometer);
    g2.draw(innerThermometer);

    drawOutline(g2, area);
}

From source file:Region.java

/**
 * Convert this region to an AWT Area.//from   w  ww  . j  a  v  a 2s .  c  om
 * <p>
 * Note: The AWT classes are referenced explicitly here rather tham
 * importing them to indicate that the Region implementation does not
 * dependent on the AWT.
 * 
 * @return  Area equivalent of this rectangle.
 */
public java.awt.geom.Area createArea() {
    if (nRectangles_ == 0)
        return null;

    java.awt.Rectangle rectangle = new java.awt.Rectangle(rectangles_[2], rectangles_[0],
            rectangles_[3] - rectangles_[2], rectangles_[1] - rectangles_[0]);
    java.awt.geom.Area area = new java.awt.geom.Area(rectangle);

    for (int i = 1; i < nRectangles_; i++) {
        int j = i * 4;
        rectangle = new java.awt.Rectangle(rectangles_[j + 2], rectangles_[j + 0],
                rectangles_[j + 3] - rectangles_[j + 2], rectangles_[j + 1] - rectangles_[j + 0]);

        area.add(new java.awt.geom.Area(rectangle));
    }

    return area;
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.PlotInstanceLegendCreator.java

/**
 * Creates a continuous legend item for one item in dimensionSet, i.e. dimensionSet must be a
 * set containing exactly one value./*  ww  w.  j av a2s .c  o  m*/
 * 
 * @param dateFormat
 *            format used to format minValue and maxValue as dates, or null if they should be
 *            displayed numerically instead of as dates
 * @throws ChartPlottimeException
 */
private LegendItem createContinuousLegendItem(PlotInstance plotInstance, Set<PlotDimension> dimensionSet,
        double minValue, double maxValue, DateFormat dateFormat) {
    PlotConfiguration plotConfiguration = plotInstance.getCurrentPlotConfigurationClone();
    PlotDimension dimension = dimensionSet.iterator().next();
    DefaultDimensionConfig dimensionConfig = (DefaultDimensionConfig) plotConfiguration
            .getDimensionConfig(dimension);
    DimensionConfigData dimensionConfigData = plotInstance.getPlotData()
            .getDimensionConfigData(dimensionConfig);
    // String label = dimensionConfig.getLabel();
    // if(label == null) {
    // label = I18N.getGUILabel("plotter.unnamed_value_label");
    // }
    String label = "";

    if (dimension == PlotDimension.COLOR) {
        ColorProvider colorProvider = dimensionConfigData.getColorProvider();
        if (!colorProvider.supportsNumericalValues()) {
            throw new RuntimeException(
                    "Color provider for continuous legend item does not support numerical values.");
        }

        // shape dimensions
        final int width = 50;
        final int height = 10;

        // create item paint

        // first disable logarithmic scale on color provider ( -> linear gradient in legend)
        // ContinuousColorProvider continuousColorProvider = null;
        // if (dimensionConfig.isLogarithmic() && colorProvider instanceof
        // ContinuousColorProvider) {
        // continuousColorProvider = (ContinuousColorProvider)colorProvider;
        // continuousColorProvider.setLogarithmic(false);
        // }

        // calculate gradient
        float fractions[] = new float[width];
        Color colors[] = new Color[width];
        for (int i = 0; i < width; ++i) {

            float fraction = i / (width - 1.0f);
            double fractionValue;
            if (colorProvider instanceof ContinuousColorProvider
                    && ((ContinuousColorProvider) colorProvider).isColorMinMaxValueDifferentFromOriginal(
                            ((ContinuousColorProvider) colorProvider).getMinValue(),
                            ((ContinuousColorProvider) colorProvider).getMaxValue())) {
                fractionValue = ((ContinuousColorProvider) colorProvider).getMinValue()
                        + fraction * (((ContinuousColorProvider) colorProvider).getMaxValue()
                                - ((ContinuousColorProvider) colorProvider).getMinValue());
            } else {
                fractionValue = minValue + fraction * (maxValue - minValue);
            }
            colors[i] = colorProvider.getColorForValue(fractionValue);
            fractions[i] = fraction;
        }
        LinearGradientPaint shapeFillPaint = new LinearGradientPaint(new Point(0, 0), new Point(width, 0),
                fractions, colors, CycleMethod.REPEAT);

        // reset color provider to logarithmic if necessary
        // if (continuousColorProvider != null && dimensionConfig.isLogarithmic()) {
        // continuousColorProvider.setLogarithmic(true);
        // }

        // create item shape
        Rectangle itemShape = new Rectangle(width, height);

        if (colorProvider instanceof ContinuousColorProvider) {
            return createFlankedShapeLegendItem(label, ((ContinuousColorProvider) colorProvider).getMinValue(),
                    ((ContinuousColorProvider) colorProvider).getMaxValue(), itemShape, shapeFillPaint, true,
                    dateFormat);
        } else {
            return createFlankedShapeLegendItem(label, minValue, maxValue, itemShape, shapeFillPaint, true,
                    dateFormat);
        }
    } else if (dimension == PlotDimension.SHAPE) {
        // shape provider probably never supports numerical values
        return null;
    } else if (dimension == PlotDimension.SIZE) {
        SizeProvider sizeProvider = dimensionConfigData.getSizeProvider();

        if (!sizeProvider.supportsNumericalValues()) {
            throw new RuntimeException(
                    "Size provider for continuous legend item does not support numerical values.");
        }

        double minScalingFactor = sizeProvider.getMinScalingFactor();
        double maxScalingFactor = sizeProvider.getMaxScalingFactor();
        ContinuousSizeProvider legendSizeProvider = new ContinuousSizeProvider(minScalingFactor,
                maxScalingFactor, MIN_LEGEND_ITEM_SCALING_FACTOR, MAX_LEGEND_ITEM_SCALING_FACTOR, false);

        int legendItemCount = 4;
        Area composedShape = new Area();
        Shape originalShape = UNDEFINED_SHAPE;
        if (dimensionSet.contains(PlotDimension.SIZE) && dimensionSet.size() == 1) {
            originalShape = UNDEFINED_SHAPE_AND_COLOR;
        }
        double maxHeight = originalShape.getBounds().getHeight() * MAX_LEGEND_ITEM_SCALING_FACTOR;
        for (int i = 0; i < legendItemCount; ++i) {
            double fraction = minScalingFactor
                    + ((double) i / legendItemCount * (maxScalingFactor - minScalingFactor));
            double legendScalingFactor = legendSizeProvider.getScalingFactorForValue(fraction);

            double composedWidth = composedShape.getBounds().getWidth();

            AffineTransform t = new AffineTransform();
            t.scale(legendScalingFactor, legendScalingFactor);
            Shape shape = t.createTransformedShape(originalShape);

            t = new AffineTransform();
            double shapeWidth = shape.getBounds().getWidth();
            double shapeHeight = shape.getBounds().getHeight();
            t.translate(composedWidth + shapeWidth * .1, (maxHeight - shapeHeight) / 2.0);
            t.translate(-shape.getBounds().getMinX(), -shape.getBounds().getMinY());
            shape = t.createTransformedShape(shape);
            composedShape.add(new Area(shape));
        }

        return createFlankedShapeLegendItem(label, minValue, maxValue, composedShape, UNDEFINED_COLOR_PAINT,
                false, dateFormat);

    } else {
        throw new RuntimeException("Unsupported dimension. Execution path should never reach this line.");
    }
}