下面列出了怎么用java.awt.image.ImageProducer的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* {@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);
}
/**
* {@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);
}
/**
* {@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);
}
/**
* {@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);
}
/**
* {@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);
}
/**
* {@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);
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
protected Icon createDisabledIcon( Icon icon ) {
if( !(icon instanceof ImageIcon) )
return null;
Image image = ((ImageIcon) icon).getImage();
ImageProducer producer = new FilteredImageSource( image.getSource(), filter );
Image disabledImage = Toolkit.getDefaultToolkit().createImage( producer );
return new ImageIcon( disabledImage );
}
public TileConsumer(final ImageProducer producer, final int y,
final int h)
{
this(producer);
this.yy = y;
this.hh = h;
}
/**
* Construct an image from an ImageProducer object.
*/
public ToolkitImage(ImageProducer is) {
source = is;
if (is instanceof InputStreamImageSource) {
src = (InputStreamImageSource) is;
}
}
/**
* Construct an image from an ImageProducer object.
*/
public ToolkitImage(ImageProducer is) {
source = is;
if (is instanceof InputStreamImageSource) {
src = (InputStreamImageSource) is;
}
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
/**
* 分割指定图像为image[]
*
* @param image
* @param row
* @param col
* @return
*/
public static Image[][] getSplit2Images(Image image, int row, int col, boolean isFiltrate) {
int wlength = image.getWidth(null) / row;
int hlength = image.getHeight(null) / col;
Image[][] abufferedimage = new Image[wlength][hlength];
for (int y = 0; y < hlength; y++) {
for (int x = 0; x < wlength; x++) {
abufferedimage[x][y] = GraphicsUtils.createImage(row, col, true);
Graphics g = abufferedimage[x][y].getGraphics();
g.drawImage(image, 0, 0, row, col, (x * row), (y * col), row + (x * row), col + (y * col), null);
g.dispose();
g = null;
PixelGrabber pgr = new PixelGrabber(abufferedimage[x][y], 0, 0, -1, -1, true);
try {
pgr.grabPixels();
} catch (InterruptedException ex) {
ex.getStackTrace();
}
int pixels[] = (int[]) pgr.getPixels();
if (isFiltrate) {
for (int i = 0; i < pixels.length; i++) {
int[] rgbs = LColor.getRGBs(pixels[i]);
if ((rgbs[0] == 247 && rgbs[1] == 0 && rgbs[2] == 255)
|| (rgbs[0] == 255 && rgbs[1] == 0 && rgbs[2] == 255)
|| (rgbs[0] == 0 && rgbs[1] == 0 && rgbs[2] == 0)) {
pixels[i] = 0;
}
}
}
ImageProducer ip = new MemoryImageSource(pgr.getWidth(), pgr.getHeight(), pixels, 0, pgr.getWidth());
abufferedimage[x][y] = toolKit.createImage(ip);
}
}
return abufferedimage;
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
/**
* Construct an image from an ImageProducer object.
*/
public ToolkitImage(ImageProducer is) {
source = is;
if (is instanceof InputStreamImageSource) {
src = (InputStreamImageSource) is;
}
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
/**
* Construct an image from an ImageProducer object.
*/
public ToolkitImage(ImageProducer is) {
source = is;
if (is instanceof InputStreamImageSource) {
src = (InputStreamImageSource) is;
}
}
synchronized private JFrame newJFrame(final String title) {
final JFrame frame = new JFrame(title);
if (null == icon) {
try {
Field mic = ImageJ.class.getDeclaredField("iconPath");
mic.setAccessible(true);
String path = (String) mic.get(IJ.getInstance());
icon = IJ.getInstance().createImage((ImageProducer) new URL("file:" + path).getContent());
} catch (Exception e) {}
}
if (null != icon) frame.setIconImage(icon);
return frame;
}
/**
* Construct an image from an ImageProducer object.
*/
public ToolkitImage(ImageProducer is) {
source = is;
if (is instanceof InputStreamImageSource) {
src = (InputStreamImageSource) is;
}
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
/**
* Returns a <code>GenericImageSinglePassIterator</code> that is either a
* <code>IntPixelIterator</code> or a <code>BytePixelIterator</code>.
*
* @param image
* the image to iterate over.
* @param iteratorType
* one of these 8 BufferedImage types: TYPE_INT_ARGB,
* TYPE_INT_ARGB_PRE, TYPE_INT_RGB, TYPE_INT_BGR, TYPE_3BYTE_BGR,
* TYPE_BYTE_GRAY, TYPE_4BYTE_ABGR, TYPE_4BYTE_ABGR_PRE.
* @return a <code>GenericImageSinglePassIterator</code> for the image
* provided.
*/
public static GenericImageSinglePassIterator get(Image image,
int iteratorType) {
if (!(iteratorType == BufferedImage.TYPE_INT_ARGB
|| iteratorType == BufferedImage.TYPE_INT_ARGB_PRE
|| iteratorType == BufferedImage.TYPE_INT_RGB
|| iteratorType == BufferedImage.TYPE_INT_BGR
|| iteratorType == BufferedImage.TYPE_3BYTE_BGR
|| iteratorType == BufferedImage.TYPE_BYTE_GRAY
|| iteratorType == BufferedImage.TYPE_4BYTE_ABGR || iteratorType == BufferedImage.TYPE_4BYTE_ABGR_PRE)) {
throw new IllegalArgumentException("illegal iterator type: "
+ iteratorType);
}
final ImageProducer producer = image.getSource();
final Consumer consumer = new Consumer(producer, iteratorType);
// ImageProducer.startProduction often starts its own thread, but it's
// not
// required to. Sometimes in my testing a BufferedImage would make
// this a blocking call. So to be safe this call should be in its
// own thread:
Thread productionThread = new Thread(
"GenericImageSinglePassIterator: Production Thread") {
@Override
public void run() {
producer.startProduction(consumer);
}
};
productionThread.start();
return consumer.getPixelIterator();
}
/**
* This is a utility method to help in loading icon images.
* It takes the name of a resource file associated with the
* current object's class file and loads an image object
* from that file. Typically images will be GIFs.
* <p>
* @param resourceName A pathname relative to the directory
* holding the class file of the current class. For example,
* "wombat.gif".
* @return an image object. May be null if the load failed.
*/
public Image loadImage(final String resourceName) {
try {
final URL url = getClass().getResource(resourceName);
if (url != null) {
final ImageProducer ip = (ImageProducer) url.getContent();
if (ip != null) {
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
} catch (final Exception ignored) {
}
return null;
}
public ImageProducer getSource() {
if (src != null) {
src.checkSecurity(null, false);
}
return source;
}
@Override
public Image createImage(final ImageProducer producer) {
return null;
}
public ImageProducer getSource() {
if (osis == null) {
osis = new OffScreenImageSource(this);
}
return osis;
}
public EmptyFilteredImage(ImageProducer imgSource) {
filter = new ImageFilter();
producer = new FilteredImageSource(imgSource, filter);
}
protected ListItem(String title, String iconfile) {
this.nicon = new ImageIcon(getClass().getResource(iconfile));
ImageProducer ip = new FilteredImageSource(nicon.getImage().getSource(), new SelectedImageFilter());
this.sicon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(ip));
this.title = title;
}
public ImageProducer getSource() {
if (src != null) {
src.checkSecurity(null, false);
}
return source;
}
protected ListItem(String title, String iconfile) {
this.nicon = new ImageIcon(getClass().getResource(iconfile));
ImageProducer ip = new FilteredImageSource(nicon.getImage().getSource(), new SelectedImageFilter());
this.sicon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(ip));
this.title = title;
}