/** * Creates a buffered image (of the specified type) from the supplied component. * * @param component the component to draw * @param bounds the area relative to the component where the image will be created. * @param imageType the type of buffered image to draw * @return an image of the component */ public static BufferedImage createImage(final Component component, Rectangle bounds, int imageType) { return createImage(component, bounds, imageType, 1); }
/** * Creates a buffered image of type TYPE_INT_RGB from the supplied component. * * @param component the component to draw * @return an image of the component */ public static BufferedImage createImage(Component component) { return createImage(component, BufferedImage.TYPE_INT_RGB); }
/** * Creates a buffered image (of the specified type) from the supplied component. * * @param component the component to draw * @param imageType the type of buffered image to draw * @return an image of the component */ public static BufferedImage createImage(final Component component, int imageType) { Dimension componentSize = component.getSize(); return createImage(component, new Rectangle(0, 0, componentSize.width, componentSize.height), imageType, 1); }
/** * Paints the component to the supplied output stream * * @param c the component to capture as an image * @param format the format of the image output data, currently "png", "jpg" or "gif" * @param stream the stream to write the image data to */ private static void writeToStream(Component c, String format, OutputStream stream) { BufferedImage img = createImage(c); try { ImageIO.write(img, format, stream); } catch (IOException e) { // could happen if the output format is not supported Logger.getAnonymousLogger().severe(e.getMessage()); } }
/** * Creates a thumbnail from the supplied component (such as a chart). If you want to display the thumbnail as a * component you can pass the created ImageIcon as a parameter to the constructor of a JLabel. * * @param component the component from which we would like to generate a thumbnail * @param width the width of the thumbnail * @param height the height of the thumbnail * @return a thumbnail Image of the supplied component */ public static Image createThumbnailImage(Component component, int width, int height) { BufferedImage image = createImage(component); BufferedImage thumbnailImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = thumbnailImage.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); double w = (double) width / image.getWidth(); double h = (double) height / image.getHeight(); assert w <= 1.0 : "The thumbnail should be smaller than the original"; assert h <= 1.0 : "The thumbnail should be smaller than the original"; AffineTransform transform = AffineTransform.getScaleInstance(w, h); g.drawRenderedImage(image, transform); return thumbnailImage; }
/** * Utility method to create a texture paint from a graphics file * * @param observer the observer to be informed when the texture image has been drawn * @param fileName the name of a file on the classpath, e.g., com/mycompany/project/images/widget.gif * @return a TexturePaint instance */ public static TexturePaint createTexture(JComponent observer, String fileName) { Image image = createImage(fileName); MediaTracker tracker = new MediaTracker(observer); tracker.addImage(image, 1); try { tracker.waitForAll(); } catch (Exception e) { } int w = image.getWidth(observer); int h = image.getHeight(observer); BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D big = bi.createGraphics(); big.drawImage(image, 0, 0, observer); return new TexturePaint(bi, new Rectangle(0, 0, bi.getWidth(), bi.getHeight())); }