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:BufferedImageMouseDrag.java

DisplayCanvas() {
    setBackground(Color.white);//  w  w w  .j a va  2s  .c o  m
    setSize(450, 400);
    addMouseMotionListener(new MouseMotionHandler());

    Image image = getToolkit().getImage("largeJava2sLogo.gif");

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 1);
    try {
        mt.waitForAll();
    } catch (Exception e) {
        System.out.println("Exception while loading image.");
    }

    if (image.getWidth(this) == -1) {
        System.out.println("no gif file");
        System.exit(0);
    }

    bi = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bi.createGraphics();
    big.drawImage(image, 0, 0, this);
}

From source file:weka.core.ChartUtils.java

/**
 * Render a combined histogram and pie chart from summary data
 * /*from  w ww  .j  av a2 s  . com*/
 * @param width the width of the resulting image
 * @param height the height of the resulting image
 * @param values the values for the chart
 * @param freqs the corresponding frequencies
 * @param additionalArgs optional arguments to the renderer (may be null)
 * @return a buffered image
 * @throws Exception if a problem occurs
 */
public static BufferedImage renderCombinedPieAndHistogramFromSummaryData(int width, int height,
        List<String> values, List<Double> freqs, List<String> additionalArgs) throws Exception {

    String plotTitle = "Combined Chart";
    String userTitle = getOption(additionalArgs, "-title");
    plotTitle = (userTitle != null) ? userTitle : plotTitle;

    List<String> opts = new ArrayList<String>();
    opts.add("-title=distribution");
    BufferedImage pie = renderPieChartFromSummaryData(width / 2, height, values, freqs, false, true, opts);

    opts.clear();
    opts.add("-title=histogram");
    BufferedImage hist = renderHistogramFromSummaryData(width / 2, height, values, freqs, opts);

    BufferedImage img = new BufferedImage(width, height + 20, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
    g2d.setFont(new Font("SansSerif", Font.BOLD, 12));
    g2d.setColor(Color.lightGray);
    g2d.fillRect(0, 0, width, height + 20);
    g2d.setColor(Color.black);
    FontMetrics fm = g2d.getFontMetrics();
    int fh = fm.getHeight();
    int sw = fm.stringWidth(plotTitle);

    g2d.drawImage(pie, 0, 20, null);
    g2d.drawImage(hist, width / 2 + 1, 20, null);
    g2d.drawString(plotTitle, width / 2 - sw / 2, fh + 2);
    g2d.dispose();

    return img;
}

From source file:Clipping.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setClip(new Ellipse2D.Double(x, y, radius, radius));
    g2d.drawImage(image, 5, 5, null);
}

From source file:de.mfo.jsurf.grid.RotationGrid.java

public static BufferedImage renderAnimGrid(int xAngleMin, int xAngleMax, int xSteps, int yAngleMin,
        int yAngleMax, int ySteps) {
    BufferedImage grid = new BufferedImage(ySteps * size, xSteps * size, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) grid.getGraphics();
    for (int x = 0; x < xSteps; ++x) {
        double xAngle = xAngleMin + (xAngleMax - xAngleMin) * (xSteps == 1 ? 0.5 : (x / (double) (xSteps - 1)));
        Matrix4d matRotX = new Matrix4d();
        matRotX.setIdentity();//  www . ja  va2  s. c  om
        matRotX.rotX(Math.toRadians(xAngle));
        for (int y = 0; y < ySteps; ++y) {
            double yAngle = yAngleMin
                    + (yAngleMax - yAngleMin) * (ySteps == 1 ? 0.5 : (y / (double) (ySteps - 1)));
            Matrix4d matRotY = new Matrix4d();
            matRotY.setIdentity();
            matRotY.rotY(Math.toRadians(yAngle));
            additional_rotation.mul(matRotY, matRotX);
            BufferedImage bi = createBufferedImageFromRGB(draw(size, size, aam, aap));
            g2.drawImage(bi,
                    new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
                    (ySteps - 1 - y) * size, x * size);
        }
    }
    return grid;
}

From source file:ImageDuplicity.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    if (image == null)
        createOffscreenImage();/*from  w  w  w . j  av  a2s  . com*/
    g2.drawImage(image, 0, 0, this);
}

From source file:com.lingxiang2014.util.ImageUtils.java

