Example usage for java.awt Graphics2D drawImage

List of usage examples for java.awt Graphics2D drawImage

Introduction

In this page you can find the example usage for java.awt Graphics2D drawImage.

Prototype

public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y);

Source Link

Document

Renders a BufferedImage that is filtered with a BufferedImageOp .

Usage

From source file:com.liusoft.dlog4j.action.PhotoAction.java

/**
 * /*w ww .j a  v  a 2 s .c  o m*/
 * @param ctx
 * @param imgURL
 * @param orient
 * @return
 * @throws IOException
 */
protected boolean rotate(HttpContext ctx, String imgURL, int orient) throws IOException {
    PhotoSaver saver = this.getPhotoSaver();
    InputStream inImg = saver.read(ctx, imgURL);
    BufferedImage old_img = (BufferedImage) ImageIO.read(inImg);
    int width = old_img.getWidth();
    int height = old_img.getHeight();
    BufferedImage new_img = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = new_img.createGraphics();

    AffineTransform origXform = g2d.getTransform();
    AffineTransform newXform = (AffineTransform) (origXform.clone());
    // center of rotation is center of the panel
    double radian = 0;
    double xRot = 0;
    double yRot = 0;
    switch (orient) {
    case 3:
        radian = 180.0;
        xRot = width / 2.0;
        yRot = height / 2.0;
    case 6:
        radian = 90.0;
        xRot = height / 2.0;
        yRot = xRot;
        break;
    case 8:
        radian = 270.0;
        xRot = width / 2.0;
        yRot = xRot;
        break;
    default:
        return false;
    }
    newXform.rotate(Math.toRadians(radian), xRot, yRot);

    g2d.setTransform(newXform);
    // draw image centered in panel
    g2d.drawImage(old_img, 0, 0, null);
    // Reset to Original
    g2d.setTransform(origXform);
    OutputStream out = saver.write(ctx, imgURL);
    try {
        ImageIO.write(new_img, "JPG", out);
    } finally {
        out.close();
    }
    return true;
}

From source file:com.openbravo.pos.util.ThumbNailBuilder.java

public Image getThumbNailText(Image img, String text) {
    /*//from   w ww  . j av a 2s. c o  m
     * Create an image containing a thumbnail of the product image,
     * or default image.
     * 
     * Then apply the text of the product name. Use text wrapping.
     * 
     * If the product name is too big for the label, ensure that
     * the first part is displayed.
     */

    img = getThumbNail(img);

    BufferedImage imgtext = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = imgtext.createGraphics();

    // The text
    // <p style="width: 100px"> DOES NOT WORK PROPERLY.
    // use width= instead.
    String html = "<html><p style=\"text-align:center\" width=\"" + imgtext.getWidth() + "\">"
            + StringEscapeUtils.escapeHtml(text) + "</p>";

    JLabel label = new JLabel(html);
    label.setOpaque(false);
    //label.setText("<html><center>Line1<br>Line2");
    label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    label.setVerticalAlignment(javax.swing.SwingConstants.TOP);
    Dimension d = label.getPreferredSize();
    label.setBounds(0, 0, imgtext.getWidth(), d.height);

    // The background
    Color c1 = new Color(0xff, 0xff, 0xff, 0x40);
    Color c2 = new Color(0xff, 0xff, 0xff, 0xd0);

    //        Point2D center = new Point2D.Float(imgtext.getWidth() / 2, label.getHeight());
    //        float radius = imgtext.getWidth() / 3;
    //        float[] dist = {0.1f, 1.0f};
    //        Color[] colors = {c2, c1};        
    //        Paint gpaint = new RadialGradientPaint(center, radius, dist, colors);
    Paint gpaint = new GradientPaint(new Point(0, 0), c1, new Point(label.getWidth() / 2, 0), c2, true);

    g2d.drawImage(img, 0, 0, null);
    int ypos = imgtext.getHeight() - label.getHeight();
    int ypos_min = -4; // todo: configurable
    if (ypos < ypos_min)
        ypos = ypos_min; // Clamp label
    g2d.translate(0, ypos);
    g2d.setPaint(gpaint);
    g2d.fillRect(0, 0, imgtext.getWidth(), label.getHeight());
    label.paint(g2d);

    g2d.dispose();

    return imgtext;
}

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

