Java Code Examples for java.awt.geom.AffineTransform#getTranslateInstance()

The following examples show how to use java.awt.geom.AffineTransform#getTranslateInstance() . These examples are extracted from open source projects. 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
@Override
    public void putDab(PPoint p, double theta) {
        var transform = AffineTransform.getTranslateInstance(
                p.getImX() - radius,
                p.getImY() - radius
        );

        // TODO SrcOver allows to smudge into transparent areas, but transparency
        // can't be smudged into non-transparent areas
        // DstOver allows only smudging into transparent
        targetG.setComposite(AlphaComposite.SrcOver.derive(strength));

// SrcAtop: can't smudge into transparent areas
//        targetG.setComposite(AlphaComposite.SrcAtop.derive(strength));

//        targetG.setComposite(BlendComposite.CrossFade.derive(strength));

        targetG.drawImage(brushImage, transform, null);

        last = p;

        repaintComp(p);
    }
 
Example 2
Source Project: java-swing-tips   File: MainPanel.java    License: MIT License 6 votes vote down vote up
private void update(MouseEvent e, JLayer<? extends V> l) {
  Shape s = null;
  switch (e.getID()) {
    case MouseEvent.MOUSE_ENTERED:
    case MouseEvent.MOUSE_MOVED:
      Component c = e.getComponent();
      if (c instanceof AbstractButton) {
        AbstractButton b = (AbstractButton) c;
        if (b.getIcon() instanceof ArrowToggleButtonBarCellIcon) {
          ArrowToggleButtonBarCellIcon icon = (ArrowToggleButtonBarCellIcon) b.getIcon();
          Rectangle r = c.getBounds();
          AffineTransform at = AffineTransform.getTranslateInstance(r.x, r.y);
          s = at.createTransformedShape(icon.getShape());
        }
      }
      break;
    default:
      break;
  }
  if (!Objects.equals(s, shape)) {
    shape = s;
    l.getView().repaint();
  }
}
 
Example 3
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    if(shapes == null) initShapes();
    // Keep shapes centered on panel.
    double x = (getWidth()  - scale*size.width)/2;
    double y = (getHeight() - scale*size.height)/2;
    AffineTransform at = AffineTransform.getTranslateInstance(x, y);
    at.scale(scale, scale);
    g2.setPaint(Color.blue);
    g2.draw(at.createTransformedShape(shapes[0]));
    g2.setPaint(Color.green.darker());
    g2.draw(at.createTransformedShape(shapes[1]));
    g2.setPaint(new Color(240,240,200));
    g2.fill(at.createTransformedShape(shapes[2]));
    g2.setPaint(Color.red);
    g2.draw(at.createTransformedShape(shapes[2]));
}
 
Example 4
private Area[] buildTicker() {
    Area[] ticker = new Area[barsCount];
    Point2D.Double center = new Point2D.Double((double) getWidth() / 2, (double) getHeight() / 2);
    double fixedAngle = 2.0 * Math.PI / (barsCount);

    for (double i = 0.0; i < barsCount; i++) {
        Area primitive = buildPrimitive();

        AffineTransform toCenter = AffineTransform.getTranslateInstance(center.getX(), center.getY());
        AffineTransform toBorder = AffineTransform.getTranslateInstance(45.0, -6.0);
        AffineTransform toCircle = AffineTransform.getRotateInstance(-i * fixedAngle, center.getX(), center.getY());

        AffineTransform toWheel = new AffineTransform();
        toWheel.concatenate(toCenter);
        toWheel.concatenate(toBorder);

        primitive.transform(toWheel);
        primitive.transform(toCircle);

        ticker[(int) i] = primitive;
    }

    return ticker;
}
 
Example 5
Source Project: coming   File: NPEfix10_ten_t.java    License: MIT License 6 votes vote down vote up
/** {@inheritDoc} */
public SubHyperplane<Euclidean2D> apply(final SubHyperplane<Euclidean2D> sub,
                                        final Hyperplane<Euclidean3D> original,
                                        final Hyperplane<Euclidean3D> transformed) {
    if (original != cachedOriginal) {
        // we have changed hyperplane, reset the in-hyperplane transform

        final Plane   oPlane = (Plane) original;
        final Plane   tPlane = (Plane) transformed;
        final Vector2D shift  = tPlane.toSubSpace((Point<Euclidean3D>) apply(oPlane.getOrigin()));
        final AffineTransform at =
                AffineTransform.getTranslateInstance(shift.getX(), shift.getY());

        cachedOriginal  = (Plane) original;
        cachedTransform =
                org.apache.commons.math3.geometry.euclidean.twod.Line.getTransform(at);

    }

    return ((SubLine) sub).applyTransform(cachedTransform);

}
 