public static void addWatermark(File srcFile, File destFile, File watermarkFile, int alpha) {
    Assert.notNull(srcFile);//  w  ww .  j ava2 s  . c  o  m
    Assert.notNull(destFile);
    Assert.state(alpha >= 0);
    Assert.state(alpha <= 100);
    if (watermarkFile == null || !watermarkFile.exists()) {
        try {
            FileUtils.copyFile(srcFile, destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }
    if (type == Type.jdk) {
        Graphics2D graphics2D = null;
        ImageOutputStream imageOutputStream = null;
        ImageWriter imageWriter = null;
        try {
            BufferedImage srcBufferedImage = ImageIO.read(srcFile);
            int srcWidth = srcBufferedImage.getWidth();
            int srcHeight = srcBufferedImage.getHeight();
            BufferedImage destBufferedImage = new BufferedImage(srcWidth, srcHeight,
                    BufferedImage.TYPE_INT_RGB);
            graphics2D = destBufferedImage.createGraphics();
            graphics2D.setBackground(BACKGROUND_COLOR);
            graphics2D.clearRect(0, 0, srcWidth, srcHeight);
            graphics2D.drawImage(srcBufferedImage, 0, 0, null);
            graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha / 100F));

            BufferedImage watermarkBufferedImage = ImageIO.read(watermarkFile);
            int watermarkImageWidth = watermarkBufferedImage.getWidth();
            int watermarkImageHeight = watermarkBufferedImage.getHeight();
            int x = srcWidth - watermarkImageWidth;
            int y = srcHeight - watermarkImageHeight;

            graphics2D.drawImage(watermarkBufferedImage, x, y, watermarkImageWidth, watermarkImageHeight, null);

            imageOutputStream = ImageIO.createImageOutputStream(destFile);
            imageWriter = ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName()))
                    .next();
            imageWriter.setOutput(imageOutputStream);
            ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
            imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            imageWriteParam.setCompressionQuality(DEST_QUALITY / 100F);
            imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam);
            imageOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (graphics2D != null) {
                graphics2D.dispose();
            }
            if (imageWriter != null) {
                imageWriter.dispose();
            }
            if (imageOutputStream != null) {
                try {
                    imageOutputStream.close();
                } catch (IOException e) {
                }
            }
        }
    } else {
        IMOperation operation = new IMOperation();
        operation.dissolve(alpha);
        operation.quality((double) DEST_QUALITY);
        operation.addImage(watermarkFile.getPath());
        operation.addImage(srcFile.getPath());
        operation.addImage(destFile.getPath());
        if (type == Type.graphicsMagick) {
            CompositeCmd compositeCmd = new CompositeCmd(true);
            if (graphicsMagickPath != null) {
                compositeCmd.setSearchPath(graphicsMagickPath);
            }
            try {
                compositeCmd.run(operation);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IM4JavaException e) {
                e.printStackTrace();
            }
        } else {
            CompositeCmd compositeCmd = new CompositeCmd(false);
            if (imageMagickPath != null) {
                compositeCmd.setSearchPath(imageMagickPath);
            }
            try {
                compositeCmd.run(operation);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IM4JavaException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:ImagePanel.java

public void paintComponent(Graphics grp) {
    Graphics2D g2D = (Graphics2D) grp;
    g2D.scale(zoom, zoom);/*from w ww .  j  av a  2  s.c o m*/
    g2D.drawImage(image, 0, 0, this);
}

From source file:RasterDemo.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2D = (Graphics2D) g;

    g2D.drawImage(bi, 0, 0, this);
}

From source file:com.liusoft.dlog4j.util.ImageUtils.java

/**
 * ???/*from w  w w .  j  a v a  2s.  c  om*/
 * ?????
 * 3: 180
 * 6: 90
 * 8: 27090
 * @param img_fn
 * @param orient
 * @throws IOException 
 */
public static boolean rotateImage(String img_fn, int orient, String dest_fn) throws IOException {
    double radian = 0;
    switch (orient) {
    case 3:
        radian = 180.0;
        break;
    case 6:
        radian = 90.0;
        break;
    case 8:
        radian = 270.0;
        break;
    default:
        return false;
    }
    BufferedImage old_img = (BufferedImage) ImageIO.read(new File(img_fn));
    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 xRot = 0;
    double yRot = 0;
    switch (orient) {
    case 3:
        xRot = width / 2.0;
        yRot = height / 2.0;
    case 6:
        xRot = height / 2.0;
        yRot = xRot;
        break;
    case 8:
        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);

    FileOutputStream out = new FileOutputStream(dest_fn);
    try {
        ImageIO.write(new_img, "JPG", out);
    } finally {
        out.close();
    }
    return true;
}

From source file:Myopia.java

@Override
public void paint(Graphics g, JComponent c) {
    int w = c.getWidth();
    int h = c.getHeight();

    if (w == 0 || h == 0) {
        return;/*from  w w w. ja  v a  2  s  . c  o  m*/
    }

    // Only create the offscreen image if the one we have
    // is the wrong size.
    if (mOffscreenImage == null || mOffscreenImage.getWidth() != w || mOffscreenImage.getHeight() != h) {
        mOffscreenImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    }

    Graphics2D ig2 = mOffscreenImage.createGraphics();
    ig2.setClip(g.getClip());
    super.paint(ig2, c);
    ig2.dispose();

    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(mOffscreenImage, mOperation, 0, 0);
}