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

public void init() {
    setBackground(Color.white);// ww  w . ja v a2s .c o  m

    bi = new BufferedImage[4];
    String s[] = { "bld.jpg", "bld.jpg", "boat.gif", "boat.gif" };
    for (int i = 0; i < bi.length; i++) {
        Image img = getImage(getURL("images/" + s[i]));
        try {
            MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(img, 0);
            tracker.waitForID(0);
        } catch (Exception e) {
        }
        int iw = img.getWidth(this);
        int ih = img.getHeight(this);
        bi[i] = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
        Graphics2D big = bi[i].createGraphics();
        big.drawImage(img, 0, 0, this);
    }
}

From source file:ch.fork.AdHocRailway.ui.utils.ImageTools.java

private BufferedImage ImageToBufferedImage(Image image, int width, int height) {
    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = dest.createGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();/*ww  w.j ava 2 s  . co m*/
    return dest;
}

From source file:CombineApp.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2D = (Graphics2D) g;

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

From source file:org.codice.imaging.nitf.render.RenderTestSupport.java

private BufferedImage convert2ARGB(ImageSegment imageSegment, BufferedImage bufferedImage) {
    BufferedImage imgAGRB = new BufferedImage(
            imageSegment.getImageLocationColumn() + (int) imageSegment.getNumberOfColumns(),
            imageSegment.getImageLocationRow() + (int) imageSegment.getNumberOfRows(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D targetGraphic = imgAGRB.createGraphics();
    targetGraphic.drawImage(bufferedImage, 0, 0, null);
    return imgAGRB;
}

From source file:ColorApp.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2D = (Graphics2D) g;
    g2D.drawImage(bi, 0, 0, this);
}

From source file:com.igormaznitsa.jhexed.values.HexSVGImageValue.java

@Override
public void prerasterizeIcon(final Shape shape) {
    try {//  w w  w . j  a  v  a2 s. co  m
        final Rectangle r = shape.getBounds();
        final BufferedImage img = this.image.rasterize(r.width, r.height, BufferedImage.TYPE_INT_ARGB);
        final BufferedImage result = new BufferedImage(r.width, r.height, BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g = result.createGraphics();
        g.setClip(shape);
        g.drawImage(img, 0, 0, null);
        g.dispose();
        this.prerasterized = result;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    if (myImage != null) {
        int x = (getWidth() - myImage.getWidth()) / 2;
        int y = (getHeight() - myImage.getHeight()) / 2;
        g2d.drawImage(myImage, x, y, this);

        g2d.setColor(Color.RED);/*from  w w  w  .  j a  v a2  s. c  o  m*/
        g2d.translate(x, y);
        g2d.draw(myOffice);
    }
    g2d.dispose();
}

From source file:org.deegree.tile.persistence.geotiff.GeoTIFFTile.java

@Override
public BufferedImage getAsImage() throws TileIOException {
    ImageReader reader = null;/*from   w  w w.j a va  2s.co m*/
    try {
        reader = (ImageReader) readerPool.borrowObject();
        BufferedImage img = reader.readTile(imageIndex, x, y);
        if (img.getWidth() != sizeX || img.getHeight() != sizeY) {
            Hashtable<Object, Object> table = new Hashtable<Object, Object>();
            String[] props = img.getPropertyNames();
            if (props != null) {
                for (String p : props) {
                    table.put(p, img.getProperty(p));
                }
            }
            BufferedImage img2 = new BufferedImage(img.getColorModel(),
                    img.getData().createCompatibleWritableRaster(sizeX, sizeY), img.isAlphaPremultiplied(),
                    table);
            Graphics2D g = img2.createGraphics();
            g.drawImage(img, 0, 0, null);
            g.dispose();
            img = img2;
        }
        return img;
    } catch (Exception e) {
        throw new TileIOException("Error retrieving image: " + e.getMessage(), e);
    } finally {
        try {
            readerPool.returnObject(reader);
        } catch (Exception e) {
            // ignore closing error
        }
    }
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    int xPos = (getWidth() - bg.getWidth()) / 2;
    int yPos = yOffset;

    yPos = yOffset;/*from  ww  w  .j av a2  s .com*/
    while (yPos < getHeight()) {
        g2d.drawImage(bg, xPos, yPos, this);
        yPos += bg.getHeight();
    }

    g2d.dispose();
}

From source file:ScalingMethods.java

/**
 * Draws the picture five times, using the five different scaling
 * approaches described in the book. All five look the same, since
 * all are using default (nearest neighbor) filtering during the
 * scale operation.// ww  w  . j a  va  2s.  c  o  m
 */
public void paintComponent(Graphics g) {
    int x = PADDING;
    int y = PADDING;

    // Simplest approach
    g.drawImage(picture, x, y, scaleW, scaleH, null);

    // Subregion approach
    x += scaleW + PADDING;
    g.drawImage(picture, x, y, x + scaleW, y + scaleH, 0, 0, picture.getWidth(), picture.getHeight(), null);

    // Graphics2D.scale approach
    x += scaleW + PADDING;
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.translate(x, y);
    g2d.scale(SCALE_FACTOR, SCALE_FACTOR);
    g2d.drawImage(picture, 0, 0, null);
    g2d.dispose();

    // AffineTransform.scale approach
    x += scaleW + PADDING;
    g2d = (Graphics2D) g.create();
    AffineTransform at = new AffineTransform();
    at.translate(x, y);
    at.scale(SCALE_FACTOR, SCALE_FACTOR);
    g2d.drawImage(picture, at, null);
    g2d.dispose();

    // getScaledInstance() approach
    x += scaleW + PADDING;
    Image scaledImg = picture.getScaledInstance(scaleW, scaleH, Image.SCALE_DEFAULT);
    g.drawImage(scaledImg, x, y, null);
}