List of usage examples for java.awt Graphics2D drawImage
public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y);
From source file:Main.java
public static void draw(BufferedImage image, Rectangle rectangle, BufferedImage backgroundImg) { Graphics2D g = backgroundImg.createGraphics(); if (rectangle == null) { g.drawImage(image, 0, 0, null); } else {/*from w w w. j a v a 2 s . co m*/ g.drawImage(image, rectangle.x, rectangle.y, null); } g.dispose(); }
From source file:Main.java
/** * Converts a {@link Image} into a {@link BufferedImage}. If the image * is already a {@link BufferedImage} then the same instance is returned. * * @param image the image to convert to a buffered image. * @param imageType the image type to use. * @return the converted image or the same instance if already a * {@link BufferedImage}.//from ww w . ja v a2s. c o m */ public static BufferedImage toBufferedImage(Image image, int imageType) { if (image instanceof BufferedImage) { return (BufferedImage) image; } final BufferedImage buffImage = new BufferedImage(image.getWidth(null), image.getHeight(null), imageType); final Graphics2D gfx = buffImage.createGraphics(); gfx.drawImage(image, 0, 0, null); return buffImage; }
From source file:Main.java
/** * Pads the given {@link BufferedImage} on all sides by the given padding amount. * * @param source The source image.//from w w w .ja va 2 s . c om * @param padding The amount to pad on all sides, in pixels. * @return A new, padded image, or the source image if no padding is performed. */ public static BufferedImage paddedImage(BufferedImage source, int padding) { if (padding == 0) { return source; } BufferedImage newImage = newArgbBufferedImage(source.getWidth() + padding * 2, source.getHeight() + padding * 2); Graphics2D g = (Graphics2D) newImage.getGraphics(); g.drawImage(source, padding, padding, null); return newImage; }
From source file:Main.java
/** * Fills the given {@link BufferedImage} with a {@link Paint}, preserving its alpha channel. * * @param source The source image.// w w w . ja v a 2 s . c o m * @param paint The paint to fill with. * @return A new, painted/filled image. */ public static BufferedImage filledImage(BufferedImage source, Paint paint) { BufferedImage newImage = newArgbBufferedImage(source.getWidth(), source.getHeight()); Graphics2D g = (Graphics2D) newImage.getGraphics(); g.drawImage(source, 0, 0, null); g.setComposite(AlphaComposite.SrcAtop); g.setPaint(paint); g.fillRect(0, 0, source.getWidth(), source.getHeight()); return newImage; }
From source file:Main.java
/** * Changes the opacity of a given image. * @param src The source image.// w ww . ja va 2 s .com * @param opacity The opacity to change to. * @return */ public static BufferedImage imgUtilAdjustImageTransparency(BufferedImage src, float opacity) { if (src == null) return null; AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity); BufferedImage cpy = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = (Graphics2D) cpy.getGraphics(); g.setComposite(ac); g.drawImage(src, 0, 0, null); g.dispose(); return cpy; }
From source file:ImageUtil.java
/** * Creates a scaled copy of the source image. * //from w w w . j a va2 s . co m * @param src * source image to be scaled * @param width * the width for the new scaled image in pixels * @param height * the height for the new scaled image in pixels * @return a copy of the source image scaled to <tt>width</tt> x * <tt>height</tt> pixels. */ public static BufferedImage scaleImage(BufferedImage src, int width, int height) { Image scaled = src.getScaledInstance(width, height, 0); BufferedImage ret = null; /* * ColorModel cm = src.getColorModel(); if (cm instanceof * IndexColorModel) { ret = new BufferedImage( width, height, * src.getType(), (IndexColorModel) cm ); } else { ret = new * BufferedImage( src.getWidth(), src.getHeight(), src.getType() ); } * Graphics2D g = ret.createGraphics(); //clear alpha channel Composite * comp = g.getComposite(); * g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, * 0.0f)); Rectangle2D.Double d = new * Rectangle2D.Double(0,0,ret.getWidth(),ret.getHeight()); g.fill(d); * g.setComposite(comp); */ ret = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = ret.createGraphics(); // copy image g.drawImage(scaled, 0, 0, null); return ret; }
From source file:Main.java
public static BufferedImage asCompatibleImage(Image img, int transparency) { BufferedImage ret = defaultScreenDeviceConfiguration().createCompatibleImage(img.getWidth(null), img.getHeight(null), transparency); Graphics2D gc = ret.createGraphics(); gc.setComposite(AlphaComposite.Src); gc.drawImage(img, 0, 0, null); gc.dispose();//from ww w . j a va2 s.co m return ret; }
From source file:Main.java
public static BufferedImage joinBufferedImage(BufferedImage img1, BufferedImage img2) { int offset = 2; int width = img1.getWidth() + img2.getWidth() + offset; int height = Math.max(img1.getHeight(), img2.getHeight()) + offset; BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = newImage.createGraphics(); Color oldColor = g2.getColor(); g2.setPaint(Color.BLACK);//from ww w .j a v a 2 s .c o m g2.fillRect(0, 0, width, height); g2.setColor(oldColor); g2.drawImage(img1, null, 0, 0); g2.drawImage(img2, null, img1.getWidth() + offset, 0); g2.dispose(); return newImage; }
From source file:Main.java
/** * Creates an outline of an image,//from ww w . j a v a 2 s .c o m * with the default clipping rectangle. * @param src The source image. * @param c The color to outline the image * in. * @return */ public static BufferedImage imgUtilOutline(BufferedImage src, Color c) { if (src == null) return null; BufferedImage b = new BufferedImage(src.getWidth(), src.getHeight(), src.getType()); Graphics2D g = (Graphics2D) b.getGraphics(); g.setColor(c); g.drawRect(1, 1, src.getWidth() - 1, src.getHeight() - 1); g.drawImage(src, 0, 0, null); g.dispose(); return b; }
From source file:Main.java
/** * Snapshots the specified {@link BufferedImage} and stores a copy of * its pixels into a JavaFX {@link Image} object, creating a new * object if needed./*from w w w. j a v a 2 s . c o m*/ * The returned {@code Image} will be a static snapshot of the state * of the pixels in the {@code BufferedImage} at the time the method * completes. Further changes to the {@code BufferedImage} will not * be reflected in the {@code Image}. * <p> * The optional JavaFX {@link WritableImage} parameter may be reused * to store the copy of the pixels. * A new {@code Image} will be created if the supplied object is null, * is too small or of a type which the image pixels cannot be easily * converted into. * * @param bimg the {@code BufferedImage} object to be converted * @param wimg an optional {@code WritableImage} object that can be * used to store the returned pixel data * @return an {@code Image} object representing a snapshot of the * current pixels in the {@code BufferedImage}. * @since JavaFX 2.2 */ public static WritableImage toFXImage(BufferedImage bimg, WritableImage wimg) { int bw = bimg.getWidth(); int bh = bimg.getHeight(); switch (bimg.getType()) { case BufferedImage.TYPE_INT_ARGB: case BufferedImage.TYPE_INT_ARGB_PRE: break; default: BufferedImage converted = new BufferedImage(bw, bh, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2d = converted.createGraphics(); g2d.drawImage(bimg, 0, 0, null); g2d.dispose(); bimg = converted; break; } // assert(bimg.getType == TYPE_INT_ARGB[_PRE]); if (wimg != null) { int iw = (int) wimg.getWidth(); int ih = (int) wimg.getHeight(); if (iw < bw || ih < bh) { wimg = null; } else if (bw < iw || bh < ih) { int empty[] = new int[iw]; PixelWriter pw = wimg.getPixelWriter(); PixelFormat<IntBuffer> pf = PixelFormat.getIntArgbPreInstance(); if (bw < iw) { pw.setPixels(bw, 0, iw - bw, bh, pf, empty, 0, 0); } if (bh < ih) { pw.setPixels(0, bh, iw, ih - bh, pf, empty, 0, 0); } } } if (wimg == null) { wimg = new WritableImage(bw, bh); } PixelWriter pw = wimg.getPixelWriter(); IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster(); int data[] = icr.getDataStorage(); int offset = icr.getDataOffset(0); int scan = icr.getScanlineStride(); PixelFormat<IntBuffer> pf = (bimg.isAlphaPremultiplied() ? PixelFormat.getIntArgbPreInstance() : PixelFormat.getIntArgbInstance()); pw.setPixels(0, 0, bw, bh, pf, data, offset, scan); return wimg; }