Example 6
Source Project: jdk1.8-source-analysis   File: TextLine.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return the union of the visual bounds of all the components.
 * This incorporates the path.  It does not include logical
 * bounds (used by carets).
 */
public Rectangle2D getVisualBounds() {
    Rectangle2D result = null;

    for (int i = 0, n = 0; i < fComponents.length; i++, n += 2) {
        TextLineComponent tlc = fComponents[getComponentLogicalIndex(i)];
        Rectangle2D r = tlc.getVisualBounds();

        Point2D.Float pt = new Point2D.Float(locs[n], locs[n+1]);
        if (lp == null) {
            r.setRect(r.getMinX() + pt.x, r.getMinY() + pt.y,
                      r.getWidth(), r.getHeight());
        } else {
            lp.pathToPoint(pt, false, pt);

            AffineTransform at = tlc.getBaselineTransform();
            if (at != null) {
                AffineTransform tx = AffineTransform.getTranslateInstance
                    (pt.x - at.getTranslateX(), pt.y - at.getTranslateY());
                tx.concatenate(at);
                r = tx.createTransformedShape(r).getBounds2D();
            } else {
                r.setRect(r.getMinX() + pt.x, r.getMinY() + pt.y,
                          r.getWidth(), r.getHeight());
            }
        }

        if (result == null) {
            result = r;
        } else {
            result.add(r);
        }
    }

    if (result == null) {
        result = new Rectangle2D.Float(Float.MAX_VALUE, Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
    }

    return result;
}
 
Example 7
Source Project: hottub   File: TextLine.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the union of the visual bounds of all the components.
 * This incorporates the path.  It does not include logical
 * bounds (used by carets).
 */
public Rectangle2D getVisualBounds() {
    Rectangle2D result = null;

    for (int i = 0, n = 0; i < fComponents.length; i++, n += 2) {
        TextLineComponent tlc = fComponents[getComponentLogicalIndex(i)];
        Rectangle2D r = tlc.getVisualBounds();

        Point2D.Float pt = new Point2D.Float(locs[n], locs[n+1]);
        if (lp == null) {
            r.setRect(r.getMinX() + pt.x, r.getMinY() + pt.y,
                      r.getWidth(), r.getHeight());
        } else {
            lp.pathToPoint(pt, false, pt);

            AffineTransform at = tlc.getBaselineTransform();
            if (at != null) {
                AffineTransform tx = AffineTransform.getTranslateInstance
                    (pt.x - at.getTranslateX(), pt.y - at.getTranslateY());
                tx.concatenate(at);
                r = tx.createTransformedShape(r).getBounds2D();
            } else {
                r.setRect(r.getMinX() + pt.x, r.getMinY() + pt.y,
                          r.getWidth(), r.getHeight());
            }
        }

        if (result == null) {
            result = r;
        } else {
            result.add(r);
        }
    }

    if (result == null) {
        result = new Rectangle2D.Float(Float.MAX_VALUE, Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
    }

    return result;
}
 
Example 8
Source Project: JDKSourceCode1.8   File: TextLine.java    License: MIT License 5 votes vote down vote up
/**
 * Return the union of the visual bounds of all the components.
 * This incorporates the path.  It does not include logical
 * bounds (used by carets).
 */
public Rectangle2D getVisualBounds() {
    Rectangle2D result = null;

    for (int i = 0, n = 0; i < fComponents.length; i++, n += 2) {
        TextLineComponent tlc = fComponents[getComponentLogicalIndex(i)];
        Rectangle2D r = tlc.getVisualBounds();

        Point2D.Float pt = new Point2D.Float(locs[n], locs[n+1]);
        if (lp == null) {
            r.setRect(r.getMinX() + pt.x, r.getMinY() + pt.y,
                      r.getWidth(), r.getHeight());
        } else {
            lp.pathToPoint(pt, false, pt);

            AffineTransform at = tlc.getBaselineTransform();
            if (at != null) {
                AffineTransform tx = AffineTransform.getTranslateInstance
                    (pt.x - at.getTranslateX(), pt.y - at.getTranslateY());
                tx.concatenate(at);
                r = tx.createTransformedShape(r).getBounds2D();
            } else {
                r.setRect(r.getMinX() + pt.x, r.getMinY() + pt.y,
                          r.getWidth(), r.getHeight());
            }
        }

        if (result == null) {
            result = r;
        } else {
            result.add(r);
        }
    }

    if (result == null) {
        result = new Rectangle2D.Float(Float.MAX_VALUE, Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
    }

    return result;
}
 
Example 9
private static AffineTransform createGradientTransform(Rectangle2D r) {
    double cx = r.getCenterX();
    double cy = r.getCenterY();
    AffineTransform xform = AffineTransform.getTranslateInstance(cx, cy);
    xform.scale(r.getWidth()/2, r.getHeight()/2);
    xform.translate(-cx, -cy);
    return xform;
}
 
Example 10
private void cacheAllStrengths() {
    final int d = radius * 2 + 1;
    
    // Create image from original brush
    final byte[] rasterData = new byte[d * d];
    for (int dx = -radius; dx <= radius; dx++) {
        for (int dy = -radius; dy <= radius; dy++) {
            rasterData[dx + radius + (dy + radius) * d] = (byte) (brush.getFullStrength(dx, dy) * 255);
        }
    }
    final DataBuffer imageDataBuffer = new DataBufferByte(rasterData, d * d);
    final Raster raster = Raster.createRaster(new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE, d, d, new int[] {255}), imageDataBuffer, new Point(0, 0));
    
    // Rotate image
    final double a = degrees / 180d * Math.PI;
    AffineTransformOp transformOp = new AffineTransformOp(AffineTransform.getRotateInstance(a), AffineTransformOp.TYPE_BICUBIC);
    Rectangle2D transformedBounds = transformOp.getBounds2D(raster);
    AffineTransform transform = AffineTransform.getTranslateInstance(-transformedBounds.getX(), -transformedBounds.getY());
    transform.rotate(a);
    transformOp = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
    fullStrengthCache = transformOp.filter(raster, null);

    // Calculate effective dimensions
    effectiveWidth = fullStrengthCache.getWidth();
    effectiveHeight = fullStrengthCache.getHeight();
    effectiveRadius = effectiveWidth / 2;
}
 
Example 11
Source Project: gcs   File: PdfGraphics2D.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @see Graphics#drawImage(Image, int, int, int, int, Color, ImageObserver)
 */
@Override
public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
	waitForImage(img);
	double scalex = width / (double) img.getWidth(observer);
	double scaley = height / (double) img.getHeight(observer);
	AffineTransform tx = AffineTransform.getTranslateInstance(x, y);
	tx.scale(scalex, scaley);
	return drawImage(img, null, tx, bgcolor, observer);
}
 
Example 12
Source Project: swingsane   File: PreviewPanel.java    License: Apache License 2.0 5 votes vote down vote up
public final void renderImagePreview(Graphics2D g) {

    if (sourceImage != null) {

      if (convertedImage == null) {

        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
            .getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();

        if (sourceImage.getColorModel() != gc.getColorModel()) {
          convertedImage = gc.createCompatibleImage(sourceImage.getWidth(),
              sourceImage.getHeight(), Transparency.OPAQUE);
          Graphics2D g2d = convertedImage.createGraphics();
          g2d.drawImage(sourceImage, 0, 0, sourceImage.getWidth(), sourceImage.getHeight(), null);
          convertedImage.flush();
          g2d.dispose();
        } else {
          convertedImage = sourceImage;
        }

      }

      g.setColor(Color.darkGray);
      g.fill(new Rectangle2D.Double(0, 0, imagePreviewLabel.getWidth(), imagePreviewLabel
          .getHeight()));

      g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
          RenderingHints.VALUE_FRACTIONALMETRICS_ON);
      g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);

      double x = (imagePreviewLabel.getWidth() / 2) - ((sourceImage.getWidth() * realZoom) / 2);
      double y = (imagePreviewLabel.getHeight() / 2) - ((sourceImage.getHeight() * realZoom) / 2);
      AffineTransform at = AffineTransform.getTranslateInstance(x, y);
      at.scale(realZoom, realZoom);
      g.drawRenderedImage(convertedImage, at);

    }

  }
 
Example 13
/**
 * Report a line which concatenates the corresponding (first or last) lines of all
 * provided staves (assumed to be side by side), slightly translated vertically by
 * verticalMargin.
 *
 * @param staffList the horizontal sequence of staves
 * @param side      the desired vertical side
 * @return iterator on the global line
 */
public PathIterator getGlobalLine (List<Staff> staffList,
                                   VerticalSide side)
{
    if (staffList.isEmpty()) {
        return null;
    }

    final GeoPath globalLine = new GeoPath();

    // Point on left
    Staff leftStaff = staffList.get(0);
    LineInfo leftLine = (side == TOP) ? leftStaff.getFirstLine() : leftStaff.getLastLine();
    NaturalSpline leftSpline = leftLine.getSpline();
    globalLine.moveTo(0, leftSpline.getFirstPoint().getY());

    // Proper line of each staff
    for (Staff staff : staffList) {
        LineInfo fLine = (side == TOP) ? staff.getFirstLine() : staff.getLastLine();
        globalLine.append(fLine.getSpline(), true);
    }

    // Point on right
    Staff rightStaff = staffList.get(staffList.size() - 1);
    LineInfo rightLine = (side == TOP) ? rightStaff.getFirstLine() : rightStaff.getLastLine();
    NaturalSpline rightSpline = rightLine.getSpline();
    globalLine.lineTo(sheet.getWidth(), rightSpline.getLastPoint().getY());

    final int verticalMargin = sheet.getScale().toPixels(constants.verticalAreaMargin);
    AffineTransform at = AffineTransform.getTranslateInstance(
            0,
            ((side == TOP) ? (-verticalMargin) : verticalMargin));

    return globalLine.getPathIterator(at);
}
 
