Example usage for java.awt.geom AffineTransform createTransformedShape

List of usage examples for java.awt.geom AffineTransform createTransformedShape

Introduction

In this page you can find the example usage for java.awt.geom AffineTransform createTransformedShape.

Prototype

public Shape createTransformedShape(Shape pSrc) 

Source Link

Document

Returns a new Shape object defined by the geometry of the specified Shape after it has been transformed by this transform.

Usage

From source file:net.sf.maltcms.common.charts.api.overlay.AbstractChartOverlay.java

/**
 *
 * @param entity// w ww.  jav  a 2s .  co  m
 * @param chartPanel
 * @param center
 * @return
 */
public static Shape toViewXY(Shape entity, ChartPanel chartPanel, Point2D center) {
    AffineTransform toPosition = getModelToViewTransformXY(chartPanel, center.getX(), center.getY());
    toPosition.concatenate(getTranslateInstance(-center.getX(), -center.getY()));
    return toPosition.createTransformedShape(entity);
}

From source file:ShapeTransform.java

/**
 * Translates a given shape. Special care is taken to preserve the shape's
 * original class, if the shape is a rectangle or a line.
 * //from   ww w. j  a va2s  .c  om
 * @param s
 *          the shape
 * @param x
 *          the x coordinate where the shape is translated to
 * @param y
 *          the y coordinate where the shape is translated to
 * @return the translated shape
 */
public static Shape translateShape(final Shape s, final double x, final double y) {
    if (s instanceof RectangularShape) {
        final RectangularShape rect = (RectangularShape) s;
        final RectangularShape retval = (RectangularShape) rect.clone();
        retval.setFrame(retval.getX() + x, retval.getY() + y, retval.getWidth(), retval.getHeight());
        return retval;
    }
    if (s instanceof Line2D) {
        final Line2D line = (Line2D) s;
        final Line2D retval = (Line2D) line.clone();
        retval.setLine(retval.getX1() + x, retval.getY1() + y, retval.getX2() + x, retval.getY2() + y);
        return retval;
    }

    final AffineTransform af = AffineTransform.getTranslateInstance(x, y);
    return af.createTransformedShape(s);
}

From source file:net.sf.maltcms.common.charts.api.overlay.AbstractChartOverlay.java

/**
 *
 * @param entity/*from  w ww.ja v a2s .com*/
 * @param chartPanel
 * @param dataset
 * @param seriesIndex
 * @param itemIndex
 * @return
 */
public static Shape toView(Shape entity, ChartPanel chartPanel, Dataset dataset, int seriesIndex,
        int itemIndex) {
    if (dataset instanceof XYDataset) {
        XYDataset xyds = (XYDataset) dataset;
        double x1 = xyds.getXValue(seriesIndex, itemIndex);
        double y1 = xyds.getYValue(seriesIndex, itemIndex);
        AffineTransform toPosition = getModelToViewTransformXY(chartPanel, x1, y1);
        toPosition.concatenate(
                getTranslateInstance(-entity.getBounds2D().getCenterX(), -entity.getBounds2D().getCenterY()));
        return toPosition.createTransformedShape(entity);
    } else if (dataset instanceof CategoryDataset) {
        CategoryDataset cds = (CategoryDataset) dataset;
        double y1 = cds.getValue(seriesIndex, itemIndex).doubleValue();
        AffineTransform toPosition = getModelToViewTransformCategory(chartPanel, itemIndex, y1);
        toPosition.concatenate(
                getTranslateInstance(-entity.getBounds2D().getCenterX(), -entity.getBounds2D().getCenterY()));
        return toPosition.createTransformedShape(entity);
    }
    throw new IllegalArgumentException("Unsupported dataset type: " + dataset.getClass());
}

From source file:ShapeTransform.java

/**
 * Scales a given shape. The shape is first normalized, then scaled and
 * finally brought back into its original position.
 * //from   w  ww .  j a  va2 s .  c o  m
 * @param shape
 *          the shape to be scaled
 * @param scaleX
 *          the horizontal scaling factor
 * @param scaleY
 *          the vertical scaling factor
 * @return the scaled shape
 */
private static Shape performDefaultTransformation(final Shape shape, final double scaleX, final double scaleY) {
    /**
     * Apply the normalisation shape transform ... bring the shape to pos (0,0)
     */
    final Rectangle2D bounds = shape.getBounds2D();
    AffineTransform af = AffineTransform.getTranslateInstance(0 - bounds.getX(), 0 - bounds.getY());
    // apply normalisation translation ...
    Shape s = af.createTransformedShape(shape);

    af = AffineTransform.getScaleInstance(scaleX, scaleY);
    // apply scaling ...
    s = af.createTransformedShape(s);

    // now retranslate the shape to its original position ...
    af = AffineTransform.getTranslateInstance(bounds.getX(), bounds.getY());
    return af.createTransformedShape(s);
}

From source file:de.bund.bfr.jung.JungUtils.java

