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.dishevelled.piccolo.sprite.statemachine.AbstractStateMachineSprite.java

@Override
public final void paint(final PPaintContext paintContext) {
    if (currentAnimation != null) {
        Graphics2D g = paintContext.getGraphics();
        Image currentFrame = currentAnimation.getCurrentFrame();
        PBounds bounds = getBoundsReference();

        double w = currentFrame.getWidth(null);
        double h = currentFrame.getHeight(null);

        g.translate(bounds.getX(), bounds.getY());
        g.scale(bounds.getWidth() / w, bounds.getHeight() / h);
        g.drawImage(currentFrame, 0, 0, null);
        g.scale(w / bounds.getWidth(), h / bounds.getHeight());
        g.translate(-1 * bounds.getX(), -1 * bounds.getY());
    }//from w w w . java 2  s  .c  o  m
}

From source file:org.fao.geonet.services.thumbnail.Set.java

public BufferedImage getImage(String inFile) throws IOException {
    String lcFile = inFile.toLowerCase();

    if (lcFile.endsWith(".tif") || lcFile.endsWith(".tiff")) {
        //--- load the TIFF/GEOTIFF file format

        Image image = getTiffImage(inFile);

        int width = image.getWidth(null);
        int height = image.getHeight(null);

        BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = bimg.createGraphics();
        g.drawImage(image, 0, 0, null);/*w  w  w. j a  v  a 2  s. c o m*/
        g.dispose();

        return bimg;
    }

    return ImageIO.read(new File(inFile));
}

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

@Test
public void test_Image_readImage_File() {
    // negativ//from w ww . j  av  a 2  s  .  c  o  m
    try {
        GraphicsUtil.readImage(null);
        fail("here was null given, but method works !");
    } catch (IllegalArgumentException e) {
        assertTrue(true);
    } catch (IOException e) {
        fail();
    }
    try {
        GraphicsUtil.readImage(new File(this.dir, "/xyz/test.jpg"));
        fail("file not exists here, but method works !");
    } catch (IOException e) {
        assertTrue(true);
    }
    // positiv
    try {
        Image i = GraphicsUtil.readImage(new File(this.dir, "test.jpg"));
        assertTrue(i != null);
        assertEquals(100, i.getHeight(null));
        assertEquals(100, i.getWidth(null));
    } catch (IOException e) {
        e.printStackTrace();
        fail();
    }
    return;
}

From source file:TextBouncer.java

public BufferedImage makeBufferedImage(Image image, int imageType) {
    if (waitForImage(image) == false)
        return null;

    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), imageType);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(image, null, null);/*  w w  w .  j  a  v  a2  s .c  om*/
    return bufferedImage;
}

From source file:GIFEncoder.java

/**
 * Construct a GIFEncoder. The constructor will convert the image to
 * an indexed color array. <B>This may take some time.</B><P>
 * //from  ww  w . j  a  v  a  2s .  c  o m
 * @param image The image to encode. The image <B>must</B> be
 * completely loaded.
 * @exception AWTException Will be thrown if the pixel grab fails. This
 * can happen if Java runs out of memory. It may also indicate that the image
 * contains more than 256 colors.
 * */
public GIFEncoder(Image image) throws AWTException {
    width_ = (short) image.getWidth(null);
    height_ = (short) image.getHeight(null);

    int values[] = new int[width_ * height_];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, width_, height_, values, 0, width_);

    try {
        if (grabber.grabPixels() != true)
            throw new AWTException("Grabber returned false: " + grabber.status());
    } catch (InterruptedException e) {
        ;
    }

    byte r[][] = new byte[width_][height_];
    byte g[][] = new byte[width_][height_];
    byte b[][] = new byte[width_][height_];
    int index = 0;
    for (int y = 0; y < height_; ++y)
        for (int x = 0; x < width_; ++x) {
            r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
            g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
            b[x][y] = (byte) ((values[index]) & 0xFF);
            ++index;
        }
    ToIndexedColor(r, g, b);
}

From source file:no.geosoft.cc.io.GifEncoder.java

/**
 * Constructing a GIF encoder./*w w w  . j a va  2  s . c om*/
 *
 * @param image  The image to encode. The image must be
 *               completely loaded.
 * @throws AWTException  If memory is exhausted or image contains
 *                       more than 256 colors.
 */