Example 14
Source Project: Rails   File: GUITile.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void paintTile(Graphics2D g2, GUIHex.HexPoint origin, GUIHex hex, Tile tile, HexSide rotation, double tileScale, int zoomStep) {

        // Preprinted tiles can have a different picture ID, defined per hex or per tile.
        // MapHex refers back to Tile if necessary
        String picId = hex.getHex().getPictureId(tile);

        ImageLoader imageLoader = ImageLoader.getInstance();
        BufferedImage tileImage = imageLoader.getTile(picId, zoomStep);

        if (tileImage != null) {

            double radians = MapOrientation.rotationInRadians(tile, rotation);
            HexPoint center = new HexPoint(
                    tileImage.getWidth() * SVG_X_CENTER_LOC * tileScale,
                    tileImage.getHeight()* SVG_Y_CENTER_LOC * tileScale
            );
            HexPoint difference = HexPoint.difference(origin, center);
            AffineTransform af = AffineTransform.getTranslateInstance(difference.getX(), difference.getY());
            af.rotate(radians, center.getX(), center.getY());
            af.scale(tileScale, tileScale);

            AffineTransformOp aop = new AffineTransformOp(af,
                    GUIGlobals.getRenderingHints());
            // FIXME: Change this to a sub-pixel approach
            // compare with
            // http://stackoverflow.com/questions/8676909/drawing-an-image-using-sub-pixel-level-accuracy-using-graphics2d
            //g2.drawImage(tileImage, aop, (int) difference.getX(), (int) difference.getY());
            // already a first approach, integrated into the affine transform, however it does not
            // increase the quality of the map
            g2.drawImage(tileImage, aop, 0, 0);

        } else {
            log.error("No image for tile {} on hex {}", tile, hex.toText());
        }
    }
 
Example 15
private AffineTransform getTranslate() {
    return AffineTransform.getTranslateInstance(-topLeft.getX(), -bottomRight.getY());
}
 