static <V, E> Shape getTransformedEdgeShape(RenderContext<V, E> rc, Layout<V, E> layout, E e) {
    Graph<V, E> graph = layout.getGraph();
    edu.uci.ics.jung.graph.util.Pair<V> endpoints = graph.getEndpoints(e);
    V v1 = endpoints.getFirst();/*w w  w .ja  va  2 s.co  m*/
    V v2 = endpoints.getSecond();

    if (!rc.getEdgeIncludePredicate().evaluate(Context.<Graph<V, E>, E>getInstance(graph, e))
            || !rc.getVertexIncludePredicate().evaluate(Context.<Graph<V, E>, V>getInstance(graph, v1))
            || !rc.getVertexIncludePredicate().evaluate(Context.<Graph<V, E>, V>getInstance(graph, v2))) {
        return null;
    }

    Point2D p1 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, layout.transform(v1));
    Point2D p2 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, layout.transform(v2));
    float x1 = (float) p1.getX();
    float y1 = (float) p1.getY();
    float x2 = (float) p2.getX();
    float y2 = (float) p2.getY();
    Shape edgeShape = rc.getEdgeShapeTransformer().transform(Context.getInstance(graph, e));
    AffineTransform edgeShapeTransform = AffineTransform.getTranslateInstance(x1, y1);

    if (v1.equals(v2)) {
        Rectangle2D bounds = rc.getVertexShapeTransformer().transform(v1).getBounds2D();

        edgeShapeTransform.scale(bounds.getWidth(), bounds.getHeight());
        edgeShapeTransform.translate(0, -edgeShape.getBounds2D().getWidth() / 2);
    } else {
        float dx = x2 - x1;
        float dy = y2 - y1;

        edgeShapeTransform.rotate(Math.atan2(dy, dx));
        edgeShapeTransform.scale(Math.sqrt(dx * dx + dy * dy), 1.0);
    }

    return edgeShapeTransform.createTransformedShape(edgeShape);
}

From source file:ec.util.chart.swing.JTimeSeriesRendererSupport.java

private static Shape createShape(double x, double y, Rectangle2D hotspot) {
    Area result = new Area(new RoundRectangle2D.Double(hotspot.getX(), hotspot.getY(), hotspot.getWidth(),
            hotspot.getHeight(), 8, 8));

    boolean right = hotspot.getMinX() > x;

    Polygon po = new Polygon();
    po.addPoint(0, 0);/*from   www.j a  v  a  2s. co  m*/
    po.addPoint(0, 10);
    po.addPoint(10, 0);
    AffineTransform af = new AffineTransform();
    if (right) {
        af.translate(hotspot.getX() - 7, hotspot.getY() + hotspot.getHeight() / 2);
        af.rotate(-Math.PI / 4);
    } else {
        af.translate(hotspot.getMaxX() + 7, hotspot.getY() + hotspot.getHeight() / 2);
        af.rotate(Math.PI * 3 / 4);
    }

    Shape shape = af.createTransformedShape(po);
    result.add(new Area(shape));
    return result;
}

From source file:RotateTransformed.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);

    for (double i = 0; i < 360; i += 5) {
        AffineTransform at = AffineTransform.getTranslateInstance(400 / 2, 400 / 2);
        at.rotate(Math.toRadians(i));
        g2.draw(at.createTransformedShape(e));
    }//from ww  w.  ja va2s.  c  om
}

From source file:Highlights.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

    String s = "Drag the text to highlight Java Source and Support.";
    Font font = new Font("Serif", Font.PLAIN, 32);

    if (textLayout == null) {
        FontRenderContext frc = g2.getFontRenderContext();
        textLayout = new TextLayout(s, font, frc);
    }//from  w  ww. j a  va2s .  com

    // Draw the highlight.
    if (firstHit != null && secondHit != null) {
        Shape base = textLayout.getLogicalHighlightShape(firstHit.getInsertionIndex(),
                secondHit.getInsertionIndex());
        AffineTransform at = AffineTransform.getTranslateInstance(x, y);
        Shape highlight = at.createTransformedShape(base);
        g2.setPaint(Color.white);
        g2.fill(highlight);
    }

    g2.setPaint(Color.black);
    textLayout.draw(g2, x, y);
}

From source file:TextLayoutWithCarets.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

    if (mInitialized == false)
        initialize(g2);// ww w.j av a 2  s  .  c om

    float x = 20, y = 80;
    mLayout.draw(g2, x, y);

    // Create a plain stroke and a dashed stroke.
    Stroke[] caretStrokes = new Stroke[2];
    caretStrokes[0] = new BasicStroke();
    caretStrokes[1] = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 0, new float[] { 4, 4 },
            0);

    // Now draw the carets
    Shape[] carets = mLayout.getCaretShapes(mHit.getInsertionIndex());
    for (int i = 0; i < carets.length; i++) {
        if (carets[i] != null) {
            AffineTransform at = AffineTransform.getTranslateInstance(x, y);
            Shape shape = at.createTransformedShape(carets[i]);
            g2.setStroke(caretStrokes[i]);
            g2.draw(shape);
        }
    }
}

From source file:RollingText.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    String s = "Java Source and Support.";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);// ww  w  .  j ava  2 s .  c om

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}