public void hideAttractor(Graphics2D g2, BufferedImage image, int imageX, int imageY) {
    this.disableAllActionsExceptStop();
    plotCopiedDisplay();/*from w  ww.j  av a2 s  .co  m*/
    Point p = new Point();
    int index = 0;
    for (int i = 0; i < gridWidth; i++) {
        for (int j = 0; j < gridHeight; j++) {
            p.set(i, j);
            if (getGridState(p) == attractorColor)
                setGridState(p, 0);
        }
        index++;
        if (index == 1000) {
            index = 0;
            g2.drawImage(image, null, imageX, imageY);
        }
        if (stopped)
            break;
    }
    g2.drawImage(image, null, imageX, imageY);
    copyDisplay();
    stopped = false;
    enableAllActionsExceptStop();
}

From source file:org.broad.igv.hic.MainWindow.java

public void createCursors() {
    BufferedImage handImage = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);

    // Make backgroun transparent
    Graphics2D g = handImage.createGraphics();
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
    Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, 32, 32);
    g.fill(rect);/*from w  ww .ja  v a2s  . c o  m*/

    // Draw hand image in middle
    g = handImage.createGraphics();
    g.drawImage(IconFactory.getInstance().getIcon(IconFactory.IconID.FIST).getImage(), 0, 0, null);
    MainWindow.fistCursor = getToolkit().createCustomCursor(handImage, new Point(8, 6), "Move");
}

From source file:net.sf.maltcms.chromaui.charts.FastHeatMapPlot.java

private void createImage(Graphics2D g2, Rectangle2D dataArea, PlotRenderingInfo info,
        CrosshairState crosshairState) {
    //System.out.println("Creating image! - new");
    BufferedImage bi = createCompatibleImage(this.width, this.height, BufferedImage.TRANSLUCENT);
    float alpha = getDatasetCount() == 1 ? 1.0f : 1.0f / (float) getDatasetCount();
    for (int i = 0; i < getDatasetCount(); i++) {
        XYBlockRenderer xybr = (XYBlockRenderer) getRenderer(i);
        //            System.out.println("alpha in plot " + ((GradientPaintScale) xybr.getPaintScale()).getAlpha());
        //            System.out.println("beta in plot " + ((GradientPaintScale) xybr.getPaintScale()).getBeta());
        //((GradientPaintScale) xybr.getPaintScale()).setAlphaBeta(0, 1);
        //            System.out.println("ramp in plot " + Arrays.deepToString(((GradientPaintScale) xybr.getPaintScale()).getRamp()));
        XYZDataset xyzd = (XYZDataset) getDataset(i);
        BufferedImage bi2 = prepareData(xyzd, this.width, this.height, xybr, g2, dataArea, info, null);
        Graphics2D gg2 = bi.createGraphics();
        gg2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        gg2.drawImage(bi2, 0, 0, null);
    }/*  w  w w  .  j a v a  2  s .c o  m*/
    setDataImage(bi, new Range(0, this.width - 1), new Range(0, this.height - 1));
}

From source file:net.sf.maltcms.chromaui.charts.FastHeatMapPlot.java

private void drawOffscreenImage(Image sourceImage) {
    // image creation
    if (offscreenBuffer == null) {
        offscreenBuffer = createCompatibleVolatileImage(sourceImage.getWidth(null), sourceImage.getHeight(null),
                Transparency.TRANSLUCENT);
    }/*from ww  w .j av  a2s.  c  om*/
    do {
        if (offscreenBuffer.validate(getGraphicsConfiguration()) == VolatileImage.IMAGE_INCOMPATIBLE) {
            // old vImg doesn't work with new GraphicsConfig; re-create it
            offscreenBuffer = createCompatibleVolatileImage(sourceImage.getWidth(null),
                    sourceImage.getHeight(null), Transparency.TRANSLUCENT);
        }
        Graphics2D g = offscreenBuffer.createGraphics();
        //
        // miscellaneous rendering commands...
        //
        g.drawImage(sourceImage, 0, 0, null);
        g.dispose();
    } while (offscreenBuffer.contentsLost());
}

From source file:net.sf.mzmine.modules.visualization.twod.TwoDXYPlot.java

