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, int width, int height, ImageObserver observer);

Source Link

Document

Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.

Usage

From source file:Capture.java

public static void main(String[] args) {
    JFrame capture = new JFrame();
    capture.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Toolkit kit = Toolkit.getDefaultToolkit();
    final Dimension d = kit.getScreenSize();
    capture.setSize(d);//from w ww.j  a  v  a  2  s  .  co m

    Rectangle rect = new Rectangle(d);
    try {
        Robot robot = new Robot();
        final BufferedImage image = robot.createScreenCapture(rect);
        image.flush();
        JPanel panel = new JPanel() {
            public void paintComponent(Graphics g) {
                g.drawImage(image, 0, 0, d.width, d.height, this);
            }
        };
        panel.setOpaque(false);
        panel.prepareImage(image, panel);
        panel.repaint();
        capture.getContentPane().add(panel);
    } catch (Exception e) {
        e.printStackTrace();
    }

    capture.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    try {//from   www. j  av a  2 s. com
        bg = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    } catch (Exception ex) {
        System.out.println(ex);
    }

    JPanel tabPanel = new JPanel(new GridBagLayout()) {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 300);
        }
    };
    JPanel buttons = new JPanel(new GridLayout(4, 1, 15, 15));
    buttons.setOpaque(false);
    for (int i = 0; i < 4; i++) {
        buttons.add(new JButton("Button"));
    }
    tabPanel.add(buttons);

    JTabbedPane tabPane = new JTabbedPane();
    tabPane.add("Panel with Bachground", tabPanel);

    JFrame frame = new JFrame();
    frame.setContentPane(tabPane);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:WaterMark.java

public static String execute(String src, String dest, String text, Color color, Font font) throws Exception {
    BufferedImage srcImage = ImageIO.read(new File(src));

    int width = srcImage.getWidth(null);
    int height = srcImage.getHeight(null);
    BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = destImage.getGraphics();

    g.drawImage(srcImage, 0, 0, width, height, null);
    g.setColor(color);/*from  w  w  w .j av a2s .  c om*/
    g.setFont(font);
    g.fillRect(0, 0, 50, 50);
    g.drawString(text, width / 5, height - 10);
    g.dispose();

    ImageIO.write(destImage, DEFAULT_FORMAT, new File("dest.jpg"));
    return dest;
}

From source file:Utils.java

/**
 * Produces a resized image that is of the given dimensions
 *
 * @param image The original image//from ww w .j  a  va 2 s . c o  m
 * @param width The desired width
 * @param height The desired height
 * @return The new BufferedImage
 */
public static BufferedImage scaledImage(BufferedImage image, int width, int height) {
    BufferedImage newImage = createCompatibleImage(width, height);
    Graphics graphics = newImage.createGraphics();

    graphics.drawImage(image, 0, 0, width, height, null);

    graphics.dispose();
    return newImage;
}

From source file:ImageUtils.java

/**
 * Scales down an image into a box of maxSideLenght x maxSideLength.
 * @param image the image to scale down. It remains untouched.
 * @param maxSideLength the maximum side length of the scaled down instance. Has to be > 0.
 * @return the scaled image, the//from w w  w .  j ava2  s  .  c o m
 */
public static BufferedImage scaleImage(BufferedImage image, int maxSideLength) {
    assert (maxSideLength > 0);
    double originalWidth = image.getWidth();
    double originalHeight = image.getHeight();
    double scaleFactor = 0.0;
    if (originalWidth > originalHeight) {
        scaleFactor = ((double) maxSideLength / originalWidth);
    } else {
        scaleFactor = ((double) maxSideLength / originalHeight);
    }
    // create smaller image
    BufferedImage img = new BufferedImage((int) (originalWidth * scaleFactor),
            (int) (originalHeight * scaleFactor), BufferedImage.TYPE_INT_RGB);
    // fast scale (Java 1.4 & 1.5)
    Graphics g = img.getGraphics();
    g.drawImage(image, 0, 0, img.getWidth(), img.getHeight(), null);
    return img;
}

From source file:jshm.gui.GuiUtil.java

