Example usage for java.awt.geom AffineTransform AffineTransform

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

Introduction

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

Prototype

public AffineTransform(double m00, double m10, double m01, double m11, double m02, double m12) 

Source Link

Document

Constructs a new AffineTransform from 6 double precision values representing the 6 specifiable entries of the 3x3 transformation matrix.

Usage

From source file:org.photovault.swingui.PhotoCollectionThumbView.java

private void paintThumbnail(Graphics2D g2, PhotoInfo photo, int startx, int starty, boolean isSelected) {
    log.debug("paintThumbnail entry " + photo.getUuid());
    long startTime = System.currentTimeMillis();
    long thumbReadyTime = 0;
    long thumbDrawnTime = 0;
    long endTime = 0;
    // Current position in which attributes can be drawn
    int ypos = starty + rowHeight / 2;
    boolean useOldThumbnail = false;

    Thumbnail thumbnail = null;/*from  www  . j av a  2 s  .com*/
    log.debug("finding thumb");
    boolean hasThumbnail = photo.hasThumbnail();
    log.debug("asked if has thumb");
    if (hasThumbnail) {
        log.debug("Photo " + photo.getUuid() + " has thumbnail");
        thumbnail = photo.getThumbnail();
        log.debug("got thumbnail");
    } else {
        /*
         Check if the thumbnail has been just invalidated. If so, use the 
         old one until we get the new thumbnail created.
         */
        thumbnail = photo.getOldThumbnail();
        if (thumbnail != null) {
            useOldThumbnail = true;
        } else {
            // No success, use default thumnail.
            thumbnail = Thumbnail.getDefaultThumbnail();
        }

        // Inform background task scheduler that we have some work to do
        ctrl.getBackgroundTaskScheduler().registerTaskProducer(this, TaskPriority.CREATE_VISIBLE_THUMBNAIL);
    }
    thumbReadyTime = System.currentTimeMillis();

    log.debug("starting to draw");
    // Find the position for the thumbnail
    BufferedImage img = thumbnail.getImage();
    if (img == null) {
        thumbnail = Thumbnail.getDefaultThumbnail();
        img = thumbnail.getImage();
    }

    float scaleX = ((float) thumbWidth) / ((float) img.getWidth());
    float scaleY = ((float) thumbHeight) / ((float) img.getHeight());
    float scale = Math.min(scaleX, scaleY);
    int w = (int) (img.getWidth() * scale);
    int h = (int) (img.getHeight() * scale);

    int x = startx + (columnWidth - w) / (int) 2;
    int y = starty + (rowHeight - h) / (int) 2;

    log.debug("drawing thumbnail");

    // Draw shadow
    int offset = isSelected ? 2 : 0;
    int shadowX[] = { x + 3 - offset, x + w + 1 + offset, x + w + 1 + offset };
    int shadowY[] = { y + h + 1 + offset, y + h + 1 + offset, y + 3 - offset };
    GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, shadowX.length);
    polyline.moveTo(shadowX[0], shadowY[0]);
    for (int index = 1; index < shadowX.length; index++) {
        polyline.lineTo(shadowX[index], shadowY[index]);
    }
    ;
    BasicStroke shadowStroke = new BasicStroke(4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
    Stroke oldStroke = g2.getStroke();
    g2.setStroke(shadowStroke);
    g2.setColor(Color.DARK_GRAY);
    g2.draw(polyline);
    g2.setStroke(oldStroke);

    // Paint thumbnail
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2.drawImage(img, new AffineTransform(scale, 0f, 0f, scale, x, y), null);
    if (useOldThumbnail) {
        creatingThumbIcon.paintIcon(this, g2,
                startx + (columnWidth - creatingThumbIcon.getIconWidth()) / (int) 2,
                starty + (rowHeight - creatingThumbIcon.getIconHeight()) / (int) 2);
    }
    log.debug("Drawn, drawing decorations");
    if (isSelected) {
        Stroke prevStroke = g2.getStroke();
        Color prevColor = g2.getColor();
        g2.setStroke(new BasicStroke(3.0f));
        g2.setColor(Color.BLUE);
        g2.drawRect(x, y, w, h);
        g2.setColor(prevColor);
        g2.setStroke(prevStroke);
    }

    thumbDrawnTime = System.currentTimeMillis();

    boolean drawAttrs = (thumbWidth >= 100);
    if (drawAttrs) {
        // Increase ypos so that attributes are drawn under the image
        ypos += ((int) h) / 2 + 3;

        // Draw the attributes

        // Draw the qualoity icon to the upper left corner of the thumbnail
        int quality = photo.getQuality();
        if (showQuality && quality != 0) {
            int qx = startx + (columnWidth - quality * starIcon.getIconWidth()) / (int) 2;
            for (int n = 0; n < quality; n++) {
                starIcon.paintIcon(this, g2, qx, ypos);
                qx += starIcon.getIconWidth();
            }
            ypos += starIcon.getIconHeight();
        }
        ypos += 6;

        if (photo.getRawSettings() != null) {
            // Draw the "RAW" icon
            int rx = startx + (columnWidth + w - rawIcon.getIconWidth()) / (int) 2 - 5;
            int ry = starty + (columnWidth - h - rawIcon.getIconHeight()) / (int) 2 + 5;
            rawIcon.paintIcon(this, g2, rx, ry);
        }
        if (photo.getHistory().getHeads().size() > 1) {
            // Draw the "unresolved conflicts" icon
            int rx = startx + (columnWidth + w - 10) / (int) 2 - 20;
            int ry = starty + (columnWidth - h - 10) / (int) 2;
            g2.setColor(Color.RED);
            g2.fillRect(rx, ry, 10, 10);
        }

        Color prevBkg = g2.getBackground();
        if (isSelected) {
            g2.setBackground(Color.BLUE);
        } else {
            g2.setBackground(this.getBackground());
        }
        Font attrFont = new Font("Arial", Font.PLAIN, 10);
        FontRenderContext frc = g2.getFontRenderContext();
        if (showDate && photo.getShootTime() != null) {
            FuzzyDate fd = new FuzzyDate(photo.getShootTime(), photo.getTimeAccuracy());

            String dateStr = fd.format();
            TextLayout txt = new TextLayout(dateStr, attrFont, frc);
            // Calculate the position for the text
            Rectangle2D bounds = txt.getBounds();
            int xpos = startx + ((int) (columnWidth - bounds.getWidth())) / 2 - (int) bounds.getMinX();
            g2.clearRect(xpos - 2, ypos - 2, (int) bounds.getWidth() + 4, (int) bounds.getHeight() + 4);
            txt.draw(g2, xpos, (int) (ypos + bounds.getHeight()));
            ypos += bounds.getHeight() + 4;
        }
        String shootPlace = photo.getShootingPlace();
        if (showPlace && shootPlace != null && shootPlace.length() > 0) {
            TextLayout txt = new TextLayout(photo.getShootingPlace(), attrFont, frc);
            // Calculate the position for the text
            Rectangle2D bounds = txt.getBounds();
            int xpos = startx + ((int) (columnWidth - bounds.getWidth())) / 2 - (int) bounds.getMinX();

            g2.clearRect(xpos - 2, ypos - 2, (int) bounds.getWidth() + 4, (int) bounds.getHeight() + 4);
            txt.draw(g2, xpos, (int) (ypos + bounds.getHeight()));
            ypos += bounds.getHeight() + 4;
        }
        g2.setBackground(prevBkg);
    }
    endTime = System.currentTimeMillis();
    log.debug("paintThumbnail: exit " + photo.getUuid());
    log.debug("Thumb fetch " + (thumbReadyTime - startTime) + " ms");
    log.debug("Thumb draw " + (thumbDrawnTime - thumbReadyTime) + " ms");
    log.debug("Deacoration draw " + (endTime - thumbDrawnTime) + " ms");
    log.debug("Total " + (endTime - startTime) + " ms");
}

