下面列出了怎么用java.awt.image.RGBImageFilter的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
/**
* 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);
}
private static ToolTipImage loadImageInternal(String resource, boolean localized) {
// Avoid a NPE that could previously occur in the isDarkLaF case only. See NETBEANS-2401.
if (resource == null) {
return null;
}
ToolTipImage image = null;
if( isDarkLaF() ) {
image = getIcon(addDarkSuffix(resource), localized);
// found an image with _dark-suffix, so there no need to apply an
// image filter to make it look nice using dark themes
}
if (null == image) {
image = getIcon(resource, localized);
// only non _dark images need filtering
RGBImageFilter imageFilter = getImageIconFilter();
if (null != image && null != imageFilter) {
image = icon2ToolTipImage(FilteredIcon.create(imageFilter, image), image.url);
}
}
return image;
}
public static Image makeColorTransparent(Image im, final Color color) {
//(C)
//Copiado da internet: 13/02/2011 - http://www.rgagnon.com/javadetails/java-0265.html e http://www.coderanch.com/t/331731/GUI/java/Resize-ImageIcon
//
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);
}
public static BufferedImage makeColorTransparent(BufferedImage im, final Color color) {
ImageFilter filter = new RGBImageFilter() {
public int markerRGB = color.getRGB() | 0xFF000000;
public final int filterRGB(int x, int y, int rgb) {
if ((rgb | 0xFF000000) == markerRGB) {
return 0x00FFFFFF & rgb;
} else {
return rgb;
}
}
};
return imageToBufferedImage(
Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(im.getSource(), filter)));
}
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);
}
/**
* 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;
}
@Override
public void paintIcon( Component c, Graphics g, int x, int y ) {
update();
Rectangle clipBounds = g.getClipBounds();
if( clipBounds != null && !clipBounds.intersects( new Rectangle( x, y, getIconWidth(), getIconHeight() ) ) )
return;
// get gray filter
RGBImageFilter grayFilter = null;
if( c != null && !c.isEnabled() ) {
Object grayFilterObj = UIManager.get( "Component.grayFilter" );
grayFilter = (grayFilterObj instanceof RGBImageFilter)
? (RGBImageFilter) grayFilterObj
: GrayFilter.createDisabledIconFilter( dark );
}
Graphics2D g2 = new GraphicsFilter( (Graphics2D) g.create(), ColorFilter.getInstance(), grayFilter );
try {
FlatUIUtils.setRenderingHints( g2 );
g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );
paintSvg( g2, x, y );
} finally {
g2.dispose();
}
}
private void basicLafChanged() {
boolean brighter = basicLafBrighterCheckBox.isSelected();
int percent = basicLafPercentSlider.getValue();
basicLafPercentValue.setText( String.valueOf( percent ) );
RGBImageFilter filter = new GrayFilter( brighter, percent );
updateFilter( basicLafToolBar, 2, filter );
}
private void metalLafChanged() {
int min = metalLafMinSlider.getValue();
int max = metalLafMaxSlider.getValue();
metalLafMinValue.setText( String.valueOf( min ) );
metalLafMaxValue.setText( String.valueOf( max ) );
RGBImageFilter filter = new MetalDisabledButtonImageFilter( min, max );
updateFilter( metalLafToolBar, 3, filter );
}
private void changed() {
int brightness = intellijBrightnessSlider.getValue();
int contrast = intellijContrastSlider.getValue();
intellijBrightnessValue.setText( String.valueOf( brightness ) );
intellijContrastValue.setText( String.valueOf( contrast ) );
RGBImageFilter filter = new IntelliJGrayFilter( brightness, contrast, 100 );
firePropertyChange( "filter", null, filter );
}
private FilteredIcon(RGBImageFilter filter, Icon delegate) {
super(delegate.getIconWidth(), delegate.getIconHeight());
Parameters.notNull("filter", filter);
Parameters.notNull("delegate", delegate);
this.filter = filter;
this.delegate = delegate;
}
public static Icon create(RGBImageFilter filter, Icon delegate) {
final int width = delegate.getIconWidth();
final int height = delegate.getIconHeight();
if (width < 0 || height < 0) {
/* This case was once observed in NETBEANS-3671. I'm not sure where the offending Icon
came from. Log some more information for future debugging, and fall back gracefully. */
Object url = ImageUtilities.icon2Image(delegate).getProperty("url", null);
LOG.log(Level.WARNING,
"NETBEANS-3671: FilteredIcon.create got {0} of invalid dimensions {1}x{2} with URL={3}",
new Object[] { delegate.getClass().getName(), width, height, url });
return delegate;
}
return new FilteredIcon(filter, delegate);
}
private static Image filterImage( Image image ) {
if( null == image )
return image;
Object obj = UIManager.get("nb.imageicon.filter");
if( obj instanceof RGBImageFilter && null != image ) {
RGBImageFilter imageIconFilter = ( RGBImageFilter ) obj;
image = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource( image.getSource(), imageIconFilter ) );
}
return image;
}
public static void main(String args[]) {
new RGBImageFilter() {
public int filterRGB(int x, int y, int rgb) {
return 0;
}
}.clone();
}
public void applyTransparency( final UIColor background ){
ImageFilter filter = new RGBImageFilter() {
public int markerRGB = (((AWTColor)background).getHandle().getRGB() | 0xFF000000);
public final int filterRGB(int x, int y, int rgb) {
if ( ( rgb | 0xFF000000 ) == markerRGB ) {
return 0x00FFFFFF & rgb;
}
return rgb;
}
};
this.handle = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(this.handle.getSource(), filter));
}
/**
* 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;
}
private Image makeColorTransparent(Image image, Color color) {
final int markerRGB = color.getRGB() | 0xFF000000;
ImageFilter filter = new RGBImageFilter() {
@Override
public int filterRGB(int x, int y, int rgb) {
if ((rgb | 0xFF000000) == markerRGB) {
return 0x00FFFFFF & rgb; // set alpha to 0
}
return rgb;
}
};
return ImageUtil.filter(image, filter);
}
/**
* Creates new icon with the filter applied.
*/
@Nullable
public static Icon filterIcon(@Nonnull Icon icon, RGBImageFilter filter, @Nullable Component ancestor) {
if (icon instanceof DesktopLazyImageImpl) icon = ((DesktopLazyImageImpl)icon).getOrComputeIcon();
if (icon == null) return null;
if (!isGoodSize(icon)) {
LOG.error(icon); // # 22481
return EMPTY_ICON;
}
if (icon instanceof CachedImageIcon) {
icon = ((CachedImageIcon)icon).asDisabledIcon();
}
else {
final float scale;
if (icon instanceof JBUI.ScaleContextAware) {
scale = (float)((JBUI.ScaleContextAware)icon).getScale(SYS_SCALE);
}
else {
scale = UIUtil.isJreHiDPI() ? JBUI.sysScale(ancestor) : 1f;
}
@SuppressWarnings("UndesirableClassUsage") BufferedImage image = new BufferedImage((int)(scale * icon.getIconWidth()), (int)(scale * icon.getIconHeight()), BufferedImage.TYPE_INT_ARGB);
final Graphics2D graphics = image.createGraphics();
graphics.setColor(UIUtil.TRANSPARENT_COLOR);
graphics.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
graphics.scale(scale, scale);
icon.paintIcon(LabelHolder.ourFakeComponent, graphics, 0, 0);
graphics.dispose();
Image img = ImageUtil.filter(image, filter);
if (UIUtil.isJreHiDPI()) img = RetinaImage.createFrom(img, scale, null);
icon = new JBImageIcon(img);
}
return icon;
}
public GraphicsFilter( Graphics2D delegate, ColorFilter colorFilter, RGBImageFilter grayFilter ) {
super( delegate );
this.colorFilter = colorFilter;
this.grayFilter = grayFilter;
}
private void intelliJTextFilterChanged(PropertyChangeEvent e) {
updateFilter( intellijTextToolBar, 5, (RGBImageFilter) e.getNewValue() );
}
private void intelliJLightFilterChanged(PropertyChangeEvent e) {
updateFilter( intellijLightToolBar, 6, (RGBImageFilter) e.getNewValue() );
}
private void intelliJDarkFilterChanged(PropertyChangeEvent e) {
updateFilter( intellijDarkToolBar, 7, (RGBImageFilter) e.getNewValue() );
}
private void updateFilter( JToolBar toolBar, int zipIndex, RGBImageFilter filter ) {
for( Component c : toolBar.getComponents() )
((FilterButton)c).setFilter( filter );
((FilterButton)zipToolBar.getComponent( zipIndex )).setFilter( filter );
}
FilterButton( RGBImageFilter filter, Icon icon ) {
this.filter = filter;
setEnabled( false );
setIcon( icon );
}
void setFilter( RGBImageFilter filter ) {
this.filter = filter;
updateDisabledIcon();
}