public GifEncoder(Image image) throws AWTException {
    imageWidth_ = (short) image.getWidth(null);
    imageHeight_ = (short) image.getHeight(null);

    int values[] = new int[imageWidth_ * imageHeight_];
    PixelGrabber grabber = new PixelGrabber(image, 0, 0, imageWidth_, imageHeight_, values, 0, imageWidth_);

    try {
        if (grabber.grabPixels() != true)
            throw new AWTException("Grabber returned false: " + grabber.status());
    } catch (InterruptedException exception) {
    }

    byte[][] r = new byte[imageWidth_][imageHeight_];
    byte[][] g = new byte[imageWidth_][imageHeight_];
    byte[][] b = new byte[imageWidth_][imageHeight_];

    int index = 0;

    for (int y = 0; y < imageHeight_; y++) {
        for (int x = 0; x < imageWidth_; x++, index++) {
            r[x][y] = (byte) ((values[index] >> 16) & 0xFF);
            g[x][y] = (byte) ((values[index] >> 8) & 0xFF);
            b[x][y] = (byte) ((values[index] >> 0) & 0xFF);
        }
    }

    toIndexColor(r, g, b);
}

From source file:org.encuestame.business.images.ImageThumbnailGeneratorImpl.java

/**
 * Create a thumbnail image and save it to disk.
 *
 * This algorithm is based on:/*from w  w  w .  j  a va 2 s.c  o  m*/
 *      http://www.philreeve.com/java_high_quality_thumbnails.php
 *
 * @param imageIn           The image you want to scale.
 * @param fileOut           The output file.
 * @param largestDimension  The largest dimension, so that neither the width nor height
 *                          will exceed this value.
 *
 * @return the image that was created, null if imageIn or fileOut is null.
 * @throws java.io.IOException if something goes wrong when saving as jpeg
 */
public BufferedImage createThumbnailImage(Image imageIn, File fileOut, int largestDimension)
        throws IOException {
    if ((imageIn == null) || (fileOut == null)) {
        return null;
    }
    //it seems to not return the right size until the methods get called for the first time
    imageIn.getWidth(null);
    imageIn.getHeight(null);

    // Find biggest dimension
    int nImageWidth = imageIn.getWidth(null);
    int nImageHeight = imageIn.getHeight(null);
    int nImageLargestDim = Math.max(nImageWidth, nImageHeight);
    double scale = (double) largestDimension / (double) nImageLargestDim;
    int sizeDifference = nImageLargestDim - largestDimension;

    //create an image buffer to draw to
    BufferedImage imageOut = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); // 8-bit RGB
    Graphics2D g2d;
    AffineTransform tx;

    // Use a few steps if the sizes are drastically different, and only scale
    // if the desired size is smaller than the original.
    int numSteps = 0;
    if (scale < 1.0d) {
        // Make sure we have at least 1 step
        numSteps = Math.max(1, (sizeDifference / 100));
    }

    if (numSteps > 0) {
        int stepSize = sizeDifference / numSteps;
        int stepWeight = stepSize / 2;
        int heavierStepSize = stepSize + stepWeight;
        int lighterStepSize = stepSize - stepWeight;
        int currentStepSize, centerStep;
        double scaledW = imageIn.getWidth(null);
        double scaledH = imageIn.getHeight(null);

        if ((numSteps % 2) == 1) //if there's an odd number of steps
            centerStep = (int) Math.ceil((double) numSteps / 2d); //find the center step
        else
            centerStep = -1; //set it to -1 so it's ignored later

        Integer intermediateSize;
        Integer previousIntermediateSize = nImageLargestDim;

        for (Integer i = 0; i < numSteps; i++) {
            if (i + 1 != centerStep) {
                //if this isn't the center step

                if (i == numSteps - 1) {
                    //if this is the last step
                    //fix the stepsize to account for decimal place errors previously
                    currentStepSize = previousIntermediateSize - largestDimension;
                } else {
                    if (numSteps - i > numSteps / 2) //if we're in the first half of the reductions
                        currentStepSize = heavierStepSize;
                    else
                        currentStepSize = lighterStepSize;
                }
            } else {
                //center step, use natural step size
                currentStepSize = stepSize;
            }

            intermediateSize = previousIntermediateSize - currentStepSize;
            scale = intermediateSize / (double) previousIntermediateSize;
            scaledW = Math.max((int) (scaledW * scale), 1);
            scaledH = Math.max((int) (scaledH * scale), 1);

            log.info("step " + i + ": scaling to " + scaledW + " x " + scaledH);
            imageOut = new BufferedImage((int) scaledW, (int) scaledH, BufferedImage.TYPE_INT_RGB); // 8 bit RGB
            g2d = imageOut.createGraphics();
            g2d.setBackground(Color.WHITE);
            g2d.clearRect(0, 0, imageOut.getWidth(), imageOut.getHeight());
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            tx = new AffineTransform();
            tx.scale(scale, scale);
            g2d.drawImage(imageIn, tx, null);
            g2d.dispose();
            imageIn = new ImageIcon(imageOut).getImage();
            previousIntermediateSize = intermediateSize;
        }
    } else {
        // This enforces a rule that we always have an 8-bit image with white background for the thumbnail.  Plus, for large
        // images, this makes subsequent downscaling really fast because we are working on a large 8-bit image
        // instead of a large 12 or 24 bit image, so the downstream effect is very noticable.
        imageOut = new BufferedImage(imageIn.getWidth(null), imageIn.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        g2d = imageOut.createGraphics();
        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, imageOut.getWidth(), imageOut.getHeight());
        tx = new AffineTransform();
        tx.setToIdentity(); //use identity matrix so image is copied exactly
        g2d.drawImage(imageIn, tx, null);
        g2d.dispose();
    }
    //saveImageAsJPEG(imageOut, fileOut);
    ImageIO.write(imageOut, "jpg", fileOut);
    return imageOut;
}