From source file:org.apache.pdfbox.pdmodel.PDPageContentStream.java

/**
 * Draw an xobject(form or image) at the x,y coordinates and a certain width and height.
 *
 * @param xobject The xobject to draw./*from   w  ww.  j a va  2 s  .  com*/
 * @param x The x-coordinate to draw the image.
 * @param y The y-coordinate to draw the image.
 * @param width The width of the image to draw.
 * @param height The height of the image to draw.
 *
 * @throws IOException If there is an error writing to the stream.
 * @deprecated Use {@link #drawImage} instead.
 */
@Deprecated
public void drawXObject(PDXObject xobject, float x, float y, float width, float height) throws IOException {
    AffineTransform transform = new AffineTransform(width, 0, 0, height, x, y);
    drawXObject(xobject, transform);
}

From source file:org.tsho.dmc2.core.chart.LyapunovRenderer.java

private AffineTransform createTransform(double x, double y, BufferedImage image, Rectangle2D dataArea) {
    int wi = image.getWidth();
    wi--;//  w w  w .j  ava2 s . c om
    int hi = image.getHeight();
    hi--;
    double wd = dataArea.getWidth();
    wd--;
    double hd = dataArea.getHeight();
    hd--;
    double a = wd / wi;
    double b = hd / hi;
    double m00 = a;
    double m01 = 0;
    double m02 = -x * a + x;
    double m10 = 0;
    double m11 = b;
    double m12 = -y * b + y;
    AffineTransform at = new AffineTransform(m00, m10, m01, m11, m02, m12);
    return at;
}

From source file:org.geotools.coverage.io.util.Utilities.java

/**
 * This method creates the GridCoverage2D from the underlying file given a specified envelope, and a requested dimension.
 * /* www . ja va 2s  .  c  om*/
 * @param imageIndex
 * @param coordinateReferenceSystem
 * @param generalEnvelope
 * @param mathTransform
 * 
 * @param iUseJAI specify if the underlying read process should leverage on a JAI ImageRead operation or a simple direct call to the {@code read}
 *        method of a proper {@code ImageReader}.
 * @param useMultithreading specify if the underlying read process should use multithreading when a JAI ImageRead operation is requested
 * @param overviewPolicy the overview policy which need to be adopted
 * @return a {@code GridCoverage}
 * 
 * @throws java.io.IOException
 */
public static GridCoverage createCoverage(ImageReaderSpi spi, Object input, final int imageIndex,
        ImageReadParam imageReadParam, final boolean useJAI, final boolean useMultithreading,
        final boolean newTransform, final GridSampleDimension[] sampleDimensions, final String coverageName,
        GridCoverageFactory coverageFactory, MathTransform raster2Model,
        CoordinateReferenceSystem coordinateReferenceSystem, GeneralEnvelope coverageEnvelope2D)
        throws IOException {
    // ////////////////////////////////////////////////////////////////////
    //
    // Doing an image read for reading the coverage.
    //
    // ////////////////////////////////////////////////////////////////////
    final PlanarImage image = readImage(spi, input, imageIndex, useJAI, imageReadParam, useMultithreading);

    // /////////////////////////////////////////////////////////////////////
    //
    // Creating the coverage
    //
    // /////////////////////////////////////////////////////////////////////
    if (newTransform) {
        // I need to calculate a new transformation (raster2Model)
        // between the cropped image and the required envelope
        final int ssWidth = image.getWidth();
        final int ssHeight = image.getHeight();

        // //
        //
        // setting new coefficients to define a new affineTransformation
        // to be applied to the grid to world transformation
        // ------------------------------------------------------
        //
        // With respect to the original envelope, the obtained
        // planarImage needs to be rescaled and translated. The scaling
        // factors are computed as the ratio between the cropped source
        // region sizes and the read image sizes. The translate
        // settings are represented by the offsets of the source region.
        //
        // //
        final Rectangle sourceRegion = imageReadParam.getSourceRegion();
        final double scaleX = sourceRegion.width / (1.0 * ssWidth);
        final double scaleY = sourceRegion.height / (1.0 * ssHeight);
        final double translateX = sourceRegion.x;
        final double translateY = sourceRegion.y;
        return Utilities.createCoverageFromImage(coverageFactory, coverageName, imageIndex, image,
                ConcatenatedTransform.create(
                        ProjectiveTransform
                                .create(new AffineTransform(scaleX, 0, 0, scaleY, translateX, translateY)),
                        raster2Model),
                coordinateReferenceSystem, (GeneralEnvelope) null, sampleDimensions, true);
    } else {
        // In case of no transformation is required (As an instance,
        // when reading the whole image)
        return Utilities.createCoverageFromImage(coverageFactory, coverageName, imageIndex, image,
                (MathTransform) null, (CoordinateReferenceSystem) null, coverageEnvelope2D, sampleDimensions,
                true);
    }
}