United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4618975 : Graphics.drawImage() does not notify ImageObserver

Details
Type:
Bug
Submit Date:
2002-01-03
Status:
Closed
Updated Date:
2004-09-10
Project Name:
JDK
Resolved Date:
2003-10-27
Component:
docs
OS:
solaris_7,generic
Sub-Component:
guides
CPU:
sparc,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.4.0,5.0
Fixed Versions:
5.0 (b26)

Related Reports

Sub Tasks

Description
Name: atR10191			Date: 01/03/2002


specifications for methods
public abstract boolean drawImage(Image img, ... ,ImageObserver observer)
of class Graphics states
"...
If the image has not yet been completely loaded, then drawImage returns false.
As more of the image becomes available, the process that draws the image
notifies the specified image observer".
The example below shows that after first call of drawImage(), following
calls of the method do not cause observer to be notified, when Image
is animated image.
============ Test56.java ==============================================
import java.awt.*;
import java.awt.image.*;

public class Test56 {
    public static void main(String argv[]) {
        Test56 test = new Test56();
        test.testRun();
        System.out.println("OKAY");
        System.exit(0);
    }
    void testRun(){
        Frame frm = new Frame();
        frm.show();    
 
        frm.setSize(300, 300);
        Graphics g = frm.getGraphics();

        int w = 50;
        int h = 50;
        int size = w * h;
        int[] pix = new int[size];

        int value = frm.getBackground().getRGB();
        for (int i = 0; i <size; i++) {
            pix[i] = value;
        }

        MemoryImageSource src = new MemoryImageSource(w, h, pix, 0, w);
        src.setAnimated(true);

        Image img = frm.createImage(src);
        MediaTracker tracker = new MediaTracker(frm);

        tracker.addImage(img, 0);
        MyImageObserver observer = new MyImageObserver();

        g.drawImage(img, 0, 0, new Container());

        if (!g.drawImage(img, 1, 1, observer)) {
            try {
                tracker.waitForAll();
            } catch (InterruptedException e) {
                 System.out.println("unable to load image");
                 System.exit(1);
            }
            if (!observer.bImageUpdateWasCalled) {
                 System.out.println("ImageObserver was not notified " +
                     "by time when image was completely loaded");
                 System.exit(1);
            }
        }
    }
}

class MyImageObserver extends Container {
    boolean bImageUpdateWasCalled = false;

    public boolean imageUpdate(Image img, int infoflags, int x,
                               int y, int width, int height) {
        bImageUpdateWasCalled = true;
        return super.imageUpdate(img, infoflags, x, y, width, height);
    }
}
======== end of Test56.java ==========================================
======== output of Test56 ==========================================
ImageObserver was not notified by time when image was completely loaded
======== end of output of Test56 ==========================================
======================================================================</TEXTAREA>
		      </td>
                    </tr>
                    <TR>
                      <TD colspan="2" bgcolor="#BFBFBF"> </td>
                    </tr>

<a name="comments"></a>
                    <!-- COMMENTS -->
                    <TR>
                      <TD bgcolor="#BFBFBF" align="left" valign="bottom" height="24"> 
		        <img src="https://central.sun.net/https%3A//swbtres.central.sun.com%3A443/bugz/images/dot.gif" width="10">Comments
		      </td>
                      <TD bgcolor="#BFBFBF" align="left" valign="bottom" height="24"> 
			<!-- BEGIN:TBR Mohan
 			<A href="javascript:doDateStampSubmit(document.editbug_general, 'comments');"><font size="-1">[ Date Stamp ]</font></A> 
			<img src="https://central.sun.net/https%3A//swbtres.central.sun.com%3A443/bugz/images/dot.gif" width="18">
			END:TBR -->
			<A href="javascript:doFullPageSubmit(document.editbug_general, 'comments');"><font size="-1">[ Full Page ]</font></a>
		        <img src="https://central.sun.net/https%3A//swbtres.central.sun.com%3A443/bugz/images/dot.gif" width="22">
		        <FONT size="-1" color="darkblue">---  Enter SUN Proprietary data here  ---</font>
	              </td>
                    </tr>

                    <TR>
                      <TD bgcolor="#BFBFBF" colspan="2" nowrap align="left">
			<img src="https://central.sun.net/https%3A//swbtres.central.sun.com%3A443/bugz/images/dot.gif" width="5">
                        <TEXTAREA rows="6" cols="95" wrap="virtual" name="comments" align="left" bgcolor="white">

Name: iaR10016			Date: 07/15/2003


JCK1.5 test api/java_awt/Graphics/index.html#DrawImage[Graphics2025_14] fails with JDK1.5.0-b10 due to this bug.
======================================================================

Name: iaR10016			Date: 11/05/2003


JCK1.5-runtime (b07) api/java_awt/Graphics/index.html#DrawImage[Graphics2025_14] test
is in the latest JCK1.5 known failure list because of this bug.
Graphics class API Specification was changed in the latest tiger builds, so the test
should be updated to meet new Specification requirenments.
I have filed bug 4949095 about this issue.
###@###.###
======================================================================

Name: yd153349			Date: 09/10/2004

Remove  ###@###.### jck


======================================================================
###@###.### 2004-09-10

                                    

Comments
EVALUATION

Passing to 2D for their evaulatuion.
###@###.### 2002-01-03

First, there is an issue with the MediaTracker in that it was implemented
to return after the "first frame" of an animated image completes.  The
reason this was done was that it would be impossible to wait for such
an image to be "fully loaded" within the constraints of its design which
relies on the ImageObserver notifications.  There is currently no way
within the Toolkit image code to know when all of the data for an image
has been fetched so we can only infer from the FRAME and ALLBITS
notifications to an observer whether the image was loaded.  Note that
the domain of image loading and tracking now falls under the Image I/O
APIs so it is unlikely that the design of Toolkit images will ever be
enhanced to make this make more sense.

Given that caveat, this test case is then flawed in that it figures that
the return from waitForAll() means that the image was completely loaded.
It would then make sense that if the observer had been handed to a
drawImage call which returned false and the image had subsequently been
completely loaded then the observer must at some point be notified in
some manner, but animated images break this in two ways:

	- They always return false from drawImage until the
	  animation completes

	- They kick MediaTracker out of its wait loops after
	  the first frame is delivered

Given those 2 conditions it then follows that if an animated image is
in the state of already having drawn its first frame when drawImage
is called, then that drawImage will return false, and an immediately
subsequent MediaTracker operation will return immediately as well and
no notifications will occur in that time frame.

Note that if you remove the call to drawImage(..., new Container())
from right before the loop then the test says  "OKAY".

The documentation for MediaTracker should really be modified to indicate
that the wait routines will return when a static image is completely loaded
or when a dynamic/animated image has its first frame completed.

As such, I am reassigning this as a doc bug.

###@###.### 2003-03-14
                                     
2003-03-14
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
generic
tiger-beta

FIXED IN:
tiger
tiger-beta

INTEGRATED IN:
tiger-b26
tiger-beta

VERIFIED IN:
1.5


                                     
2004-09-13



Hardware and Software, Engineered to Work Together