public boolean render(final Graphics2D g2, final Rectangle2D dataArea, int index, PlotRenderingInfo info,
        CrosshairState crosshairState) {

    // if this is not TwoDDataSet
    if (index != 0)
        return super.render(g2, dataArea, index, info, crosshairState);

    // prepare some necessary constants
    final int x = (int) dataArea.getX();
    final int y = (int) dataArea.getY();
    final int width = (int) dataArea.getWidth();
    final int height = (int) dataArea.getHeight();

    final double imageRTMin = (double) getDomainAxis().getRange().getLowerBound();
    final double imageRTMax = (double) getDomainAxis().getRange().getUpperBound();
    final double imageRTStep = (imageRTMax - imageRTMin) / width;
    final double imageMZMin = (double) getRangeAxis().getRange().getLowerBound();
    final double imageMZMax = (double) getRangeAxis().getRange().getUpperBound();
    final double imageMZStep = (imageMZMax - imageMZMin) / height;

    if ((zoomOutBitmap != null) && (imageRTMin == totalRTRange.getMin())
            && (imageRTMax == totalRTRange.getMax()) && (imageMZMin == totalMZRange.getMin())
            && (imageMZMax == totalMZRange.getMax()) && (zoomOutBitmap.getWidth() == width)
            && (zoomOutBitmap.getHeight() == height)) {
        g2.drawImage(zoomOutBitmap, x, y, null);
        return true;
    }/* w w w. j  a v  a2s .c  o  m*/

    // Save current time
    Date renderStartTime = new Date();

    // prepare a double array of summed intensities
    double values[][] = new double[width][height];
    maxValue = 0; // now this is an instance variable
    Random r = new Random();

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {

            double pointRTMin = imageRTMin + (i * imageRTStep);
            double pointRTMax = pointRTMin + imageRTStep;
            double pointMZMin = imageMZMin + (j * imageMZStep);
            double pointMZMax = pointMZMin + imageMZStep;

            double lv = dataset.getMaxIntensity(new Range(pointRTMin, pointRTMax),
                    new Range(pointMZMin, pointMZMax), plotMode);

            if (logScale) {
                lv = Math.log10(lv);
                if (lv < 0 || Double.isInfinite(lv))
                    lv = 0;
                values[i][j] = lv;
                //values[r.nextInt(width)][r.nextInt(height)] = lv;
            } else {
                values[i][j] = lv;
            }

            if (lv > maxValue)
                maxValue = lv;

        }

    // This should never happen, but just for correctness
    if (maxValue == 0)
        return false;

    // Normalize all values
    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            values[i][j] /= maxValue;
        }

    // prepare a bitmap of required size
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    // draw image points
    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            Color pointColor = paletteType.getColor(values[i][j]);
            image.setRGB(i, height - j - 1, pointColor.getRGB());
        }

    // if we are zoomed out, save the values
    if ((imageRTMin == totalRTRange.getMin()) && (imageRTMax == totalRTRange.getMax())
            && (imageMZMin == totalMZRange.getMin()) && (imageMZMax == totalMZRange.getMax())) {
        zoomOutBitmap = image;
    }

    // Paint image
    g2.drawImage(image, x, y, null);

    Date renderFinishTime = new Date();

    logger.finest("Finished rendering 2D visualizer, "
            + (renderFinishTime.getTime() - renderStartTime.getTime()) + " ms");

    return true;

}

From source file:org.fhcrc.cpl.viewer.quant.gui.QuantitationVisualizer.java

/**
 * Save chart to an image file, with or without the sidebar information and/or theoretical peaks
 * @param chartPanel/*w w w  .  ja  v a  2s .  co  m*/
 * @param outFile
 * @param sidebarWidth
 * @param charge
 * @param lightMz
 * @param heavyMz
 * @param ratio
 * @throws IOException
 */