/**
 * Resize an ImageIcon to the specified size.
 * @param icon The ImageIcon to resize/*w  w w . j  a va 2s.  co  m*/
 * @param width The new width
 * @param height The new height
 */
public final static javax.swing.ImageIcon resizeIcon(ImageIcon icon, int width, int height) {
    LOG.info("Resizing to " + width + "x" + height);
    Image img = icon.getImage();
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics g = bi.createGraphics();
    g.drawImage(img, 0, 0, width, height, null);
    g.dispose();

    return new javax.swing.ImageIcon(bi);
}

From source file:com.ricemap.spateDB.operations.Plot.java

/**
 * Combines images of different datasets into one image that is displayed
 * to users./*from  w  w  w.  j a v  a2s.  c o m*/
 * @param fs The file system that contains the datasets and images
 * @param files Paths to directories which contains the datasets
 * @param includeBoundaries Also plot the indexing boundaries of datasets
 * @return An image that is the combination of all datasets images
 * @throws IOException
 */
public static BufferedImage combineImages(Configuration conf, Path[] files, boolean includeBoundaries,
        int width, int height) throws IOException {
    BufferedImage result = null;
    // Retrieve the MBRs of all datasets
    Prism allMbr = new Prism(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE, -Double.MAX_VALUE,
            -Double.MAX_VALUE, -Double.MAX_VALUE);
    for (Path file : files) {
        FileSystem fs = file.getFileSystem(conf);
        Prism mbr = FileMBR.fileMBR(fs, file, null);
        allMbr.expand(mbr);
    }

    // Adjust width and height to maintain aspect ratio
    if ((allMbr.x2 - allMbr.x1) / (allMbr.y2 - allMbr.y1) > (double) width / height) {
        // Fix width and change height
        height = (int) ((allMbr.y2 - allMbr.y1) * width / (allMbr.x2 - allMbr.x1));
    } else {
        width = (int) ((allMbr.x2 - allMbr.x1) * height / (allMbr.y2 - allMbr.y1));
    }
    result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    for (Path file : files) {
        FileSystem fs = file.getFileSystem(conf);
        if (fs.getFileStatus(file).isDir()) {
            // Retrieve the MBR of this dataset
            Prism mbr = FileMBR.fileMBR(fs, file, null);
            // Compute the coordinates of this image in the whole picture
            mbr.x1 = (mbr.x1 - allMbr.x1) * width / allMbr.getWidth();
            mbr.x2 = (mbr.x2 - allMbr.x1) * width / allMbr.getWidth();
            mbr.y1 = (mbr.y1 - allMbr.y1) * height / allMbr.getHeight();
            mbr.y2 = (mbr.y2 - allMbr.y1) * height / allMbr.getHeight();
            // Retrieve the image of this dataset
            Path imagePath = new Path(file, "_data.png");
            if (!fs.exists(imagePath))
                throw new RuntimeException("Image " + imagePath + " not ready");
            FSDataInputStream imageFile = fs.open(imagePath);
            BufferedImage image = ImageIO.read(imageFile);
            imageFile.close();
            // Draw the image
            Graphics graphics = result.getGraphics();
            graphics.drawImage(image, (int) mbr.x1, (int) mbr.y1, (int) mbr.getWidth(), (int) mbr.getHeight(),
                    null);
            graphics.dispose();

            if (includeBoundaries) {
                // Plot also the image of the boundaries
                // Retrieve the image of the dataset boundaries
                imagePath = new Path(file, "_partitions.png");
                if (fs.exists(imagePath)) {
                    imageFile = fs.open(imagePath);
                    image = ImageIO.read(imageFile);
                    imageFile.close();
                    // Draw the image
                    graphics = result.getGraphics();
                    graphics.drawImage(image, (int) mbr.x1, (int) mbr.y1, (int) mbr.getWidth(),
                            (int) mbr.getHeight(), null);
                    graphics.dispose();
                }
            }
        }
    }

    return result;
}

From source file:edu.umn.cs.spatialHadoop.operations.Plot.java

