Example usage for java.awt Image getWidth

List of usage examples for java.awt Image getWidth

Introduction

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

Prototype

public abstract int getWidth(ImageObserver observer);

Source Link

Document

Determines the width of the image.

Usage

From source file:org.mili.core.graphics.GraphicsUtil.java

/**
 * Points an image at coordinates x/y in defined image.
 *
 * @param og original image./*w w  w . j  av a  2  s  .  c o  m*/
 * @param i image to point.
 * @param x coordinate x >= 0.
 * @param y coordinate y >= 0.
 * @return new image included image to point.
 */
public static Image pointImage(Image og, Image i, int x, int y) {
    Validate.notNull(og, "original image cannot be null!");
    Validate.notNull(i, "image to point cannot be null!");
    Validate.isTrue(x >= 0, "x can only be >= 0!");
    Validate.isTrue(y >= 0, "y can only be >= 0!");
    BufferedImage bi = new BufferedImage(og.getWidth(null), og.getHeight(null), BufferedImage.TYPE_INT_RGB);
    og = new ImageIcon(og).getImage();
    i = new ImageIcon(i).getImage();
    Graphics2D g = (Graphics2D) bi.createGraphics();
    g.drawImage(og, 0, 0, null);
    g.drawImage(i, x, y, null);
    g.dispose();
    return bi;
}

From source file:net.rptools.lib.image.ImageUtil.java

public static BufferedImage createCompatibleImage(Image img, Map<String, Object> hints) {
    if (img == null) {
        return null;
    }/*from   w  w w . j  a  v  a 2s  .co  m*/
    return createCompatibleImage(img, img.getWidth(null), img.getHeight(null), hints);
}

From source file:com.t3.image.ImageUtil.java

public static BufferedImage createCompatibleImage(Image img, Map<String, Object> hints) {
    if (img == null) {
        return null;
    }/*  w w w. ja  v a2s . c  o  m*/

    return createCompatibleImage(img, img.getWidth(null), img.getHeight(null), hints);
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public static BufferedImage toBufferedImage(Image image) throws InterruptedException {

    if (image instanceof BufferedImage)
        return (BufferedImage) image;

    image = new ImageIcon(image).getImage();
    int type = hasAlpha(image) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
    BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    Graphics g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);/* w  w w .  j a  v a 2 s. com*/
    g.dispose();
    return bimage;
}

From source file:org.paxle.tools.icon.impl.IconTool.java

public static byte[] toBytes(Image icon) {
    try {//from   www .  j a v a 2s. co  m
        BufferedImage bi = null;
        if (icon instanceof BufferedImage) {
            bi = (BufferedImage) icon;
        } else {

            int width = icon.getWidth(null);
            int height = icon.getHeight(null);
            if (width <= 0)
                width = (height > 0) ? height : 32;
            if (height <= 0)
                height = (width > 0) ? width : 32;

            bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            bi.createGraphics().drawImage(icon, 0, 0, width, height, null);
        }

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ImageIO.write(bi, "png", bout);
        bout.flush();
        bout.close();

        return bout.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:org.sbs.util.ImageCompress.java

/** Encodes the given image at the given quality to the output stream. */
private static void encode(OutputStream outputStream, Image outputImage, String format)
        throws java.io.IOException {
    int outputWidth = outputImage.getWidth(null);
    if (outputWidth < 1)
        throw new IllegalArgumentException("output image width " + outputWidth + " is out of range");
    int outputHeight = outputImage.getHeight(null);
    if (outputHeight < 1)
        throw new IllegalArgumentException("output image height " + outputHeight + " is out of range");
    // Get a buffered image from the image.
    BufferedImage bi = new BufferedImage(outputWidth, outputHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D biContext = bi.createGraphics();
    biContext.drawImage(outputImage, 0, 0, null);
    ImageIO.write(bi, format, outputStream);
    outputStream.flush();/*w ww . ja  v  a2  s .c  om*/
}

From source file:ala.soils2sat.DrawingUtils.java

public static Rectangle resizeImage(Image img, int targetWidth, int targetHeight) {
    int srcHeight = img.getHeight(null);
    int srcWidth = img.getWidth(null);
    return resizeImage(srcWidth, srcHeight, targetWidth, targetHeight);
}

From source file:org.polymap.rhei.batik.engine.svg.Svg2Png.java

public static BufferedImage makeColorTransparent(BufferedImage im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {

        private int shift = 0xFF000000;

        public int rgbToMakeTransparent = color.getRGB() | shift;

        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | shift) == rgbToMakeTransparent) {
                return 0x00FFFFFF & rgb;
            }//  w ww  . j av  a 2 s . co  m
            return rgb;
        }
    };
    Image newImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(im.getSource(), filter));
    BufferedImage bufferedImage = new BufferedImage(newImage.getWidth(null), newImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(newImage, 0, 0, null);
    g2.dispose();
    return bufferedImage;
}

From source file:org.mili.core.graphics.GraphicsUtil.java

/**
 * Scales an image./* w  ww .  j  a va  2s .c o m*/
 *
 * @param i image.
 * @param scaleX scale to x >= 0.
 * @param scaleY scale to y >= 0.
 * @return scaled image.
 */
public static Image scaleImage(Image i, int scaleX, int scaleY) {
    Validate.notNull(i, "image cannot be null!");
    Validate.isTrue(scaleX >= 0, "scaleX cannot be < 0!");
    Validate.isTrue(scaleY >= 0, "scaleY cannot be < 0!");
    if (scaleX == 0 && scaleY == 0) {
        return i;
    }
    double f = getRelationFactor(scaleX, scaleY, i);
    int x = (int) (i.getWidth(null) * f);
    int y = (int) (i.getHeight(null) * f);
    i = i.getScaledInstance(x, y, Image.SCALE_SMOOTH);
    ImageIcon ii = new ImageIcon(i);
    i = ii.getImage();
    if (i instanceof ToolkitImage) {
        ToolkitImage ti = (ToolkitImage) i;
        i = ti.getBufferedImage();
    }
    return i;
}

From source file:smanilov.mandelbrot.compute.Computer.java

/**
 * Returns a crop view of the Mandelbrot set.
 * @param drawing the image to draw on.//from  w ww .ja v  a2  s .  co m
 * @param foregroundColor the color to use when drawing Mandelbrot pixels.
 * @param backgroundColor the color to use when drawing non-Mandelbrot pixels.
 * @param drawingLock the lock to use for locking the image.
 * @param center Center of the crop, in units of the complex plane, w.r.t. the origin.
 * @param scale log2(pixels) per unit in the complex plane. 
 */
public static void drawMandelbrotCrop(final Image drawing, final Color foregroundColor,
        final Color backgroundColor, final ReentrantLock drawingLock, final int scale, final Point2D center) {
    initDrawStep(drawing);

    Thread pt = createProducerThread(drawing.getWidth(null), drawing.getHeight(null));
    pt.start();

    for (int i = 0; i < shaders; ++i) {
        Thread sh = createShaderThread(drawing, foregroundColor, backgroundColor, drawingLock, scale, center);
        sh.start();
    }
}