2 Replies Latest reply on Oct 2, 2002 9:34 AM by 843797

    drawImage and ClassCastExceptions

      Hi everyone. For two weeks, I've been suffering with a ClassCastException that I can't get rid of. I'm writing a poker like java applet that allows you to click on locations to place your cards. Each card has an image associated with it - these images are all gif's. In a JPanel, within my paintComponent method, I call g.drawImage(image, x, y, this) to display a card. Occasionally - not consistently, an image I try to place doesn't appear and I'll get the following stack trace:

      Uncaught error fetching image:
      java.lang.ClassCastException: [I
      at java.awt.image.ColorModel.getAlpha(ColorModel.java:814)
      at java.awt.image.ColorModel.getRGB(ColorModel.java:859)
      at sun.awt.image.ImageRepresentation.convertToRGB(ImageRepresentation.java:271)
      at sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:485)
      at java.awt.image.AreaAveragingScaleFilter.accumPixels(AreaAveragingScaleFilter.java:176)
      at java.awt.image.AreaAveragingScaleFilter.setPixels(AreaAveragingScaleFilter.java:215)
      at sun.awt.image.PixelStore8.replayLines(PixelStore8.java:38)
      at sun.awt.image.PixelStore.replay(PixelStore.java:217)
      at sun.awt.image.PixelStore.replay(PixelStore.java:146)
      at sun.awt.image.InputStreamImageSource.updateFromStore(InputStreamImageSource.java:295)
      at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:244)
      at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:214)
      at sun.awt.image.ImageFetcher.run(ImageFetcher.java:182)

      What can I do in my code to stop this exception from being thrown, so my cards always appear?

      Thanks very much for your help

        • 1. Re: drawImage and ClassCastExceptions
          Since there's no useful message accompanying it (though the "[I" might indicate some sort of integer array), I think the best way to figure out what's going on is to look at the source file.

          Go to your JDK directory.  There should be a file called src.zip or src.jar somewhere in there.  Unzip it with a normal .zip program or with "jar xf src.jar".

          Go into the directory you unzipped into and find the source code for the ColorModel class (unzipdir/java/awt/image/ColorModel.java.  Look for an explicit cast somewhere on line 814.

          I couldn't look at the problem because I don't have the same version of the JDK as you do (my ColorModel.java's getAlpha() method is abstract).  If you provide the JDK version number to forum readers maybe someone can take a look and figure it out.

          Though the lack of an error message or any of your function calls in the trace seems to indicate a JDK bug but then again, it might be caused by a parameter you gave it a long time ago.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
          • 2. Re: drawImage and ClassCastExceptions
            Perhaps this may be useful :


            see Reply Nr. 5