Example 16
Source Project: dsworkbench   File: Arrow.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void renderForm(Graphics2D g2d) {
    Point2D.Double s = MapPanel.getSingleton().virtualPosToSceenPosDouble(getXPos(), getYPos());
    Point2D.Double e = MapPanel.getSingleton().virtualPosToSceenPosDouble(xPosEnd, yPosEnd);
    if (xPosEnd == -1 && yPosEnd == -1) {
        e = MapPanel.getSingleton().virtualPosToSceenPosDouble(getXPos(), getYPos());
    }
    java.awt.Rectangle mapBounds = MapPanel.getSingleton().getBounds();

    setVisibleOnMap(mapBounds.intersectsLine(new Line2D.Double(s, e)));
    if (!isVisibleOnMap()) {
        return;
    }

    //store properties
    Stroke before = g2d.getStroke();
    Composite coBefore = g2d.getComposite();
    Font fBefore = g2d.getFont();
    Color cBefore = g2d.getColor();
    checkShowMode(g2d, drawColor);
    //start draw
    g2d.setFont(fBefore.deriveFont((float) getTextSize()));
    g2d.setStroke(getStroke());
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, drawAlpha));

    double h = Math.abs(s.y - e.y);
    double c = s.distance(e);
    double a = Math.asin(h / c);

    /*
     * x > 0, y > 0: quadrant I.
    x < 0, y > 0: quadrant II.
    x < 0, y < 0: quadrant III
    x > 0, y < 0: quadrant IV
     */
    path = new GeneralPath();
    path.moveTo(0, -10);
    path.lineTo(80, -10);
    path.lineTo(80, -20);
    path.lineTo(100, 0);
    path.lineTo(80, 20);
    path.lineTo(80, 10);
    path.lineTo(0, 10);
    path.closePath();

    double rot = 0;

    if (e.x > s.x && e.y >= s.y) {
        rot = Math.toDegrees(a);
    } else if (e.x <= s.x && e.y >= s.y) {
        rot = 180 - Math.toDegrees(a);
    } else if (e.x >= s.x && e.y <= s.y) {
        rot = 360 - Math.toDegrees(a);
    } else {
        rot = 180 + Math.toDegrees(a);
    }

    a = Math.toRadians(rot);
    AffineTransform trans = AffineTransform.getScaleInstance(c / 100.0, c / 210.0);
    path.transform(trans);
    trans = AffineTransform.getTranslateInstance(path.getBounds2D().getX(), 0);
    path.transform(trans);
    trans = AffineTransform.getRotateInstance(a, 0, 0);
    path.transform(trans);
    trans = AffineTransform.getTranslateInstance(s.x, s.y);
    path.transform(trans);

    if (filled) {
        g2d.fill(path);
    } else {
        g2d.draw(path);
    }
    drawDecoration(s, e, g2d);
    //reset properties
    g2d.setStroke(before);
    g2d.setComposite(coBefore);
    g2d.setFont(fBefore);
    g2d.setColor(cBefore);
}
 
Example 17
public RectListImpl getTranslation(int dx, int dy) {
    AffineTransform at = AffineTransform.getTranslateInstance(dx, dy);
    return new AreaImpl(theArea.createTransformedArea(at));
}
 
Example 18
/**
 * Returns a <code>Shape</code> whose interior corresponds to the
 * visual representation of the specified glyph
 * within this <code>GlyphVector</code>, offset to x,&nbsp;y.
 * The outline returned by this method is positioned around the
 * origin of each individual glyph.
 * @param glyphIndex the index into this <code>GlyphVector</code>
 * @param x the X coordinate of the location of this {@code GlyphVector}
 * @param y the Y coordinate of the location of this {@code GlyphVector}
 * @return a <code>Shape</code> that is the outline of the glyph
 *   at the specified <code>glyphIndex</code> of this
 *   <code>GlyphVector</code> when rendered at the specified
 *   coordinates.
 * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
 *   is less than 0 or greater than or equal to the number
 *   of glyphs in this <code>GlyphVector</code>
 * @since 1.4
 */
public Shape getGlyphOutline(int glyphIndex, float x, float y) {
    Shape s = getGlyphOutline(glyphIndex);
    AffineTransform at = AffineTransform.getTranslateInstance(x,y);
    return at.createTransformedShape(s);
    }
 
Example 19
Source Project: gcs   File: CloudyBorder.java    License: Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Returns the <code>Matrix</code> entry for the appearance stream form XObject.
 *
 * @return Matrix for appearance stream form XObject.
 */
AffineTransform getMatrix()
{
    return AffineTransform.getTranslateInstance(-bboxMinX, -bboxMinY);
}
 
Example 20
/**
 * Returns a <code>Shape</code> whose interior corresponds to the
 * visual representation of the specified glyph
 * within this <code>GlyphVector</code>, offset to x,&nbsp;y.
 * The outline returned by this method is positioned around the
 * origin of each individual glyph.
 * @param glyphIndex the index into this <code>GlyphVector</code>
 * @param x the X coordinate of the location of this {@code GlyphVector}
 * @param y the Y coordinate of the location of this {@code GlyphVector}
 * @return a <code>Shape</code> that is the outline of the glyph
 *   at the specified <code>glyphIndex</code> of this
 *   <code>GlyphVector</code> when rendered at the specified
 *   coordinates.
 * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
 *   is less than 0 or greater than or equal to the number
 *   of glyphs in this <code>GlyphVector</code>
 * @since 1.4
 */
public Shape getGlyphOutline(int glyphIndex, float x, float y) {
    Shape s = getGlyphOutline(glyphIndex);
    AffineTransform at = AffineTransform.getTranslateInstance(x,y);
    return at.createTransformedShape(s);
    }