/**
 * Combines images of different datasets into one image that is displayed
 * to users./*  w ww  .  ja  v  a2  s  .c  om*/
 * @param fs The file system that contains the datasets and images
 * @param files Paths to directories which contains the datasets
 * @param includeBoundaries Also plot the indexing boundaries of datasets
 * @return An image that is the combination of all datasets images
 * @throws IOException
 */
public static BufferedImage combineImages(Configuration conf, Path[] files, boolean includeBoundaries,
        int width, int height) throws IOException {
    BufferedImage result = null;
    // Retrieve the MBRs of all datasets
    Rectangle allMbr = new Rectangle(Double.MAX_VALUE, Double.MAX_VALUE, -Double.MAX_VALUE, -Double.MAX_VALUE);
    for (Path file : files) {
        FileSystem fs = file.getFileSystem(conf);
        Rectangle mbr = FileMBR.fileMBR(fs, file, null);
        allMbr.expand(mbr);
    }

    // Adjust width and height to maintain aspect ratio
    if ((allMbr.x2 - allMbr.x1) / (allMbr.y2 - allMbr.y1) > (double) width / height) {
        // Fix width and change height
        height = (int) ((allMbr.y2 - allMbr.y1) * width / (allMbr.x2 - allMbr.x1));
    } else {
        width = (int) ((allMbr.x2 - allMbr.x1) * height / (allMbr.y2 - allMbr.y1));
    }
    result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    for (Path file : files) {
        FileSystem fs = file.getFileSystem(conf);
        if (fs.getFileStatus(file).isDir()) {
            // Retrieve the MBR of this dataset
            Rectangle mbr = FileMBR.fileMBR(fs, file, null);
            // Compute the coordinates of this image in the whole picture
            mbr.x1 = (mbr.x1 - allMbr.x1) * width / allMbr.getWidth();
            mbr.x2 = (mbr.x2 - allMbr.x1) * width / allMbr.getWidth();
            mbr.y1 = (mbr.y1 - allMbr.y1) * height / allMbr.getHeight();
            mbr.y2 = (mbr.y2 - allMbr.y1) * height / allMbr.getHeight();
            // Retrieve the image of this dataset
            Path imagePath = new Path(file, "_data.png");
            if (!fs.exists(imagePath))
                throw new RuntimeException("Image " + imagePath + " not ready");
            FSDataInputStream imageFile = fs.open(imagePath);
            BufferedImage image = ImageIO.read(imageFile);
            imageFile.close();
            // Draw the image
            Graphics graphics = result.getGraphics();
            graphics.drawImage(image, (int) mbr.x1, (int) mbr.y1, (int) mbr.getWidth(), (int) mbr.getHeight(),
                    null);
            graphics.dispose();

            if (includeBoundaries) {
                // Plot also the image of the boundaries
                // Retrieve the image of the dataset boundaries
                imagePath = new Path(file, "_partitions.png");
                if (fs.exists(imagePath)) {
                    imageFile = fs.open(imagePath);
                    image = ImageIO.read(imageFile);
                    imageFile.close();
                    // Draw the image
                    graphics = result.getGraphics();
                    graphics.drawImage(image, (int) mbr.x1, (int) mbr.y1, (int) mbr.getWidth(),
                            (int) mbr.getHeight(), null);
                    graphics.dispose();
                }
            }
        }
    }

    return result;
}

From source file:com.simiacryptus.util.Util.java

/**
 * Resize buffered image.// w  ww  .j  av a  2s.  c  o m
 *
 * @param image the image
 * @return the buffered image
 */
@Nullable
public static BufferedImage resize(@Nullable final BufferedImage image) {
    if (null == image)
        return image;
    final int width = Math.min(image.getWidth(), 800);
    if (width == image.getWidth())
        return image;
    final int height = image.getHeight() * width / image.getWidth();
    @javax.annotation.Nonnull
    final BufferedImage rerender = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics gfx = rerender.getGraphics();
    @javax.annotation.Nonnull
    final RenderingHints hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    ((Graphics2D) gfx).setRenderingHints(hints);
    gfx.drawImage(image, 0, 0, rerender.getWidth(), rerender.getHeight(), null);
    return rerender;
}

From source file:flushMe.java

public void paint(Graphics g) {
    g.drawImage(im, 0, 0, 175, 225, this);
}