Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4399407

Drawing an offscreen image through a filtered image doesn't work anymore

XMLWordPrintable

    • 2d
    • x86
    • windows_nt

      Name: boT120536 Date: 12/19/2000


      -----------------

      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      My problem is that I have an Icon class that supports a "disabled" state from
      an initial image. It calculates the disabled image using a filter. In JDK 1.3
      this object causes an exception to be thrown in the sun.* libraries. (Thus I
      have not been able to figure out the actual error)

      Here is the source code that no longer works as of JDK 1.3 (but works fine in
      JDK 1.2.2) It includes a test harness as well (so the class is self-runnable).


      ------ SOURCE ------

      /** This icon has the logic to be enabled or disabled
        */

      import java.awt.*;
      import java.awt.image.*;
      import javax.swing.*;

      public class DisablableIcon implements Icon
      {
          private Icon originalIcon;
          private Image disabledImage;
          private Component lastComponent;
          
          private boolean enabled = true;
          
          public DisablableIcon(Icon enabledIcon, boolean isenabled)
          {
              enabled = isenabled;
              originalIcon = enabledIcon;
          }
          
          public DisablableIcon(Icon enabledIcon)
          {
              this(enabledIcon, true);
          }
          
          public void setEnabled(boolean tf)
          {
              enabled = tf;
          }
          
          public int getIconHeight()
          {
            return originalIcon.getIconHeight();
          }
          
          public int getIconWidth()
          {
            return originalIcon.getIconWidth();
          }
          
          public void paintIcon(Component c, Graphics g, int x, int y)
          {
              if (enabled)
              {
                  originalIcon.paintIcon(c, g, x, y);
              }
              else
              {
                  if ((disabledImage == null) || (lastComponent != c))
                  {
                      int w = originalIcon.getIconWidth();
                      int h = originalIcon.getIconHeight();
                      
                      Image tempImage = c.createImage(w, h);
                      Graphics tempImageGraphics = tempImage.getGraphics();
                      
                      tempImageGraphics.setColor(c.getBackground());
                      tempImageGraphics.fillRect(0, 0, w, h);
                      originalIcon.paintIcon(c, tempImageGraphics, 0, 0);
                      
                      disabledImage = c.createImage(new FilteredImageSource
      (tempImage.getSource(), new GrayOutFilter(true)));
                      
                      lastComponent = c;
                  }

                  g.drawImage(disabledImage, x, y, c);
              }
          }
          
          private class GrayOutFilter extends RGBImageFilter
          {
            boolean isTransparent;
            int savedRGB;
            
            private GrayOutFilter(boolean isTrans)
            {
              isTransparent = isTrans;
            }
            
            public int filterRGB(int x, int y, int rgb)
            {
              if ((x == 0) && (y == 0)) savedRGB = rgb;
              
              if ((isTransparent) && (rgb == savedRGB))
              {
                return 0x00000000;
              }
              
              int a = (rgb & 0xff000000);
              int r = (rgb & 0x00ff0000) >> 16;
              int g = (rgb & 0x0000ff00) >> 8;
              int b = (rgb & 0x000000ff);

              int avgcol = Math.min(255, (r + g + b) / 3); // Correct for rounding.
              
              return a | (avgcol << 16) | (avgcol << 8) | (avgcol);
            }
          }
          
          private static void main(String[] args)
          {
              ImageIcon ii = new ImageIcon("C:\\image.gif");
              DisablableIcon di = new DisablableIcon(ii, false);
              
              JFrame jf = new JFrame();
              JPanel jp = new JPanel();
              
              jp.add(new JLabel(ii));
              jp.add(new JLabel(di));
              
              jf.getContentPane().add(jp);
              
              jf.show();
          }
      }

      ------ EXCEPTION OUTPUT ------

      Exception occurred during event dispatching:
      java.lang.NullPointerException
              at sun.awt.image.OffScreenImageSource.sendPixels(OffScreenImageSource.ja
      va:105)
              at sun.awt.image.OffScreenImageSource.produce(OffScreenImageSource.java:
      164)
              at sun.awt.image.OffScreenImageSource.addConsumer(OffScreenImageSource.j
      ava:40)
              at sun.awt.image.OffScreenImageSource.startProduction(OffScreenImageSour
      ce.java:54)
              at java.awt.image.FilteredImageSource.startProduction(FilteredImageSourc
      e.java:116)
              at sun.awt.image.ImageRepresentation.startProduction(ImageRepresentation
      .java:634)
              at sun.awt.image.ImageRepresentation.drawToBufImage(ImageRepresentation.
      java:710)
              at sun.awt.image.BufferedImageGraphics2D.drawImage(BufferedImageGraphics
      2D.java:548)
              at DisablableIcon.paintIcon(DisablableIcon.java:67)
              at javax.swing.plaf.basic.BasicLabelUI.paint(BasicLabelUI.java:152)
              at javax.swing.plaf.ComponentUI.update(ComponentUI.java:39)
              at javax.swing.JComponent.paintComponent(JComponent.java:398)
              at javax.swing.JComponent.paint(JComponent.java:739)
              at javax.swing.JComponent.paintChildren(JComponent.java:523)
              at javax.swing.JComponent.paint(JComponent.java:748)
              at javax.swing.JComponent.paintChildren(JComponent.java:523)
              at javax.swing.JComponent.paint(JComponent.java:748)
              at javax.swing.JLayeredPane.paint(JLayeredPane.java:546)
              at javax.swing.JComponent.paintChildren(JComponent.java:523)
              at javax.swing.JComponent.paint(JComponent.java:719)
              at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:23)

              at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:
      54)
              at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:91
      )
              at java.awt.Container.paint(Container.java:960)
              at javax.swing.JFrame.update(JFrame.java:333)
              at sun.awt.RepaintArea.update(RepaintArea.java:337)
              at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:197)
              at java.awt.Component.dispatchEventImpl(Component.java:2665)
              at java.awt.Container.dispatchEventImpl(Container.java:1213)
              at java.awt.Window.dispatchEventImpl(Window.java:912)
              at java.awt.Component.dispatchEvent(Component.java:2499)
              at java.awt.EventQueue.dispatchEvent(EventQueue.java:319)
              at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:10
      3)
              at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
              at java.awt.EventDispatchThread.run(EventDispatchThread.java:84)
      (Review ID: 110060)
      ======================================================================

            tdv Dmitri Trembovetski (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: