java.awt.Image subclass fails to render

Jeremy Wood mickleness at gmail.com
Mon Mar 6 05:12:52 UTC 2023


If I call Graphics2D#drawImage(customImage, 0, 0, null) on a custom 
java.awt.Image subclass: the drawImage(..) method returns false and 
nothing renders.

Is this something I can write up as an openJDK ticket and propose a 
fix/PR for? (That is: is anyone with more authority willing to sponsor 
me if I try to approach this?)

I made a simple test case here 
<https://drive.google.com/file/d/1kMlRZ3SPp7FkGTsA1vdnn--tINGQRmp4/view?usp=sharing>. 
(about 230 lines)

I think the problem is coming from this line 
<https://drive.google.com/file/d/13v7jqv-XpDTIeYoC2uU0Rck4w3iI-u1I/view?usp=sharing> 
in SurfaceManager.java, where we cast an Image to a BufferedImage (after 
already ruling out the possibility that it’s a ToolkitImage).

In my case ‘img’ is not a BufferedImage, so this triggers a 
ClassCastException. (Which is responsibly caught and eventually 
translates into “the drawImage method returns false and nothing 
renders”).

In my case a viable solution might be to add code like this somewhere in 
SunGraphics2D or a supporting class:

try {
     if (! (img instanceof BufferedImage || img instanceof ToolkitImage)) 
{
         img = newTempImage = Toolkit.getDefaultToolkit().createImage( 
img.getSource() );
     }
     paintAsUsual(img)
} finally {
     if (newTempImage != null)
         newTempImage.flush()
}

That solution could in some cases be wasteful regarding memory (if we 
constantly create and flush a ToolkitImage), but I’d suggest that seeing 
inefficient results is better than seeing no results. Thoughts?

Regards,
  - Jeremy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/client-libs-dev/attachments/20230306/392e5f53/attachment.htm>


More information about the client-libs-dev mailing list