Example usage for java.awt Image getHeight

List of usage examples for java.awt Image getHeight

Introduction

In this page you can find the example usage for java.awt Image getHeight.

Prototype

public abstract int getHeight(ImageObserver observer);

Source Link

Document

Determines the height of the image.

Usage

From source file:Util.java

/**
 * Converts a java.awt.Image into an array of pixels
 *//*  www .j  a v a  2 s  .  c o m*/
public static int[] convertToPixels(Image img) {
    int width = img.getWidth(null);
    int height = img.getHeight(null);
    int[] pixel = new int[width * height];

    PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixel, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        throw new IllegalStateException("Error: Interrupted Waiting for Pixels");
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        throw new IllegalStateException("Error: Image Fetch Aborted");
    }
    return pixel;
}

From source file:ImageUtil.java

/**
 * Creates and returns a buffered version of the specified image.
 *
 * @param image the image to create a buffered image for
 * @return a buffered image based on the specified image
 *///from  w ww  .  j  a  va  2 s.  com
public static BufferedImage getBufferedImage(Image image) {
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);

    Graphics g = bufferedImage.getGraphics();

    g.drawImage(image, 0, 0, null);

    return bufferedImage;
}

From source file:Main.java

public static Image merge(Image left, Image right) {
    BufferedImage merged = new BufferedImage(left.getWidth(null) + right.getWidth(null), left.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) merged.getGraphics();
    g.drawImage(left, 0, 0, null);/*from ww w .  j  a v  a  2  s  . c o m*/
    g.drawImage(right, left.getWidth(null), 0, null);
    return merged;
}

From source file:com.taunova.app.libview.components.ImageHelpers.java

public static Dimension getScaledDimension(Image image, int width) {
    double k = ((double) image.getHeight(null)) / image.getWidth(null);
    int height = (int) (width * k);

    return new Dimension(width, height);
}

From source file:Main.java

public static BufferedImage highlightRegions(Image img, int[][] regions, int regionId, Color fgColour) {
    BufferedImage canvas = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = canvas.createGraphics();
    g2d.drawImage(img, 0, 0, null);/*from w  w w .  java2 s.  c  om*/
    g2d.setColor(fgColour);
    for (int y = 0; y < regions.length; y++) {
        for (int x = 0; x < regions[y].length; x++) {
            if (regions[y][x] == regionId) {
                g2d.drawRect(x, y, 1, 1);
            }
        }
    }

    return canvas;
}

From source file:Main.java

public static Cursor buildCursorByTrimming(Toolkit toolkit, Image image, int x, int y, String name,
        Cursor defaultCursor) {/*from   www  . j  a va2 s . co  m*/
    Dimension d = toolkit.getBestCursorSize(image.getWidth(null), image.getHeight(null));
    if (d == null || d.getWidth() <= 0 || d.getHeight() <= 0)
        return defaultCursor;
    BufferedImage out = new BufferedImage((int) d.getWidth(), (int) d.getHeight(), BufferedImage.TYPE_INT_ARGB);
    out.getGraphics().drawImage(image, 0, 0, null);
    return toolkit.createCustomCursor(out, new Point(x, y), name);
}

From source file:Main.java

public static BufferedImage asCompatibleImage(Image img) {
    BufferedImage ret = defaultScreenDeviceConfiguration().createCompatibleImage(img.getWidth(null),
            img.getHeight(null));
    Graphics2D gc = ret.createGraphics();
    gc.drawImage(img, 0, 0, null);//ww  w  .java  2 s  .  c  om
    gc.dispose();
    return ret;
}

From source file:Main.java

public static BufferedImage asCompatibleImage(Image img, int transparency) {
    BufferedImage ret = defaultScreenDeviceConfiguration().createCompatibleImage(img.getWidth(null),
            img.getHeight(null), transparency);
    Graphics2D gc = ret.createGraphics();
    gc.setComposite(AlphaComposite.Src);
    gc.drawImage(img, 0, 0, null);/* w ww. j ava2s .  co m*/
    gc.dispose();
    return ret;
}

From source file:Main.java

/**
 * Draw the image with 9 grids scaling/*from  w  ww  .  j  ava 2 s.c  o  m*/
 * 
 * @param g
 * @param img
 * @param dx1
 * @param dy1
 * @param dx2
 * @param dy2
 * @param insets
 * @param paintCenter
 */
public static void drawImageWith9Grids(Graphics g, Image img, int dx1, int dy1, int dx2, int dy2, Insets insets,
        boolean paintCenter) {

    final int imgWidth = img.getWidth(null);
    final int imgHeight = img.getHeight(null);

    // top-left cornor
    g.drawImage(img, dx1, dy1, dx1 + insets.left, dy1 + insets.top, 0, 0, insets.left, insets.top, null);
    // top-right cornor
    g.drawImage(img, dx2 - insets.right, dy1, dx2, dy1 + insets.top, imgWidth - insets.right, 0, imgWidth,
            insets.top, null);
    // bottom-left cornor
    g.drawImage(img, dx1, dy2 - insets.bottom, dx1 + insets.left, dy2, 0, imgHeight - insets.bottom,
            insets.left, imgHeight, null);
    // bottom-right cornor
    g.drawImage(img, dx2 - insets.right, dy2 - insets.bottom, dx2, dy2, imgWidth - insets.right,
            imgHeight - insets.bottom, imgWidth, imgHeight, null);
    // top border
    g.drawImage(img, dx1 + insets.left, dy1, dx2 - insets.right, dy1 + insets.top, insets.left, 0,
            imgWidth - insets.right, insets.top, null);
    // bottom border
    g.drawImage(img, dx1 + insets.left, dy2 - insets.bottom, dx2 - insets.right, dy2, insets.left,
            imgHeight - insets.bottom, imgWidth - insets.right, imgHeight, null);
    // left border
    g.drawImage(img, dx1, dy1 + insets.top, dx1 + insets.left, dy2 - insets.bottom, 0, insets.top, insets.left,
            imgHeight - insets.bottom, null);
    // right border
    g.drawImage(img, dx2 - insets.right, dy1 + insets.top, dx2, dy2 - insets.bottom, imgWidth - insets.right,
            insets.top, imgWidth, imgHeight - insets.bottom, null);
    // center
    if (paintCenter) {
        g.drawImage(img, dx1 + insets.left, dy1 + insets.top, dx2 - insets.right, dy2 - insets.bottom,
                insets.left, insets.top, imgWidth - insets.right, imgHeight - insets.bottom, null);
    }
}

From source file:Main.java

/**
 * Converts a {@link Image} into a {@link BufferedImage}. If the image 
 * is already a {@link BufferedImage} then the same instance is returned.
 *
 * @param image the image to convert to a buffered image.
 * @param imageType the image type to use.
 * @return the converted image or the same instance if already a
 * {@link BufferedImage}.//  ww w  .  j av a  2 s  .c o  m
 */
public static BufferedImage toBufferedImage(Image image, int imageType) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }
    final BufferedImage buffImage = new BufferedImage(image.getWidth(null), image.getHeight(null), imageType);
    final Graphics2D gfx = buffImage.createGraphics();
    gfx.drawImage(image, 0, 0, null);
    return buffImage;
}