use of java.awt.geom.AffineTransform in project android_frameworks_base by ResurrectionRemix.
the class Path_Delegate method offset.
/**
* Offset the path by (dx,dy), returning true on success
*
* @param dx The amount in the X direction to offset the entire path
* @param dy The amount in the Y direction to offset the entire path
*/
public void offset(float dx, float dy) {
GeneralPath newPath = new GeneralPath();
PathIterator iterator = mPath.getPathIterator(new AffineTransform(0, 0, dx, 0, 0, dy));
newPath.append(iterator, false);
mPath = newPath;
}
use of java.awt.geom.AffineTransform in project EnrichmentMapApp by BaderLab.
the class AbstractChartLayer method draw.
@Override
public void draw(final Graphics2D g, final Shape shape, final CyNetworkView networkView, final View<? extends CyIdentifiable> view) {
// Give JFreeChart a larger area to draw into, so the proportions of the chart elements looks better
final double scale = 2.0;
Rectangle2D newBounds = new Rectangle2D.Double(bounds.getX() * scale, bounds.getY() * scale, bounds.getWidth() * scale, bounds.getHeight() * scale);
// Of course, we also have to ask Graphics2D to apply the inverse transformation
final double invScale = 1.0 / scale;
final AffineTransform at = new AffineTransform();
at.scale(invScale, invScale);
g.transform(at);
// Check to see if we have a current alpha composite
Composite comp = g.getComposite();
if (comp instanceof AlphaComposite) {
float alpha = ((AlphaComposite) comp).getAlpha();
JFreeChart fc = getChart();
Plot plot = fc.getPlot();
plot.setForegroundAlpha(alpha);
fc.draw(g, newBounds);
} else {
getChart().draw(g, newBounds);
}
// Make sure Graphics2D is "back to normal" before returning
try {
at.invert();
} catch (NoninvertibleTransformException e) {
e.printStackTrace();
}
g.transform(at);
}
use of java.awt.geom.AffineTransform in project Overloaded by CJ-MC-Mods.
the class CompressedBlockAssets method scaleToWidth.
private static BufferedImage scaleToWidth(@Nonnull BufferedImage original, int width) {
double scale = original.getWidth() / (double) width;
AffineTransform at = new AffineTransform();
at.scale(1 / scale, 1 / scale);
AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
return scaleOp.filter(original, null);
}
use of java.awt.geom.AffineTransform in project Activiti by Activiti.
the class DefaultProcessDiagramCanvas method drawDefaultSequenceFlowIndicator.
public void drawDefaultSequenceFlowIndicator(Line2D.Double line, double scaleFactor) {
double length = DEFAULT_INDICATOR_WIDTH / scaleFactor, halfOfLength = length / 2, f = 8;
Line2D.Double defaultIndicator = new Line2D.Double(-halfOfLength, 0, halfOfLength, 0);
double angle = Math.atan2(line.y2 - line.y1, line.x2 - line.x1);
double dx = f * Math.cos(angle), dy = f * Math.sin(angle), x1 = line.x1 + dx, y1 = line.y1 + dy;
AffineTransform transformation = new AffineTransform();
transformation.setToIdentity();
transformation.translate(x1, y1);
transformation.rotate((angle - 3 * Math.PI / 4));
AffineTransform originalTransformation = g.getTransform();
g.setTransform(transformation);
g.draw(defaultIndicator);
g.setTransform(originalTransformation);
}
use of java.awt.geom.AffineTransform in project Botnak by Gocnak.
the class Scalr method rotate.
/**
* Used to apply a {@link Rotation} and then <code>0</code> or more
* {@link BufferedImageOp}s to a given image and return the result.
* <p>
* <strong>TIP</strong>: This operation leaves the original <code>src</code>
* image unmodified. If the caller is done with the <code>src</code> image
* after getting the result of this operation, remember to call
* {@link BufferedImage#flush()} on the <code>src</code> to free up native
* resources and make it easier for the GC to collect the unused image.
*
* @param src The image that will have the rotation applied to it.
* @param rotation The rotation that will be applied to the image.
* @param ops Zero or more optional image operations (e.g. sharpen, blur,
* etc.) that can be applied to the final result before returning
* the image.
* @return a new {@link BufferedImage} representing <code>src</code> rotated
* by the given amount and any optional ops applied to it.
* @throws IllegalArgumentException if <code>src</code> is <code>null</code>.
* @throws IllegalArgumentException if <code>rotation</code> is <code>null</code>.
* @throws ImagingOpException if one of the given {@link BufferedImageOp}s fails to apply.
* These exceptions bubble up from the inside of most of the
* {@link BufferedImageOp} implementations and are explicitly
* defined on the imgscalr API to make it easier for callers to
* catch the exception (if they are passing along optional ops
* to be applied). imgscalr takes detailed steps to avoid the
* most common pitfalls that will cause {@link BufferedImageOp}s
* to fail, even when using straight forward JDK-image
* operations.
* @see Rotation
*/
public static BufferedImage rotate(BufferedImage src, Rotation rotation, BufferedImageOp... ops) throws IllegalArgumentException, ImagingOpException {
long t = -1;
if (DEBUG)
t = System.currentTimeMillis();
if (src == null)
throw new IllegalArgumentException("src cannot be null");
if (rotation == null)
throw new IllegalArgumentException("rotation cannot be null");
if (DEBUG)
log(0, "Rotating Image [%s]...", rotation);
/*
* Setup the default width/height values from our image.
*
* In the case of a 90 or 270 (-90) degree rotation, these two values
* flip-flop and we will correct those cases down below in the switch
* statement.
*/
int newWidth = src.getWidth();
int newHeight = src.getHeight();
/*
* We create a transform per operation request as (oddly enough) it ends
* up being faster for the VM to create, use and destroy these instances
* than it is to re-use a single AffineTransform per-thread via the
* AffineTransform.setTo(...) methods which was my first choice (less
* object creation); after benchmarking this explicit case and looking
* at just how much code gets run inside of setTo() I opted for a new AT
* for every rotation.
*
* Besides the performance win, trying to safely reuse AffineTransforms
* via setTo(...) would have required ThreadLocal instances to avoid
* race conditions where two or more resize threads are manipulating the
* same transform before applying it.
*
* Misusing ThreadLocals are one of the #1 reasons for memory leaks in
* server applications and since we have no nice way to hook into the
* init/destroy Servlet cycle or any other initialization cycle for this
* library to automatically call ThreadLocal.remove() to avoid the
* memory leak, it would have made using this library *safely* on the
* server side much harder.
*
* So we opt for creating individual transforms per rotation op and let
* the VM clean them up in a GC. I only clarify all this reasoning here
* for anyone else reading this code and being tempted to reuse the AT
* instances of performance gains; there aren't any AND you get a lot of
* pain along with it.
*/
AffineTransform tx = new AffineTransform();
switch(rotation) {
case CW_90:
/*
* A 90 or -90 degree rotation will cause the height and width to
* flip-flop from the original image to the rotated one.
*/
newWidth = src.getHeight();
newHeight = src.getWidth();
// Reminder: newWidth == result.getHeight() at this point
tx.translate(newWidth, 0);
tx.quadrantRotate(1);
break;
case CW_270:
/*
* A 90 or -90 degree rotation will cause the height and width to
* flip-flop from the original image to the rotated one.
*/
newWidth = src.getHeight();
newHeight = src.getWidth();
// Reminder: newHeight == result.getWidth() at this point
tx.translate(0, newHeight);
tx.quadrantRotate(3);
break;
case CW_180:
tx.translate(newWidth, newHeight);
tx.quadrantRotate(2);
break;
case FLIP_HORZ:
tx.translate(newWidth, 0);
tx.scale(-1.0, 1.0);
break;
case FLIP_VERT:
tx.translate(0, newHeight);
tx.scale(1.0, -1.0);
break;
}
// Create our target image we will render the rotated result to.
BufferedImage result = createOptimalImage(src, newWidth, newHeight);
Graphics2D g2d = result.createGraphics();
/*
* Render the resultant image to our new rotatedImage buffer, applying
* the AffineTransform that we calculated above during rendering so the
* pixels from the old position are transposed to the new positions in
* the resulting image correctly.
*/
g2d.drawImage(src, tx, null);
g2d.dispose();
if (DEBUG)
log(0, "Rotation Applied in %d ms, result [width=%d, height=%d]", System.currentTimeMillis() - t, result.getWidth(), result.getHeight());
// Apply any optional operations (if specified).
if (ops != null && ops.length > 0)
result = apply(result, ops);
return result;
}
Aggregations