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

PNG Transparency ignored

XMLWordPrintable

    • 2d
    • x86
    • windows_nt, windows_xp

      Name: rlT66838 Date: 01/26/2000


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

      Loading a transparent PNG image using the ImageIcon constructor doesn't render
      correctly (image is treated as opaque). The code sample creates two images,
      loads both of them and renders them. It also creates an html file that loads
      the same images. Internet Explorer 5.0 shows both images as transparent.

      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      import javax.swing.*;

      public class Test extends JFrame {
          static short[] aimpoint_gif = new short[] {
              71,73,70,56,57,97,16,0,16,0,243,0,0,0,0,0,128,
              0,0,0,128,0,128,128,0,0,0,128,128,0,128,0,128,
              128,192,192,192,128,128,128,255,0,0,0,255,0,255,255,0,
              0,0,255,255,0,255,0,255,255,255,255,255,33,249,4,9,
              0,0,15,0,44,0,0,0,0,16,0,16,0,0,4,56,
              240,201,73,171,125,32,231,43,115,74,64,23,86,192,247,141,
              29,85,154,32,57,173,38,154,98,236,105,133,112,124,99,90,
              239,111,178,78,173,21,164,213,70,193,92,107,246,58,170,84,
              172,226,198,56,189,248,42,17,0,59 };
          static short[] aimpoint_png = new short[] {
              137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,
              0,0,16,0,0,0,16,4,3,0,0,0,237,221,226,82,
              0,0,0,48,80,76,84,69,0,0,0,128,0,0,0,128,
              0,128,128,0,0,0,128,128,0,128,0,128,128,192,192,192,
              128,128,128,255,0,0,0,255,0,255,255,0,0,0,255,255,
              0,255,0,255,255,255,255,255,79,19,38,73,0,0,0,16,
              116,82,78,83,255,255,255,255,255,255,255,255,255,255,255,255,
              255,255,255,0,224,35,93,25,0,0,0,1,98,75,71,68,
              15,24,186,0,217,0,0,0,67,73,68,65,84,120,218,77,
              205,193,9,0,65,8,3,64,191,251,74,235,126,237,50,29,
              100,163,119,224,6,132,1,197,132,244,37,122,2,131,40,16,
              6,171,32,26,167,33,44,188,105,240,65,252,161,31,249,44,
              23,194,0,15,216,200,233,210,73,108,251,5,158,101,80,185,
              68,88,87,125,0,0,0,0,73,69,78,68,174,66,96,130 };

          protected Icon gif_icon;
          protected Icon png_icon;

          public Test ()
          {
              try {
                  createFiles ();
                  initScreen ();
              } catch (Exception exn) {exn.printStackTrace();}
          }

          public void createFiles () throws Exception
          {
              write (aimpoint_gif, "Aimpoint.gif");
              write (aimpoint_png, "Aimpoint.png");

              PrintWriter wtr = new PrintWriter (new FileWriter ("Sample.html"));
              wtr.println("<html>");
              wtr.println("<body bgcolor=aqua>");
              wtr.println("<img src=\"aimpoint.gif\"><p>");
              wtr.println("<img src=\"aimpoint.png\">");
              wtr.println("</body>");
              wtr.println("</html>");
              wtr.close ();
          }

          public static void write (short[] data, String filename) throws Exception
          {
              byte[] bdata = new byte[data.length];
              for (int i=0; i<data.length; i++)
              {
                  if (data[i] < 128) bdata[i] = (byte)data[i];
                  else bdata[i] = (byte) (data[i]-256);
              }
              FileOutputStream file = new FileOutputStream (filename);
              file.write (bdata);
              file.close ();
          }

          public void initScreen ()
          {
              gif_icon = new ImageIcon ("Aimpoint.gif");
              png_icon = new ImageIcon ("Aimpoint.png");
              setSize (200, 200);
              getContentPane().add(new MyPanel());
              addWindowListener (new WindowAdapter () {
                  public void windowClosing (WindowEvent e) {
                      System.exit (0);
                  }
              });
          }

          class MyPanel extends JPanel
          {
              public MyPanel ()
              {
                  MyPanel.this.setBackground (Color.blue);
              }
              public void paint (Graphics g)
              {
                  super.paint(g);
                  gif_icon.paintIcon (this, g, 0, 0);
                  g.drawString ("<--GIF",40,10);
                  png_icon.paintIcon (this, g, 0, 30);
                  g.drawString ("<--PNG",40,40);
              }
          }

          static public void main (String[] args)
          {
              new Test ().show();
          }


      }
      (Review ID: 100395)
      ======================================================================

            bae Andrew Brygin
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: