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

public static void main(String[] args) {
    Image image = new ImageIcon("image.gif").getImage();

    width = image.getWidth(null);
    height = image.getHeight(null);//from  w w  w. j a va2 s .c  om

}

From source file:BasicDraw.java

public static void main(String[] args) {
    Image image = Toolkit.getDefaultToolkit().getImage("image.gif");
    int width = image.getWidth(null);
    int height = image.getHeight(null);

    if (width >= 0) {
        // The image has been fully loaded
    } else {//  w  ww .ja v a 2s .c om
        // The image has not been fully loaded
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Image image = new ImageIcon("image.gif").getImage();

    BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null),
            BufferedImage.TYPE_INT_RGB);

    Graphics2D g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);//  w  w  w.  ja  va  2s .co m
    g.dispose();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL urlImage1 = new URL("http://www.java2s.com/style/download.png");

    final Image fgImage = ImageIO.read(urlImage1);
    int w = fgImage.getWidth(null);
    int h = fgImage.getHeight(null);
    final BufferedImage bgImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    final BufferedImage finalImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = finalImage.createGraphics();
    g.drawImage(bgImage, 0, 0, null);/*from w  ww .ja  v a  2 s  . c om*/
    g.drawImage(fgImage, 0, 0, null);
    g.dispose();

    Runnable r = new Runnable() {
        @Override
        public void run() {
            JPanel gui = new JPanel(new GridLayout(1, 0, 5, 5));

            gui.add(new JLabel(new ImageIcon(bgImage)));
            gui.add(new JLabel(new ImageIcon(fgImage)));
            gui.add(new JLabel(new ImageIcon(finalImage)));

            JOptionPane.showMessageDialog(null, gui);
        }
    };
    SwingUtilities.invokeLater(r);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Image img = new ImageIcon("test.png").getImage();

    BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null),
            BufferedImage.TYPE_INT_RGB);

    Graphics g = bufferedImage.createGraphics();
    g.drawImage(img, 0, 0, null);// w  w w.  j ava  2 s  . com
    g.dispose();

    ImageIO.write(bufferedImage, "png", new File("a.png"));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.delay(40);//  ww  w  .  ja v  a2 s . com
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
    robot.delay(404);

    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    DataFlavor[] flavors = cb.getAvailableDataFlavors();
    for (DataFlavor flavor : flavors) {
        if (flavor.toString().indexOf("java.awt.Image") <= 0) {
            continue;
        }
        Image i = (Image) cb.getData(flavor);
        BufferedImage bi = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.drawImage(i, 0, 0, null);
        g.dispose();
        ImageIO.write(bi, "png", new File("c:/Java_Dev/test.png"));
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {

    final int SCALE = 2;

    Image img = new ImageIcon(new URL("http://www.java2s.com/style/download.png")).getImage();

    BufferedImage bi = new BufferedImage(SCALE * img.getWidth(null), SCALE * img.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D grph = (Graphics2D) bi.getGraphics();
    grph.scale(SCALE, SCALE);// www  .  j a  va2s .  co m

    grph.drawImage(img, 0, 0, null);
    grph.dispose();

    ImageIO.write(bi, "png", new File("double_size.png"));
}

From source file:Main.java

static boolean isImage(String image_path) {
    Image image = new ImageIcon(image_path).getImage();
    if (image.getWidth(null) == -1) {
        return false;
    } else {/*from   w  ww .  j a va 2 s.  co m*/
        return true;
    }
}

From source file:Main.java

public static Image resizeToHeight(Image image, int h) {
    int w = image.getWidth(null) * h / image.getHeight(null);
    Image newimg = image.getScaledInstance(w, h, Image.SCALE_SMOOTH); // scale it the smooth way  
    return newimg;
}

From source file:Util.java

/**
 * Converts a java.awt.Image into an array of pixels
 *//*from  w ww  .  j  ava  2s.  c om*/
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;
}