From source file:edu.uci.ics.jung.visualization.decorators.VertexIconShapeTransformer.java

/**
 * get the shape from the image. If not available, get
 * the shape from the delegate VertexShapeFunction
 *///from  w w  w .  j  a  v a 2s .c om
public Shape transform(V v) {
    Icon icon = iconMap.get(v);
    if (icon != null && icon instanceof ImageIcon) {
        Image image = ((ImageIcon) icon).getImage();
        Shape shape = (Shape) shapeMap.get(image);
        if (shape == null) {
            shape = FourPassImageShaper.getShape(image, 30);
            if (shape.getBounds().getWidth() > 0 && shape.getBounds().getHeight() > 0) {
                // don't cache a zero-sized shape, wait for the image
                // to be ready
                int width = image.getWidth(null);
                int height = image.getHeight(null);
                AffineTransform transform = AffineTransform.getTranslateInstance(-width / 2, -height / 2);
                shape = transform.createTransformedShape(shape);
                shapeMap.put(image, shape);
            }
        }
        return shape;
    } else {
        return delegate.transform(v);
    }
}

From source file:jmap2gml.ItemImage.java

@Override
public void draw(Graphics g) {
    Image img = IMAGES.get(itemName);

    int xOffset, yOffset;

    if (config.has(itemName + "XOFFSET")) {
        xOffset = config.getInt(itemName + "XOFFSET");
    } else {/*w w w . j a  va2  s. c om*/
        xOffset = 0;
    }

    if (config.has(itemName + "YOFFSET")) {
        yOffset = config.getInt(itemName + "YOFFSET");
    } else {
        yOffset = 0;
    }

    if (img != null) {
        int width, height;

        width = (int) (img.getWidth(null) * xScale);
        height = (int) (img.getHeight(null) * yScale);

        g.drawImage(img, x + xOffset, y + yOffset, width, height, null);
    } else {
        super.draw(g);
    }
}

From source file:gg.msn.ui.panel.MainPanel.java

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    super.paintComponent(g2d);
    g2d.setRenderingHints(//  w ww  . j a v  a  2 s .  c  o m
            new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR));

    try {

        ImageIcon icon = ThemeManager.getTheme().get(ThemeManager.MAIN_BACKGROUND);

        if (icon != null) {
            Image image = icon.getImage();
            g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        } else {
            log.warn("image " + icon.getDescription() + " not Found");
        }

        icon = ThemeManager.getTheme().get(ThemeManager.MAIN_IMAGE);

        if (icon != null) {

            Image cartel = icon.getImage();
            g2d.drawImage(cartel, 0 - cartel.getWidth(this) / 10, getHeight() - cartel.getHeight(this), this);
        } else {
            log.warn("image " + icon.getDescription() + " not Found");
        }
    } catch (Exception e) {
        //            log.error(e);
    }
}