📄 imageutils.java
字号:
Color color = c.getBackground(); if (color != null && !(color instanceof ColorUIResource)) { ColorFillFilter filter = new ColorFillFilter(color); BufferedImage buffered = toBufferedImage(image); BufferedImage filtered = new BufferedImage(buffered.getWidth(), buffered.getHeight(), BufferedImage.TYPE_INT_ARGB); filter.filter(buffered, filtered); image = filtered; } switch (paintType) { case PAINT_NORMAL : g.drawImage(image, x, y, c); break; case PAINT_STRETCH : g.drawImage(image, x, y, width, height, c); break; case PAINT_TILE : paintTile(c, g, image, x, y, width, height, alignWithParent); break; case PAINT_CENTERED : g.drawImage(image, (width - image.getWidth(c)) / 2, (height - image.getHeight(c)) / 2, c); break; case PAINT_NONE : break; } } /* * The idea is to paint an image to a buffer of the full size of width/height and * setting everything else but the window to transparent */ public static void paintWindow(Component c, Graphics g, Image image, int x, int y, int width, int height, int windowX, int windowY, int windowWidth, int windowsHeight, boolean alignWithParent, int paintType) { width += x; height += y; BufferedImage windowed = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D graph = windowed.createGraphics(); graph.setClip(0, 0, width, height); switch (paintType) { case PAINT_NORMAL : graph.drawImage(image, x, y, c); break; case PAINT_STRETCH : graph.drawImage(image, x, y, width, height, c); break; case PAINT_TILE : paintTile(c, graph, image, x, y, width, height, alignWithParent); break; case PAINT_CENTERED : graph.drawImage(image, (width - image.getWidth(c)) / 2, (height - image.getHeight(c)) / 2, c); break; case PAINT_NONE : break; } WritableRaster raster = windowed.getAlphaRaster(); //do the left part of the window for (int i = 0; i < windowX; i++) { for (int j = 0; j < height; j++) { raster.setSample(i, j, 0, 0); } } //do the right part of the window for (int i = windowX + windowWidth; i < width; i++) { for (int j = 0; j < height; j++) { raster.setSample(i, j, 0, 0); } } int windowXEnd = windowX + windowWidth; //do the bottom part of the window for (int i = windowX; i < windowXEnd; i++) { for (int j = windowY + windowsHeight; j < height; j++) { raster.setSample(i, j, 0, 0); } } //do the top part of the window for (int i = windowX; i < windowXEnd; i++) { for (int j = 0; j < windowY; j++) { raster.setSample(i, j, 0, 0); } } g.drawImage(windowed, 0, 0, width, height, c); } /** * Description of the Method * * @param component Description of Parameter * @param g Description of Parameter * @param image Description of Parameter * @param x Description of Parameter * @param y Description of Parameter * @param width Description of Parameter * @param height Description of Parameter */ public static void paintTile(Component component, Graphics g, Image image, int x, int y, int width, int height) { paintTile(component, g, image, x, y, width, height, true); } /** * Description of the Method * * @param image Description of Parameter * @return Description of the Returned Value */ public static Image transparent(Image image) { return toBufferedImage(image); } /** * Description of the Method * * @param image Description of Parameter * @param x Description of Parameter * @param y Description of Parameter * @param width Description of Parameter * @param height Description of Parameter * @return Description of the Returned Value */ public static Image grab(Image image, int x, int y, int width, int height) { if (width * height <= 0) { return null; } if (image instanceof BufferedImage) { return ((BufferedImage)image).getSubimage(x, y, width, height); } int[] pixels = new int[width * height]; PixelGrabber grabber = new PixelGrabber(image, x, y, width, height, pixels, 0, width); try { grabber.grabPixels(); } catch (Exception e) { e.printStackTrace(); } int pixel, red, green, blue; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { pixel = pixels[j * width + i]; red = (pixel >> 16) & 0xff; green = (pixel >> 8) & 0xff; blue = (pixel) & 0xff; if ((red == TRANSPARENT_RED) && (green == TRANSPARENT_GREEN) && (blue == TRANSPARENT_BLUE)) { pixels[j * width + i] = TRANSPARENT_PIXEL; } } } Image newImage = producer.createImage(new MemoryImageSource(width, height, pixels, 0, width)); return toBufferedImage(newImage); } /** * Description of the Method * * @param image Description of Parameter * @param factor Description of Parameter * @return Description of the Returned Value */ public static Image buildTile(Image image, int factor) { int width = image.getWidth(producer); int height = image.getHeight(producer); int[] pixels = new int[width * height]; PixelGrabber grabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width); try { grabber.grabPixels(); } catch (Exception e) { e.printStackTrace(); } // do an horizontal tiling int[] zoomed = new int[pixels.length * factor]; for (int i = 0; i < height; i++) { for (int j = 0; j < factor; j++) { System.arraycopy(pixels, width * i, zoomed, (width * factor * i) + width * j, width); } } pixels = zoomed; // do a vertical duplication int[] zoomed2 = new int[pixels.length * factor]; for (int i = 0; i < factor; i++) { System.arraycopy(pixels, 0, zoomed2, i * pixels.length, pixels.length); } return producer.createImage(new MemoryImageSource(width * factor, height * factor, zoomed2, 0, width * factor)); } /** * Description of the Method * * @param image Description of Parameter * @return Description of the Returned Value */ public static BufferedImage toBufferedImage(Image image) { if (image == null) { return null; } if (image instanceof BufferedImage) { return (BufferedImage)image; } // This code ensures that all the pixels in // the image are loaded. image = new ImageIcon(image).getImage(); // Create the buffered image. BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); // Copy image to buffered image. Graphics2D g = bufferedImage.createGraphics(); // paint the image. g.drawImage(image, 0, 0, null); g.dispose(); // Get the DataBuffer from your bufferedImage like DataBuffer db = bufferedImage.getRaster().getDataBuffer(); // then you can just iterate through and convert your chosencolor to a transparent color for (int i = 0, c = db.getSize(); i < c; i++) { if (db.getElem(i) == TRANSPARENT_TO_REMOVE) { // set to transparent db.setElem(i, TRANSPARENT_PIXEL); } } return bufferedImage; } /** * Description of the Method * * @param image Description of Parameter * @return Description of the Returned Value */ public static Image flipHorizontally(Image image) { if (image == null) { return null; } AffineTransform transform = PanelArtistUtilities.getYFlipTransform(image.getHeight(producer)); AffineTransformOp operation = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); return operation.filter((BufferedImage)image, null); } /** * Description of the Method * * @param anImage Description of Parameter * @return Description of the Returned Value */ public static Image rotateLeft(Image anImage) { if (anImage == null) { return null; } AffineTransform transform = PanelArtistUtilities.getCCWRotateTransform(anImage.getWidth(producer), anImage.getHeight(producer)); AffineTransformOp operation = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); return transparent(operation.filter((BufferedImage)anImage, null)); } public static Image rotateRight(Image anImage) { if (anImage == null) { return null; } AffineTransform transform = PanelArtistUtilities.getCWRotateTransform(anImage.getWidth(producer), anImage.getHeight(producer)); AffineTransformOp operation = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); return transparent(operation.filter((BufferedImage)anImage, null)); } /** * Description of the Method * * @param component Description of Parameter * @param g Description of Parameter * @param image Description of Parameter * @param x Description of Parameter * @param y Description of Parameter * @param width Description of Parameter * @param height Description of Parameter * @param alignWithParent Description of Parameter */ private static void paintTile(Component component, Graphics g, Image image, int x, int y, int width, int height, boolean alignWithParent) { if (image == null) { return; } final int dx = image.getWidth(null); final int dy = image.getHeight(null); // skip a 0 size image if (dx <= 0 || dy <= 0) { return; } //work out the offset from (0,0) in the root frame. int xoff = 0; int yoff = 0; Shape oldClip = g.getClip(); if (oldClip.contains(x, y, width, height) || (width <= 0 || height <= 0)) { g.setClip(x, y, width, height); } else if (oldClip.intersects(x, y, width, height)) { Rectangle currentRect = oldClip.getBounds(); g.setClip(SwingUtilities.computeIntersection(x, y, width, height, (Rectangle)currentRect.clone())); } if (alignWithParent) { Component parent = component.getParent(); xoff = component.getLocation().x; yoff = component.getLocation().y; while (parent != null && (parent instanceof javax.swing.JInternalFrame == false)) { //don't want the screen coords of the topmost container... if (parent.getParent() != null) { xoff += parent.getLocation().x; yoff += parent.getLocation().y; } parent = parent.getParent(); } x -= (xoff % dx); y -= (yoff % dy); } int maxX = x + width + dx; int maxY = y + height + dy; for (; x <= maxX; x += dx) { for (int j = y; j <= maxY; j += dy) { g.drawImage(image, x, j, component); } } g.setClip(oldClip); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -