List of usage examples for java.awt.image BufferedImage BufferedImage
public BufferedImage(int width, int height, int imageType)
From source file:Main.java
public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint, boolean higherQuality) { int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage ret = img;//w w w . j a v a 2s.co m int w, h; if (higherQuality) { // Use multi-step technique: start with original size, then // scale down in multiple passes with drawImage() // until the target size is reached w = img.getWidth(); h = img.getHeight(); } else { // Use one-step technique: scale directly from original // size to target size with a single drawImage() call w = targetWidth; h = targetHeight; } do { if (higherQuality && w > targetWidth) { w /= 2; if (w < targetWidth) { w = targetWidth; } } if (higherQuality && h > targetHeight) { h /= 2; if (h < targetHeight) { h = targetHeight; } } BufferedImage tmp = new BufferedImage(w, h, type); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint); g2.drawImage(ret, 0, 0, w, h, null); g2.dispose(); ret = tmp; } while (w != targetWidth || h != targetHeight); return ret; }
From source file:Main.java
public static ImageIcon createImage() { BufferedImage image = new BufferedImage(28, 28, BufferedImage.TYPE_INT_ARGB); Graphics g = image.getGraphics(); g.setColor(Color.RED);//w w w. j ava 2s . c om g.fillRect(0, 0, 32, 32); g.dispose(); return new ImageIcon(image); }
From source file:Main.java
/** * Rotates given image by given degrees which should be a multiple of 90 * @param source image to be rotated// www . j a va2 s. com * @param degrees the angle by which to rotate, should be a multiple of 90 * @return the rotated image */ public static BufferedImage rotateByRightAngle(BufferedImage source, int degrees) { assert degrees % 90 == 0; degrees = degrees % 360; int w = source.getWidth(); int h = source.getHeight(); int w1, h1; switch (degrees) { case 90: case 270: w1 = h; h1 = w; break; default: w1 = w; h1 = h; } BufferedImage rotated = new BufferedImage(w1, h1, source.getType()); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { int v = source.getRGB(x, y); int x1, y1; switch (degrees) { case 90: x1 = h - y - 1; y1 = x; break; case 180: x1 = w - x - 1; y1 = h - y - 1; break; case 270: x1 = y; y1 = w - x - 1; break; default: x1 = x; y1 = y; break; } rotated.setRGB(x1, y1, v); } } return rotated; }
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 www . j a v a 2 s . 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:BlurredImage.java
public void paint(Graphics g) { try {//from ww w .j ava2 s. c o m BufferedImage myImage = ImageIO.read(this.getClass().getResource("redrock.png")); BufferedImage filteredImage = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null), BufferedImage.TYPE_BYTE_GRAY); Graphics g1 = filteredImage.getGraphics(); g1.drawImage(myImage, 400, 200, null); float[] blurKernel = { 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f, 1 / 9f }; BufferedImageOp blur = new ConvolveOp(new Kernel(3, 3, blurKernel)); myImage = blur.filter(myImage, null); g1.dispose(); Graphics2D g2d = (Graphics2D) g; g2d.drawImage(myImage, null, 3, 3); } catch (Exception e) { } }
From source file:ImageClip.java
/** * Clips the input image to the specified shape * /* ww w . j ava 2 s. c o m*/ * @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:TexturePaintDemo.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB); Graphics2D big = bi.createGraphics(); big.setColor(Color.blue);/*from ww w .j a v a2 s.c o m*/ big.fillRect(0, 0, 5, 5); big.setColor(Color.lightGray); big.fillOval(0, 0, 5, 5); Rectangle r = new Rectangle(0, 0, 5, 5); g2.setPaint(new TexturePaint(bi, r)); Rectangle rect = new Rectangle(5, 5, 200, 200); g2.fill(rect); }
From source file:Utils.java
public static Shape generateShapeFromText(Font font, String string) { BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = img.createGraphics(); try {//from w w w. j av a2 s. c om GlyphVector vect = font.createGlyphVector(g2.getFontRenderContext(), string); Shape shape = vect.getOutline(0f, (float) -vect.getVisualBounds().getY()); return shape; } finally { g2.dispose(); } }
From source file:MainClass.java
public BufferedImage emboss(BufferedImage src) { int width = src.getWidth(); int height = src.getHeight(); BufferedImage dst;/*from w w w . j a v a 2s .c om*/ dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) { int upperLeft = 0; int lowerRight = 0; if (i > 0 && j > 0) upperLeft = src.getRGB(j - 1, i - 1); if (i < height - 1 && j < width - 1) lowerRight = src.getRGB(j + 1, i + 1); int redDiff = ((lowerRight >> 16) & 255) - ((upperLeft >> 16) & 255); int greenDiff = ((lowerRight >> 8) & 255) - ((upperLeft >> 8) & 255); int blueDiff = (lowerRight & 255) - (upperLeft & 255); int diff = redDiff; if (Math.abs(greenDiff) > Math.abs(diff)) diff = greenDiff; if (Math.abs(blueDiff) > Math.abs(diff)) diff = blueDiff; int grayColor = 128 + diff; if (grayColor > 255) grayColor = 255; else if (grayColor < 0) grayColor = 0; int newColor = (grayColor << 16) + (grayColor << 8) + grayColor; dst.setRGB(j, i, newColor); } return dst; }
From source file:ColorPan.java
public void paint(Graphics g) { int width = getSize().width; int height = getSize().height; int[] data = new int[width * height]; int i = 0;//from w ww .ja v a2 s.c o m for (int y = 0; y < height; y++) { int red = (y * 255) / (height - 1); for (int x = 0; x < width; x++) { int green = (x * 255) / (width - 1); int blue = 128; data[i++] = (red << 16) | (green << 8) | blue; } } BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.setRGB(0, 0, width, height, data, 0, width); g.drawImage(image, 0, 0, this); }