Example usage for java.awt Graphics drawImage

List of usage examples for java.awt Graphics drawImage

Introduction

In this page you can find the example usage for java.awt Graphics drawImage.

Prototype

public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer);

Source Link

Document

Draws as much of the specified image as is currently available.

Usage

From source file:Main.java

/**
 * Create a custom cursor out of the specified image, with the specified hotspot.
 *//*w  w  w.j  a  v  a2s. c o  m*/
public static Cursor createImageCursor(Image img, Point hotspot) {
    Toolkit tk = Toolkit.getDefaultToolkit();

    // for now, just report the cursor restrictions, then blindly create
    int w = img.getWidth(null);
    int h = img.getHeight(null);
    Dimension d = tk.getBestCursorSize(w, h);
    //         int colors = tk.getMaximumCursorColors();
    //         Log.debug("Creating custom cursor [desiredSize=" + w + "x" + h +
    //                   ", bestSize=" + d.width + "x" + d.height +
    //                   ", maxcolors=" + colors + "].");

    // if the passed-in image is smaller, pad it with transparent pixels and use it anyway.
    if (((w < d.width) && (h <= d.height)) || ((w <= d.width) && (h < d.height))) {
        Image padder = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration().createCompatibleImage(d.width, d.height, Transparency.BITMASK);
        Graphics g = padder.getGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();

        // and reassign the image to the padded image
        img = padder;

        // and adjust the 'best' to cheat the hotspot checking code
        d.width = w;
        d.height = h;
    }

    // make sure the hotspot is valid
    if (hotspot == null) {
        hotspot = new Point(d.width / 2, d.height / 2);
    } else {
        hotspot.x = Math.min(d.width - 1, Math.max(0, hotspot.x));
        hotspot.y = Math.min(d.height - 1, Math.max(0, hotspot.y));
    }

    // and create the cursor
    return tk.createCustomCursor(img, hotspot, "samskivertDnDCursor");
}

From source file:ImageClip.java

/**
 * Clips the input image to the specified shape
 * /*ww  w .  ja  va 2s .c  om*/
 * @param image
 *           the input image
 * @param clipVerts
 *           list of x, y pairs defining the clip shape, normalised
 *           to image dimensions (think texture coordinates)
 * @return The smallest image containing those pixels that fall
 *         inside the clip shape
 */
public static BufferedImage clip(BufferedImage image, float... clipVerts) {
    assert clipVerts.length >= 6;
    assert clipVerts.length % 2 == 0;

    int[] xp = new int[clipVerts.length / 2];
    int[] yp = new int[xp.length];

    int minX = image.getWidth(), minY = image.getHeight(), maxX = 0, maxY = 0;

    for (int j = 0; j < xp.length; j++) {
        xp[j] = Math.round(clipVerts[2 * j] * image.getWidth());
        yp[j] = Math.round(clipVerts[2 * j + 1] * image.getHeight());

        minX = Math.min(minX, xp[j]);
        minY = Math.min(minY, yp[j]);
        maxX = Math.max(maxX, xp[j]);
        maxY = Math.max(maxY, yp[j]);
    }

    for (int i = 0; i < xp.length; i++) {
        xp[i] -= minX;
        yp[i] -= minY;
    }

    Polygon clip = new Polygon(xp, yp, xp.length);
    BufferedImage out = new BufferedImage(maxX - minX, maxY - minY, image.getType());
    Graphics g = out.getGraphics();
    g.setClip(clip);

    g.drawImage(image, -minX, -minY, null);
    g.dispose();

    return out;
}

From source file:SplashScreenTest.java

/**
 * This method displays a frame with the same image as the splash screen.
 *///from   w  w  w  .ja  v a  2  s. c om
