List of usage examples for java.awt Graphics2D drawImage
public abstract boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer);
From source file:GraphicsUtil.java
public static void drawCentered(Graphics g, BufferedImage img, Point2D location, double newWidth, double newHeight, ImageObserver observer) { Graphics2D g2 = (Graphics2D) g; g2.drawImage(img, // what to draw (int) (location.getX() - (newWidth / 2)), // dest left (int) (location.getY() - (newHeight / 2)), // dest top (int) (location.getX() + (newWidth / 2)), // dest right (int) (location.getY() + (newHeight / 2)), // dest bottom 0, // src left 0, // src top img.getWidth(), // src right img.getHeight(), // src bottom observer // to notify of image updates );// www . jav a2 s . c o m }
From source file:GraphicsUtil.java
public static void drawImage(Graphics g, BufferedImage img, Rectangle2D bounds, ImageObserver observer) { Graphics2D g2 = (Graphics2D) g; g2.drawImage(img, // what to draw (int) bounds.getMinX(), // dest left (int) bounds.getMinY(), // dest top (int) bounds.getMaxX(), // dest right (int) bounds.getMaxY(), // dest bottom 0, // src left 0, // src top img.getWidth(), // src right img.getHeight(), // src bottom observer // to notify of image updates );//from w w w . j a v a2 s . c o m }
From source file:CursorUtil.java
public static Cursor createCursor(BufferedImage img, Point hotspot, String name) throws IndexOutOfBoundsException, Exception { Toolkit tk = Toolkit.getDefaultToolkit(); Dimension d = tk.getBestCursorSize(img.getWidth(), img.getHeight()); if ((d.width == img.getWidth()) && (d.height == img.getHeight())) return tk.createCustomCursor(img, hotspot, name); if ((d.width + d.height) < 2) throw new Exception("Invalid Size"); BufferedImage newImg = GraphicsUtil.createImage(d.width, d.height); Graphics2D g2 = newImg.createGraphics(); g2.drawImage(img, // what to draw 0, // dest left 0, // dest top newImg.getWidth(), // dest right newImg.getHeight(), // dest bottom 0, // src left 0, // src top img.getWidth(), // src right img.getHeight(), // src bottom null // to notify of image updates );//from www . ja v a2s.c om return tk.createCustomCursor(newImg, hotspot, name); }
From source file:ImageUtil.java
public static BufferedImage crop(BufferedImage src, int width, int height) throws IOException { int x = src.getWidth() / 2 - width / 2; int y = src.getHeight() / 2 - height / 2; // System.out.println("---" + src.getWidth() + " - " + src.getHeight() + " - " + x + " - " + y); BufferedImage clipping = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//src.getType()); Graphics2D area = (Graphics2D) clipping.getGraphics().create(); area.drawImage(src, 0, 0, clipping.getWidth(), clipping.getHeight(), x, y, x + clipping.getWidth(), y + clipping.getHeight(), null); area.dispose();//from w w w .j a va2 s .com return clipping; }
From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java
private static BufferedImage trim9PBorder(BufferedImage inputImage) { BufferedImage trimedImage = UIUtil.createImage(inputImage.getWidth() - 2, inputImage.getHeight() - 2, BufferedImage.TYPE_INT_ARGB); Graphics2D g = trimedImage.createGraphics(); g.drawImage(inputImage, 0, 0, trimedImage.getWidth(), trimedImage.getHeight(), 1, 1, inputImage.getWidth() - 1, inputImage.getHeight() - 1, null); g.dispose();//ww w .j a v a2 s .c om return trimedImage; }
From source file:com.fluidops.iwb.deepzoom.DZConvert.java
/** * Gets an image containing the tile at the given row and column * for the given image./*from w w w .j a va2 s . c o m*/ * @param img - the input image from whihc the tile is taken * @param row - the tile's row (i.e. y) index * @param col - the tile's column (i.e. x) index */ private static BufferedImage getTile(BufferedImage img, int row, int col) { int x = col * tileSize - (col == 0 ? 0 : tileOverlap); int y = row * tileSize - (row == 0 ? 0 : tileOverlap); int w = tileSize + (col == 0 ? 1 : 2) * tileOverlap; int h = tileSize + (row == 0 ? 1 : 2) * tileOverlap; if (x + w > img.getWidth()) w = img.getWidth() - x; if (y + h > img.getHeight()) h = img.getHeight() - y; if (debugMode) System.out.printf("getTile: row=%d, col=%d, x=%d, y=%d, w=%d, h=%d%n", row, col, x, y, w, h); assert (w > 0); assert (h > 0); BufferedImage result = new BufferedImage(w, h, getType(img)); Graphics2D g = result.createGraphics(); g.drawImage(img, 0, 0, w, h, x, y, x + w, y + h, null); return result; }
From source file:Main.java
/** * Draws the given {@link BufferedImage} to the canvas, centered and cropped to fill the * bounds defined by the destination rectangle, and with preserved aspect ratio. * * @param g The destination canvas. * @param source The source image.//from ww w.ja v a 2 s . c om * @param dstRect The destination rectangle in the destination canvas into which to draw the * image. */ public static void drawCenterCrop(Graphics2D g, BufferedImage source, Rectangle dstRect) { final int srcWidth = source.getWidth(); final int srcHeight = source.getHeight(); if (srcWidth * 1.0 / srcHeight > dstRect.width * 1.0 / dstRect.height) { final int scaledWidth = dstRect.height * srcWidth / srcHeight; final int scaledHeight = dstRect.height; Image scaledImage = scaledImage(source, scaledWidth, scaledHeight); g.drawImage(scaledImage, dstRect.x, dstRect.y, dstRect.x + dstRect.width, dstRect.y + dstRect.height, 0 + (scaledWidth - dstRect.width) / 2, 0, 0 + (scaledWidth - dstRect.width) / 2 + dstRect.width, 0 + dstRect.height, null); } else { final int scaledWidth = dstRect.width; final int scaledHeight = dstRect.width * srcHeight / srcWidth; Image scaledImage = scaledImage(source, scaledWidth, scaledHeight); g.drawImage(scaledImage, dstRect.x, dstRect.y, dstRect.x + dstRect.width, dstRect.y + dstRect.height, 0, 0 + (scaledHeight - dstRect.height) / 2, 0 + dstRect.width, 0 + (scaledHeight - dstRect.height) / 2 + dstRect.height, null); } }
From source file:Main.java
/** * Draws the given {@link BufferedImage} to the canvas, centered, wholly contained within the * bounds defined by the destination rectangle, and with preserved aspect ratio. * * @param g The destination canvas. * @param source The source image.//from w w w. j a v a 2s. c o m * @param dstRect The destination rectangle in the destination canvas into which to draw the * image. */ public static void drawCenterInside(Graphics2D g, BufferedImage source, Rectangle dstRect) { final int srcWidth = source.getWidth(); final int srcHeight = source.getHeight(); if (srcWidth * 1.0 / srcHeight > dstRect.width * 1.0 / dstRect.height) { final int scaledWidth = Math.max(1, dstRect.width); final int scaledHeight = Math.max(1, dstRect.width * srcHeight / srcWidth); Image scaledImage = scaledImage(source, scaledWidth, scaledHeight); g.drawImage(scaledImage, dstRect.x, dstRect.y + (dstRect.height - scaledHeight) / 2, dstRect.x + dstRect.width, dstRect.y + (dstRect.height - scaledHeight) / 2 + scaledHeight, 0, 0, 0 + scaledWidth, 0 + scaledHeight, null); } else { final int scaledWidth = Math.max(1, dstRect.height * srcWidth / srcHeight); final int scaledHeight = Math.max(1, dstRect.height); Image scaledImage = scaledImage(source, scaledWidth, scaledHeight); g.drawImage(scaledImage, dstRect.x + (dstRect.width - scaledWidth) / 2, dstRect.y, dstRect.x + (dstRect.width - scaledWidth) / 2 + scaledWidth, dstRect.y + dstRect.height, 0, 0, 0 + scaledWidth, 0 + scaledHeight, null); } }
From source file:com.fluidops.iwb.deepzoom.DZConvert.java
/** * Returns resized image/*from ww w. j a v a 2 s. co m*/ * NB - useful reference on high quality image resizing can be found here: * http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html * @param width the required width * @param height the frequired height * @param img the image to be resized */ private static BufferedImage resizeImage(BufferedImage img, double width, double height) { int w = (int) width; int h = (int) height; BufferedImage result = new BufferedImage(w, h, getType(img)); Graphics2D g = result.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.drawImage(img, 0, 0, w, h, 0, 0, img.getWidth(), img.getHeight(), null); return result; }
From source file:Main.java
/** * Paints a set of {@link Rectangle} object out of a rendered {@link BufferedImage} * such that the resulting image is transparent except for a minimum bounding * rectangle of the selected elements./*from ww w . jav a 2 s.co m*/ * * @param image the source image * @param rectangles the set of rectangles to copy * @param boundingBox the bounding rectangle of the set of rectangles to copy, can be * computed by {@link ImageUtils#getBoundingRectangle} * @param scale a scale factor to apply to the result, e.g. 0.5 to shrink the * destination down 50%, 1.0 to leave it alone and 2.0 to zoom in by * doubling the image size * @return a rendered image, or null */ public static BufferedImage drawRectangles(BufferedImage image, java.util.List<Rectangle> rectangles, Rectangle boundingBox, double scale) { // This code is not a test. When I implemented image cropping, I first implemented // it for BufferedImages (since it's easier; easy image painting, easy scaling, // easy transparency handling, etc). However, this meant that we would need to // convert the SWT images from the ImageOverlay to BufferedImages, crop and convert // back; not ideal, so I rewrote it in SWT (see SwtUtils). However, I // don't want to throw away the code in case we start keeping BufferedImages rather // than SWT images or need it for other purposes, but rather than place it in the // production codebase I'm leaving this utility here in the associated ImageUtils // test class. It was used like this: // @formatter:off // // BufferedImage wholeImage = SwtUtils.convertToAwt(image); // BufferedImage result = ImageUtils.cropSelection(wholeImage, // rectangles, boundingBox, scale); // e.image = SwtUtils.convertToSwt(image.getDevice(), result, true, // DRAG_TRANSPARENCY); // // @formatter:on if (boundingBox == null) { return null; } int destWidth = (int) (scale * boundingBox.width); int destHeight = (int) (scale * boundingBox.height); BufferedImage dest = new BufferedImage(destWidth, destHeight, image.getType()); Graphics2D g = dest.createGraphics(); for (Rectangle bounds : rectangles) { int dx1 = bounds.x - boundingBox.x; int dy1 = bounds.y - boundingBox.y; int dx2 = dx1 + bounds.width; int dy2 = dy1 + bounds.height; dx1 *= scale; dy1 *= scale; dx2 *= scale; dy2 *= scale; int sx1 = bounds.x; int sy1 = bounds.y; int sx2 = sx1 + bounds.width; int sy2 = sy1 + bounds.height; g.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null); } g.dispose(); return dest; }