下面列出了怎么用java.awt.image.ImageObserver的API类实例代码及写法,或者点击链接到github查看源代码。
synchronized void abort() {
image.getSource().removeConsumer(this);
consuming = false;
newbits = null;
bimage = null;
biRaster = null;
cmodel = null;
srcLUT = null;
isDefaultBI = false;
isSameCM = false;
newInfo(image, ImageObserver.ABORT, -1, -1, -1, -1);
availinfo &= ~(ImageObserver.SOMEBITS
| ImageObserver.FRAMEBITS
| ImageObserver.ALLBITS
| ImageObserver.ERROR);
}
/**
* Combines two images into one
*
* @param image1
* left image
* @param image2
* right image
* @return combined Image
*/
public static Image combine(ImageIcon image1, ImageIcon image2) {
ImageObserver comp = new JComponent() {
private static final long serialVersionUID = 1L;
};
int w = image1.getIconWidth() + image2.getIconWidth();
int h = Math.max(image1.getIconHeight(), image2.getIconHeight());
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.drawImage(image1.getImage(), 0, 0, comp);
g2.drawImage(image2.getImage(), image1.getIconWidth(), 0, comp);
g2.dispose();
return image;
}
public void setDimensions(int w, int h) {
if (src != null) {
src.checkSecurity(null, false);
}
image.setDimensions(w, h);
newInfo(image, (ImageObserver.WIDTH | ImageObserver.HEIGHT),
0, 0, w, h);
if (w <= 0 || h <= 0) {
imageComplete(ImageConsumer.IMAGEERROR);
return;
}
if (width != w || height != h) {
// dimension mismatch => trigger recreation of the buffer
bimage = null;
}
width = w;
height = h;
availinfo |= ImageObserver.WIDTH | ImageObserver.HEIGHT;
}
public synchronized void reconstruct(int flags) {
if (src != null) {
src.checkSecurity(null, false);
}
int missinginfo = flags & ~availinfo;
if ((availinfo & ImageObserver.ERROR) == 0 && missinginfo != 0) {
numWaiters++;
try {
startProduction();
missinginfo = flags & ~availinfo;
while ((availinfo & ImageObserver.ERROR) == 0 &&
missinginfo != 0)
{
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
missinginfo = flags & ~availinfo;
}
} finally {
decrementWaiters();
}
}
}
/**
* Return a property of the image by name. Individual property names
* are defined by the various image formats. If a property is not
* defined for a particular image, then this method will return the
* UndefinedProperty object. If the properties for this image are
* not yet known, then this method will return null and the ImageObserver
* object will be notified later. The property name "comment" should
* be used to store an optional comment which can be presented to
* the user as a description of the image, its source, or its author.
*/
public Object getProperty(String name, ImageObserver observer) {
if (name == null) {
throw new NullPointerException("null property name is not allowed");
}
if (src != null) {
src.checkSecurity(null, false);
}
if (properties == null) {
addWatcher(observer, true);
if (properties == null) {
return null;
}
}
Object o = properties.get(name);
if (o == null) {
o = Image.UndefinedProperty;
}
return o;
}
public boolean scaleImage(SunGraphics2D sg, Image img,
int x, int y,
int width, int height,
Color bgColor,
ImageObserver observer) {
if (!(img instanceof ToolkitImage)) {
return scaleImage(sg, img, x, y, width, height, bgColor);
} else {
ToolkitImage sunimg = (ToolkitImage)img;
if (!imageReady(sunimg, observer)) {
return false;
}
ImageRepresentation ir = sunimg.getImageRep();
return ir.drawToBufImage(sg, sunimg, x, y, width, height, bgColor,
observer);
}
}
private boolean scaleImage(Image img, int dx1, int dy1, int dx2, int dy2,
int sx1, int sy1, int sx2, int sy2,
Color bgcolor, ImageObserver observer)
{
try {
return imagepipe.scaleImage(this, img, dx1, dy1, dx2, dy2, sx1, sy1,
sx2, sy2, bgcolor, observer);
} catch (InvalidPipeException e) {
try {
revalidateAll();
return imagepipe.scaleImage(this, img, dx1, dy1, dx2, dy2, sx1,
sy1, sx2, sy2, bgcolor, observer);
} catch (InvalidPipeException e2) {
// Still catching the exception; we are not yet ready to
// validate the surfaceData correctly. Fail for now and
// try again next time around.
return false;
}
} finally {
surfaceData.markDirty();
}
}
/**
* Draws an image, applying a transform from image space into user space
* before drawing.
* The transformation from user space into device space is done with
* the current transform in the Graphics2D.
* The given transformation is applied to the image before the
* transform attribute in the Graphics2D state is applied.
* The rendering attributes applied include the clip, transform,
* and composite attributes. Note that the result is
* undefined, if the given transform is noninvertible.
* @param img The image to be drawn.
* @param xform The transformation from image space into user space.
* @param obs The image observer to be notified as more of the image
* is converted.
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
*/
public boolean drawImage(Image img,
AffineTransform xform,
ImageObserver obs) {
if (img == null) {
return true;
}
mDrawingArea.addInfinite();
mPrintMetrics.drawImage(this, img);
return mGraphics.drawImage(img, xform, obs);
// if (mDrawingArea[0] != null) {
// Rectangle2D.Double bbox = new Rectangle2D.Double();
// Point2D leftTop = new Point2D.Double(0, 0);
// Point2D rightBottom = new Point2D.Double(getImageWidth(img),
// getImageHeight(img));
// xform.transform(leftTop, leftTop);
// xform.transform(rightBottom, rightBottom);
// bbox.setBoundsFromDiagonal(leftTop, rightBottom);
// addDrawingRect(bbox);
// }
}
/**
* Return the width of the original image source.
* If the width isn't known, then the ImageObserver object will be
* notified when the data is available.
*/
public synchronized int getWidth(ImageObserver iw) {
if (src != null) {
src.checkSecurity(null, false);
}
if ((availinfo & ImageObserver.WIDTH) == 0) {
addWatcher(iw, true);
if ((availinfo & ImageObserver.WIDTH) == 0) {
return -1;
}
}
return width;
}
public void preload(ImageObserver iw) {
if (src != null) {
src.checkSecurity(null, false);
}
if ((availinfo & ImageObserver.ALLBITS) == 0) {
addWatcher(iw, true);
}
}
public int check(ImageObserver iw) {
if (src != null) {
src.checkSecurity(null, false);
}
if ((availinfo & ImageObserver.ERROR) == 0 &&
((~availinfo) & (ImageObserver.WIDTH |
ImageObserver.HEIGHT |
ImageObserver.PROPERTIES)) != 0) {
addWatcher(iw, false);
}
return availinfo;
}
/**
* Return the width of the original image source.
* If the width isn't known, then the image is reconstructed.
*/
public int getWidth() {
if (src != null) {
src.checkSecurity(null, false);
}
if ((availinfo & ImageObserver.WIDTH) == 0) {
reconstruct(ImageObserver.WIDTH);
}
return width;
}
public boolean copyImage(SunGraphics2D sg, Image img,
int x, int y,
Color bgColor,
ImageObserver observer) {
if (!(img instanceof ToolkitImage)) {
return copyImage(sg, img, x, y, bgColor);
} else {
ToolkitImage sunimg = (ToolkitImage)img;
if (!imageReady(sunimg, observer)) {
return false;
}
ImageRepresentation ir = sunimg.getImageRep();
return ir.drawToBufImage(sg, sunimg, x, y, bgColor, observer);
}
}
synchronized void dispose() {
image.getSource().removeConsumer(this);
consuming = false;
newbits = null;
availinfo &= ~(ImageObserver.SOMEBITS
| ImageObserver.FRAMEBITS
| ImageObserver.ALLBITS);
}
/**
* Return the width of the original image source.
* If the width isn't known, then the image is reconstructed.
*/
public int getWidth() {
if (src != null) {
src.checkSecurity(null, false);
}
if ((availinfo & ImageObserver.WIDTH) == 0) {
reconstruct(ImageObserver.WIDTH);
}
return width;
}
public boolean drawToBufImage(Graphics g, ToolkitImage img,
int x, int y, Color bg,
ImageObserver iw) {
if (src != null) {
src.checkSecurity(null, false);
}
if ((availinfo & ImageObserver.ERROR) != 0) {
if (iw != null) {
iw.imageUpdate(image, ImageObserver.ERROR|ImageObserver.ABORT,
-1, -1, -1, -1);
}
return false;
}
boolean done = ((availinfo & ImageObserver.ALLBITS) != 0);
boolean abort = ((availinfo & ImageObserver.ABORT) != 0);
if (!done && !abort) {
addWatcher(iw);
startProduction();
// Some producers deliver image data synchronously
done = ((availinfo & ImageObserver.ALLBITS) != 0);
}
if (done || (0 != (availinfo & ImageObserver.FRAMEBITS))) {
g.drawImage (bimage, x, y, bg, null);
}
return done;
}
public boolean copyImage(SunGraphics2D sg, Image img,
int dx, int dy, int sx, int sy, int w, int h,
Color bgColor,
ImageObserver observer) {
if (validate(sg)) {
return sg.imagepipe.copyImage(sg, img, dx, dy, sx, sy, w, h,
bgColor, observer);
} else {
return false;
}
}
public boolean drawToBufImage(Graphics g, ToolkitImage img,
int x, int y, Color bg,
ImageObserver iw) {
if (src != null) {
src.checkSecurity(null, false);
}
if ((availinfo & ImageObserver.ERROR) != 0) {
if (iw != null) {
iw.imageUpdate(image, ImageObserver.ERROR|ImageObserver.ABORT,
-1, -1, -1, -1);
}
return false;
}
boolean done = ((availinfo & ImageObserver.ALLBITS) != 0);
boolean abort = ((availinfo & ImageObserver.ABORT) != 0);
if (!done && !abort) {
addWatcher(iw);
startProduction();
// Some producers deliver image data synchronously
done = ((availinfo & ImageObserver.ALLBITS) != 0);
}
if (done || (0 != (availinfo & ImageObserver.FRAMEBITS))) {
g.drawImage (bimage, x, y, bg, null);
}
return done;
}
/**
* Return the height of the original image source.
* If the height isn't known, then the image is reconstructed.
*/
public int getHeight() {
if (src != null) {
src.checkSecurity(null, false);
}
if ((availinfo & ImageObserver.HEIGHT) == 0) {
reconstruct(ImageObserver.HEIGHT);
}
return height;
}
/** Calls another <code>drawImage</code> method */
@Override
public boolean drawImage(Image i, int x, int y, Color bgColor,
ImageObserver obs) {
if (i instanceof BufferedImage) {
BufferedImage bi = (BufferedImage) i;
return drawImage(bi, x, y, x + bi.getWidth(), y + bi.getHeight(),
bgColor, obs);
}
loadImage(i);
return drawImage(i, x, y, x + i.getWidth(null), y + i.getHeight(null),
bgColor, obs);
}
public boolean drawImage(Image image, int x, int y, int width, int height, ImageObserver imageObserver) {
if (currentItem != null) {
rectangle.setRect(x, y, width, height);
testForHit(rectangle, false);
}
return true;
}
private synchronized void addWatcher(ImageObserver iw, boolean load) {
if ((availinfo & ImageObserver.ERROR) != 0) {
if (iw != null) {
iw.imageUpdate(this, ImageObserver.ERROR|ImageObserver.ABORT,
-1, -1, -1, -1);
}
return;
}
ImageRepresentation ir = getImageRep();
ir.addWatcher(iw);
if (load) {
ir.startProduction();
}
}
public boolean copyImage(SunGraphics2D sg, Image img,
int x, int y,
Color bgColor,
ImageObserver observer) {
if (validate(sg)) {
return sg.imagepipe.copyImage(sg, img, x, y, bgColor, observer);
} else {
return false;
}
}
public void imageComplete(int status) {
if (src != null) {
src.checkSecurity(null, false);
}
boolean done;
int info;
switch (status) {
default:
case ImageConsumer.IMAGEABORTED:
done = true;
info = ImageObserver.ABORT;
break;
case ImageConsumer.IMAGEERROR:
image.addInfo(ImageObserver.ERROR);
done = true;
info = ImageObserver.ERROR;
dispose();
break;
case ImageConsumer.STATICIMAGEDONE:
done = true;
info = ImageObserver.ALLBITS;
break;
case ImageConsumer.SINGLEFRAMEDONE:
done = false;
info = ImageObserver.FRAMEBITS;
break;
}
synchronized (this) {
if (done) {
image.getSource().removeConsumer(this);
consuming = false;
newbits = null;
if (bimage != null) {
bimage = getOpaqueRGBImage();
}
}
availinfo |= info;
notifyAll();
}
newInfo(image, info, 0, 0, width, height);
image.infoDone(status);
}
void setDimensions(int w, int h) {
width = w;
height = h;
addInfo(ImageObserver.WIDTH | ImageObserver.HEIGHT);
}
/**
* Draws an image scaled to x,y,w,h in nonblocking mode with a
* callback object.
*/
public boolean drawImage(Image img, int x, int y, int width, int height,
ImageObserver observer) {
return drawImage(img, x, y, width, height, null, observer);
}
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
if (img instanceof BufferedImage) {
this.image = (BufferedImage) img;
}
return false; // TODO: check
}
public static ImageObserver getResolutionVariantObserver(
final Image image, final ImageObserver observer,
final int imgWidth, final int imgHeight,
final int rvWidth, final int rvHeight, boolean concatenateInfo) {
if (observer == null) {
return null;
}
synchronized (ObserverCache.INSTANCE) {
ImageObserver o = (ImageObserver) ObserverCache.INSTANCE.get(observer);
if (o == null) {
o = (Image resolutionVariant, int flags,
int x, int y, int width, int height) -> {
if ((flags & (ImageObserver.WIDTH | BITS_INFO)) != 0) {
width = (width + 1) / 2;
}
if ((flags & (ImageObserver.HEIGHT | BITS_INFO)) != 0) {
height = (height + 1) / 2;
}
if ((flags & BITS_INFO) != 0) {
x /= 2;
y /= 2;
}
if(concatenateInfo){
flags &= ((ToolkitImage) image).
getImageRep().check(null);
}
return observer.imageUpdate(
image, flags, x, y, width, height);
};
ObserverCache.INSTANCE.put(observer, o);
}
return o;
}
}
private static void isAnimated(BufferedImage bi, JButton button) {
if (!button.imageUpdate(bi, ImageObserver.SOMEBITS, 0, 0, 1, 1)) {
throw new RuntimeException();
}
}
/**
* Draws as much of the specified image as is currently available.
* The image is drawn with its top-left corner at
* (<i>x</i>, <i>y</i>) in this graphics context's coordinate
* space. Transparent pixels in the image do not affect whatever
* pixels are already there.
* <p>
* This method returns immediately in all cases, even if the
* complete image has not yet been loaded, and it has not been dithered
* and converted for the current output device.
* <p>
* If the image has not yet been completely loaded, then
* <code>drawImage</code> returns <code>false</code>. As more of
* the image becomes available, the process that draws the image notifies
* the specified image observer.
* @param img the specified image to be drawn.
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @param observer object to be notified as more of
* the image is converted.
* @see java.awt.Image
* @see java.awt.image.ImageObserver
* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
* @since JDK1.0
*/
public boolean drawImage(Image img, int x, int y,
ImageObserver observer) {
if (img == null) {
return true;
}
/* The ImageWaiter creation does not return until the
* image is loaded.
*/
ImageWaiter dim = new ImageWaiter(img);
addDrawingRect(x, y, dim.getWidth(), dim.getHeight());
mPrintMetrics.drawImage(this, img);
return mGraphics.drawImage(img, x, y, observer);
}