private static void init2() {
    final Image img = Toolkit.getDefaultToolkit().getImage(splash.getImageURL());

    final JFrame splashFrame = new JFrame();
    splashFrame.setUndecorated(true);

    final JPanel splashPanel = new JPanel() {
        public void paintComponent(Graphics g) {
            g.drawImage(img, 0, 0, null);
        }
    };

    final JProgressBar progressBar = new JProgressBar();
    progressBar.setStringPainted(true);
    splashPanel.setLayout(new BorderLayout());
    splashPanel.add(progressBar, BorderLayout.SOUTH);

    splashFrame.add(splashPanel);
    splashFrame.setBounds(splash.getBounds());
    splashFrame.setVisible(true);

    new SwingWorker<Void, Integer>() {
        protected Void doInBackground() throws Exception {
            try {
                for (int i = 0; i <= 100; i++) {
                    publish(i);
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
            }
            return null;
        }

        protected void process(List<Integer> chunks) {
            for (Integer chunk : chunks) {
                progressBar.setString("Loading module " + chunk);
                progressBar.setValue(chunk);
                splashPanel.repaint(); // because img is loaded asynchronously
            }
        }

        protected void done() {
            splashFrame.setVisible(false);

            JFrame frame = new JFrame();
            frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("SplashScreenTest");
            frame.setVisible(true);
        }
    }.execute();
}

From source file:de.laures.cewolf.util.ImageHelper.java

public static BufferedImage loadBufferedImage(String fileName) {
    Image image = loadImage(fileName);
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from  www.  j av  a  2s  .c  o m*/
    /*        final boolean hasAlpha = hasAlpha(image);
    int transparency = Transparency.OPAQUE;
    if (hasAlpha) {
    transparency = Transparency.BITMASK;
    }*/
    int width = (int) Math.max(1.0, image.getWidth(null));
    int height = (int) Math.max(1.0, image.getHeight(null));
    // BufferedImage bimage = GRAPHICS_CONV.createCompatibleImage(width, height, transparency);
    BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}

From source file:zz.pseas.ghost.utils.BrowserUtil.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//from   ww w. j a  va  2  s.c o  m
    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        int transparency = Transparency.OPAQUE;
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }
    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }
    // Copy image to buffered image
    Graphics g = bimage.createGraphics();
    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}

From source file:net.duckling.ddl.util.ImageUtils.java

/**
 * ???/*w w w.j ava 2 s  .c  om*/
 * @param tmpFilePath ?
 * @return
 */
public static boolean scare(String tmpFilePath) {
    try {
        BufferedImage src = ImageIO.read(new File(tmpFilePath)); // 
        int width = src.getWidth();
        int height = src.getHeight();
        if (width > DEFAULT_WIDTH) {
            height = (DEFAULT_WIDTH * height) / width;
            width = DEFAULT_WIDTH;
        }
        Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = tag.getGraphics();
        g.drawImage(image, 0, 0, null); // ??
        g.dispose();
        File resultFile = new File(tmpFilePath);
        ImageIO.write(tag, "JPEG", resultFile);// ?
        return true;
    } catch (IOException e) {
        LOG.error(e);
    }
    return false;
}

From source file:Main.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }//from  ww  w.  ja v a  2  s  .  co  m

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = true;

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:ImageUtilities.java

private static BufferedImage convertToARGB(BufferedImage srcImage) {
    BufferedImage newImage = new BufferedImage(srcImage.getWidth(null), srcImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics bg = newImage.getGraphics();
    bg.drawImage(srcImage, 0, 0, null);
    bg.dispose();//from   w  ww.  jav  a2  s.  c  o m
    return newImage;
}

From source file:ImageUtil.java

/**
 * Posted by alpha02 at http://www.dreamincode.net/code/snippet1076.htm
 *///from  www.ja  va2  s  . co  m
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage)
        return (BufferedImage) image;

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;

        if (hasAlpha == true)
            transparency = Transparency.BITMASK;

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();

        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
    } //No screen

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;

        if (hasAlpha == true) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:Main.java

/**
 * This method returns a buffered image with the contents of an image
 * This snippet was taken from: http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html
 * @param image/*from  ww  w . jav  a  2 s.c om*/
 * @return The buffered image
 */
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see e661 Determining If an Image Has Transparent Pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}