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

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    g2d.drawImage(myImage, 10, 10, null);
}

From source file:RasterDemo.java

RasterPanel() {
    setBackground(Color.white);//from ww  w.  j av  a 2s  .co m
    setSize(450, 400);

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

    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 jpg file");
        System.exit(0);
    }

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

From source file:weka.core.ChartUtils.java

/**
 * Render a combined histogram and box plot chart from summary data
 * //from   w w w  .jav  a2 s .  c o m
 * @param width the width of the resulting image
 * @param height the height of the resulting image
 * @param bins the values for the chart
 * @param freqs the corresponding frequencies
 * @param summary the summary stats for the box plot
 * @param outliers an optional list of outliers for the box plot
 * @param additionalArgs optional arguments to the renderer (may be null)
 * @return a buffered image
 * @throws Exception if a problem occurs
 */
public static BufferedImage renderCombinedBoxPlotAndHistogramFromSummaryData(int width, int height,
        List<String> bins, List<Double> freqs, List<Double> summary, List<Double> outliers,
        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=histogram");
    BufferedImage hist = renderHistogramFromSummaryData(width / 2, height, bins, freqs, opts);
    opts.clear();
    opts.add("-title=box plot");
    BufferedImage box = null;
    try {
        box = renderBoxPlotFromSummaryData(width / 2, height, summary, outliers, opts);
    } catch (Exception ex) {
        // if we can't generate the box plot (i.e. probably because
        // data is 100% missing) then just return the histogram
    }

    if (box == null) {
        width /= 2;
    }
    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);

    if (box != null) {
        g2d.drawImage(box, 0, 20, null);
        g2d.drawImage(hist, width / 2 + 1, 20, null);
    } else {
        g2d.drawImage(hist, 0, 20, null);
    }
    g2d.drawString(plotTitle, width / 2 - sw / 2, fh + 2);
    g2d.dispose();

    return img;
}

From source file:ImageComposite.java

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    BufferedImage buffImg = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB);
    Graphics2D gbi = buffImg.createGraphics();

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);

    gbi.drawImage(a, 40, 30, null);
    gbi.setComposite(ac);//from  w w  w.  java 2  s. co m
    gbi.drawImage(b, 0, 0, null);

    g2d.drawImage(buffImg, 20, 20, null);
}

From source file:RotateImage45Degrees.java

public RotateImage45Degrees(String imageFile) {
    addNotify();/*from ww  w .  j av  a 2s .c o m*/
    frameInsets = getInsets();
    inputImage = Toolkit.getDefaultToolkit().getImage(imageFile);

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(inputImage, 0);
    try {
        mt.waitForID(0);
    } catch (InterruptedException ie) {
    }

    sourceBI = new BufferedImage(inputImage.getWidth(null), inputImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = (Graphics2D) sourceBI.getGraphics();
    g.drawImage(inputImage, 0, 0, null);

    AffineTransform at = new AffineTransform();

    // scale image
    at.scale(2.0, 2.0);

    // rotate 45 degrees around image center
    at.rotate(45.0 * Math.PI / 180.0, sourceBI.getWidth() / 2.0, sourceBI.getHeight() / 2.0);

    /*
     * translate to make sure the rotation doesn't cut off any image data
     */
    AffineTransform translationTransform;
    translationTransform = findTranslation(at, sourceBI);
    at.preConcatenate(translationTransform);

    // instantiate and apply affine transformation filter
    BufferedImageOp bio;
    bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);

    destinationBI = bio.filter(sourceBI, null);

    int frameInsetsHorizontal = frameInsets.right + frameInsets.left;
    int frameInsetsVertical = frameInsets.top + frameInsets.bottom;
    setSize(destinationBI.getWidth() + frameInsetsHorizontal, destinationBI.getHeight() + frameInsetsVertical);
    show();
}

From source file:net.pkhsolutions.pecsapp.control.PictureTransformer.java

/**
 * TODO Document me//  w  w  w.j  a v a 2 s.  co m
 *
 * @param image
 * @param maxHeightInPx
 * @param maxWidthInPx
 * @return
 */
@NotNull
public BufferedImage scaleIfNecessary(@NotNull BufferedImage image, int maxHeightInPx, int maxWidthInPx) {
    int w = image.getWidth();
    int h = image.getHeight();

    int newW;
    int newH;

    if (w <= maxWidthInPx && h <= maxHeightInPx) {
        return image;
    } else if (w > h) {
        newW = maxWidthInPx;
        newH = newW * h / w;
    } else {
        newH = maxHeightInPx;
        newW = newH * w / h;
    }
    LOGGER.info("Original size was w{} x h{}, new size is w{} x h{}", w, h, newW, newH);
    Image tmp = image.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
    BufferedImage img = new BufferedImage(newW, newH, image.getType());
    Graphics2D g2d = img.createGraphics();
    g2d.drawImage(tmp, 0, 0, null);
    g2d.dispose();
    return img;
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Shape shape = new java.awt.geom.Ellipse2D.Float(20, 20, 200, 200);

    g2d.setClip(shape);//ww w .  j  av a  2  s .  co m

    int x = 0;
    int y = 0;
    g2d.drawImage(new ImageIcon("a.png").getImage(), x, y, this);
}

From source file:ImageOps.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    // Create a BufferedImage big enough to hold the Image loaded
    // in the constructor. Then copy that image into the new
    // BufferedImage object so that we can process it.
    BufferedImage bimage = new BufferedImage(image.getWidth(this), image.getHeight(this),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D ig = bimage.createGraphics();
    ig.drawImage(image, 0, 0, this); // copy the image

    // Set some default graphics attributes
    g.setFont(new Font("SansSerif", Font.BOLD, 12)); // 12pt bold text
    g.setColor(Color.green); // Draw in green
    g.translate(10, 10); // Set some margins

    // Loop through the filters
    for (int i = 0; i < filters.length; i++) {
        // If the filter is null, draw the original image, otherwise,
        // draw the image as processed by the filter
        if (filters[i] == null)
            g.drawImage(bimage, 0, 0, this);
        else//from   ww  w  .j a  v a  2 s. co  m
            g.drawImage(filters[i].filter(bimage, null), 0, 0, this);
        g.drawString(filterNames[i], 0, 205); // Label the image
        g.translate(137, 0); // Move over
        if (i % 4 == 3)
            g.translate(-137 * 4, 215); // Move down after 4
    }
}

From source file:com.wet.wired.jsr.recorder.DesktopScreenRecorder.java

public BufferedImage captureScreen(Rectangle recordArea) {
    Point mousePosition = MouseInfo.getPointerInfo().getLocation();
    BufferedImage image = robot.createScreenCapture(recordArea);

    Graphics2D grfx = image.createGraphics();

    grfx.drawImage(mouseCursor, mousePosition.x - 8, mousePosition.y - 5, null);

    grfx.dispose();//from  w ww .j a v a  2s  .  co  m

    return image;
}

From source file:Java2DExample.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D graphics = (Graphics2D) g;
    graphics.drawImage(displayImage, 0, 0, null);
}