public void saveChartToImageFile(PanelWithChart chartPanel, File outFile, int sidebarWidth, int charge,
        float lightMz, float heavyMz, float ratio, boolean writeChartInfo, int width, int height,
        boolean overrideSize) throws IOException {
    BufferedImage spectrumImage = null;
    if (overrideSize)
        spectrumImage = chartPanel.createImage(width, height);
    else
        spectrumImage = chartPanel.createImage();
    BufferedImage imageToWrite = spectrumImage;

    if (writeChartInfo) {
        int fullImageWidth = spectrumImage.getWidth() + sidebarWidth;

        imageToWrite = new BufferedImage(fullImageWidth, spectrumImage.getHeight(), BufferedImage.TYPE_INT_RGB);

        Graphics2D g = imageToWrite.createGraphics();
        g.drawImage(spectrumImage, sidebarWidth, 0, null);

        //write in sidebar
        int lineHeight = 20;
        int lineNum = 1;
        int indent = 5;

        if (writeChartInfo) {
            g.setPaint(Color.WHITE);
            //                g.drawString(peptide, indent, lineNum++ * lineHeight);
            //                g.drawString("Charge=" + charge, indent, lineNum++ * lineHeight);
            //                g.drawString("Light mass=" + lightMass, indent, lineNum++ * lineHeight);
            //                g.drawString("Light m/z=" + lightMz, indent, lineNum++ * lineHeight);
            //                g.drawString("Heavy m/z=" + heavyMz, indent, lineNum++ * lineHeight);
            //                g.drawString("Light int=" + lightIntensity, indent, lineNum++ * lineHeight);
            //                g.drawString("Heavy int=" + heavyIntensity, indent, lineNum++ * lineHeight);
            g.drawString("Ratio=" + ratio, indent, lineNum++ * lineHeight);

            //                g.drawString("MinscanLt=" + lightMinQuantScan, indent, lineNum++ * lineHeight);
            //                g.drawString("MaxscanLt=" + lightMaxQuantScan, indent, lineNum++ * lineHeight);
            //                g.drawString("MinScanHv=" + heavyMinQuantScan, indent, lineNum++ * lineHeight);
            //                g.drawString("MaxScanHv=" + heavyMaxQuantScan, indent, lineNum++ * lineHeight);
            //                g.drawString("ID scan=" + idScan, indent, lineNum++ * lineHeight);
            //                g.drawString("IDscan level=" + idScanLevel, indent, lineNum++ * lineHeight);

            //theoretical peaks in bottom left
            int theoreticalPeaksHeight = (int) (sidebarWidth * 2.0 / 3.0);
            int theoreticalPeaksTop = spectrumImage.getHeight() - theoreticalPeaksHeight - 10;
            g.drawString("Ideal Peaks", indent, theoreticalPeaksTop - 20);
            //combined theoretical peak distribution chart
            PanelWithPeakChart theoreticalPeakChart = buildTheoreticalPeakChart(lightMz, heavyMz, charge, ratio,
                    sidebarWidth, (int) (sidebarWidth * 2.0 / 3.0));
            g.drawImage(theoreticalPeakChart.createImage(sidebarWidth, (int) (sidebarWidth * 2.0 / 3.0)), 0,
                    theoreticalPeaksTop, null);
        }
        g.dispose();
    }
    ImageIO.write(imageToWrite, "png", outFile);
}

From source file:de.fhg.igd.mapviewer.AbstractTileOverlayPainter.java

/**
 * @see TileOverlayPainter#paintTile(Graphics2D, int, int, int, int, int,
 *      int, int, PixelConverter, Rectangle)
 *///ww w  .j a v a  2 s  . c  o  m
@Override
public void paintTile(final Graphics2D gfx, final int x, final int y, final int zoom, final int tilePosX,
        final int tilePosY, final int tileWidth, final int tileHeight, final PixelConverter converter,
        Rectangle viewportBounds) {
    BufferedImage img;
    synchronized (this) {
        img = getCachedTile(x, y, zoom);

        if (img == null) {
            // start tile creation

            // temporarily set empty image TODO load image?
            img = loadingImage;
            cacheTile(x, y, zoom, img);

            TilePaintDelegate paint = new TilePaintDelegate(x, y, tilePosX, tilePosY, tileWidth, tileHeight,
                    converter, zoom);
            scheduleTileRepaint(paint);
        }
    }

    if (img == loadingImage) {
        configureGraphics(gfx);
        gfx.drawImage(img, (tileWidth - img.getWidth()) / 2, (tileHeight - img.getHeight()) / 2, null);
    } else if (img != null && img != emptyImage) {
        configureGraphics(gfx);
        drawOverlay(gfx, img, zoom, tilePosX, tilePosY, tileWidth, tileHeight, viewportBounds, converter);
    }
}