Java Code Examples for java.awt.image.BufferedImage#getSource()

The following examples show how to use java.awt.image.BufferedImage#getSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WindowsLookAndFeel.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 2
Source File: ImageUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Make a color of the image transparent.
 * 
 * @param bufferedImageToProcess the image to extract the color from.
 * @param colorToMakeTransparent the color to make transparent.
 * @return the new image.
 */
public static BufferedImage makeColorTransparent( BufferedImage bufferedImageToProcess, final Color colorToMakeTransparent ) {
    ImageFilter filter = new RGBImageFilter(){
        public int markerRGB = colorToMakeTransparent.getRGB() | 0xFF000000;
        public final int filterRGB( int x, int y, int rgb ) {
            if ((rgb | 0xFF000000) == markerRGB) {
                // Mark the alpha bits as zero - transparent
                return 0x00FFFFFF & rgb;
            } else {
                return rgb;
            }
        }
    };
    ImageProducer ip = new FilteredImageSource(bufferedImageToProcess.getSource(), filter);
    Image image = Toolkit.getDefaultToolkit().createImage(ip);
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
    return bufferedImage;
}
 
Example 3
Source File: WindowsLookAndFeel.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 4
Source File: WindowsLookAndFeel.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 5
Source File: ImageInfoReader.java    From WorldGrower with GNU General Public License v3.0 6 votes vote down vote up
private Image transformColorToTransparency(BufferedImage image, Color color)
{
  ImageFilter filter = new RGBImageFilter()
  {
    public final int filterRGB(int x, int y, int rgb)
    {
    	if (rgb == color.getRGB()) {
    		return new Color(0, 0, 0, 0).getRGB();
    	} else {
    		return rgb;
    	}
    }
  };

  ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
  return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 6
Source File: WindowsLookAndFeel.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 7
Source File: WindowsLookAndFeel.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 8
Source File: WindowsLookAndFeel.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 9
Source File: WindowsLookAndFeel.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 10
Source File: WindowsLookAndFeel.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 11
Source File: WindowsLookAndFeel.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 12
Source File: WindowsLookAndFeel.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 13
Source File: WindowsLookAndFeel.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 14
Source File: WindowsLookAndFeel.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 15
Source File: WindowsLookAndFeel.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 16
Source File: WindowsLookAndFeel.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @since 1.6
 */
public Icon getDisabledIcon(JComponent component, Icon icon) {
    // if the component has a HI_RES_DISABLED_ICON_CLIENT_KEY
    // client property set to Boolean.TRUE, then use the new
    // hi res algorithm for creating the disabled icon (used
    // in particular by the WindowsFileChooserUI class)
    if (icon != null
            && component != null
            && Boolean.TRUE.equals(component.getClientProperty(HI_RES_DISABLED_ICON_CLIENT_KEY))
            && icon.getIconWidth() > 0
            && icon.getIconHeight() > 0) {
        BufferedImage img = new BufferedImage(icon.getIconWidth(),
                icon.getIconWidth(), BufferedImage.TYPE_INT_ARGB);
        icon.paintIcon(component, img.getGraphics(), 0, 0);
        ImageFilter filter = new RGBGrayFilter();
        ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
        Image resultImage = component.createImage(producer);
        return new ImageIconUIResource(resultImage);
    }
    return super.getDisabledIcon(component, icon);
}
 
Example 17
Source File: ImageUtils.java    From markdown-image-kit with MIT License 6 votes vote down vote up
/**
 * http://stackoverflow.com/questions/665406/how-to-make-a-color-transparent-in-a-bufferedimage-and-save-as-png
 *
 * @param image the image
 * @return the image
 */
public static Image whiteToTransparent(@NotNull BufferedImage image) {
    ImageFilter filter = new RGBImageFilter() {
        int markerRGB = JBColor.WHITE.getRGB() | 0xFF000000;

        @Override
        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | 0xFF000000) == markerRGB) {
                // Mark the alpha bits as zero - transparent
                return 0x00FFFFFF & rgb;
            } else {
                // nothing to do
                return rgb;
            }
        }
    };

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 18
Source File: GlobalUtil.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Make a color of a image transparent
 *
 * @param im The image
 * @param color The color
 * @return Result image
 */
public static Image makeColorTransparent(BufferedImage im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {
        // the color we are looking for... Alpha bits are set to opaque
        public int markerRGB = color.getRGB() | 0xFF000000;

        @Override
        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | 0xFF000000) == markerRGB) {
                // Mark the alpha bits as zero - transparent
                return 0x00FFFFFF & rgb;
            } else {
                // nothing to do
                return rgb;
            }
        }
    };

    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 19
Source File: InvertingImageComparator.java    From java-image-processing-survival-guide with Apache License 2.0 5 votes vote down vote up
/**
 * Make provided image transparent wherever color matches the provided color.
 *
 * @param im BufferedImage whose color will be made transparent.
 * @param color Color in provided image which will be made transparent.
 * @return Image with transparency applied.
 */
public static BufferedImage createTransparentImage(final BufferedImage im, final Color color) {

    final ImageFilter filter = new RGBImageFilter() {
        // the color we are looking for (white)... Alpha bits are set to opaque
        public int markerRGB = color.getRGB() | 0xFFFFFFFF;

        public final int filterRGB(final int x, final int y, final int rgb) {
            if ((rgb | 0xFF000000) == markerRGB) {
                // Mark the alpha bits as zero - transparent
                return 0x00FFFFFF & rgb;
            } else {
                // nothing to do
                return rgb;
            }
        }
    };

    final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    final Image image = Toolkit.getDefaultToolkit().createImage(ip);

    final BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
    return bufferedImage;
}
 
Example 20
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private static BufferedImage makeFilteredImage(BufferedImage src, ImageFilter filter) {
  ImageProducer ip = src.getSource();
  Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(ip, filter));
  int w = img.getWidth(null);
  int h = img.getHeight(null);
  BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  Graphics g = bi.createGraphics();
  g.drawImage(img, 0, 0, null);
  g.dispose();
  return bi;
}