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

Background not refreshed when painting over a transparent JFrame

    XMLWordPrintable

Details

    • b11
    • x86_64
    • linux_ubuntu

    Backports

      Description

        FULL PRODUCT VERSION :
        openjdk version "1.8.0_141"
        OpenJDK Runtime Environment (build 1.8.0_141-8u141-b15-3~14.04-b15)
        OpenJDK 64-Bit Server VM (build 25.141-b15, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Linux Wall-E 3.13.0-35-generic #62-Ubuntu SMP Fri Aug 15 01:58:42 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

        A DESCRIPTION OF THE PROBLEM :
        The best is to check this link to understand the problem : https://stackoverflow.com/questions/46273464/background-not-refreshed-when-painting-over-a-transparent-jframe

        Basically, the background of the JFrame is not refreshed despite calling repaint() when the JFrame is transparent. The bug seems to affect only the jvm for GNU/Linux as it could not be reproduced under MacOS.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        There is a test case at https://stackoverflow.com/questions/46273464/background-not-refreshed-when-painting-over-a-transparent-jframe

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The "text" label should be moving around the frame
        ACTUAL -
        The "text" label is painted over the frame but previously painted text are never cleared.

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package test;

        import java.awt.Color;
        import java.awt.Component;
        import java.awt.Graphics;
        import java.awt.Graphics2D;
        import java.awt.Point;
        import java.awt.event.MouseAdapter;
        import java.awt.event.MouseEvent;
        import java.awt.image.BufferedImage;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.JPanel;
        import javax.swing.SwingUtilities;

        public class TranslucentWindow extends JFrame {
            /** The image we will draw on the frame **/
            private final BufferedImage test = generateImage();
            /** The location where to paint the image **/
            private Point p = new Point();
            /** Set up the GUI **/
            public TranslucentWindow() {
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                setUndecorated(true);
                setExtendedState(MAXIMIZED_BOTH);
                setBackground(new Color(0, 0, 0, 0));
                //Disable this line for case 2
                setContentPane(new TranslucentPane());
                getContentPane().addMouseMotionListener(new MouseDragListener());
            }
            public static void main(String[] args) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        new TranslucentWindow().setVisible(true);
                    }
                });
            }
            
            /** Move the image on the frame **/
            private class MouseDragListener extends MouseAdapter {
                @Override
                public void mouseMoved(MouseEvent e) {
                    p = e.getPoint();
                    repaint();
                }
            }
            
            /** Capture an image of any component **/
            private static BufferedImage getImage(Component c) {
                if(c==null) return null;
                BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = image.createGraphics();
                c.printAll(g);
                g.dispose();
                return image;
            }
            
            /** Generates a dummy image to be painted on the frame **/
            private static BufferedImage generateImage() {
                JLabel label = new JLabel("test");
                label.setSize(label.getPreferredSize());
                return getImage(label);
            }
            
            //Enable these lines for case 2
        // @Override
        // public void paint(Graphics g) {
        // super.paint(g);
        // g.drawImage(test, p.x, p.y, this);
        // }
            
            public class TranslucentPane extends JPanel {
                public TranslucentPane() {
                    setOpaque(false);
                }
                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    Graphics2D g2d = (Graphics2D) g.create();
                    g2d.setColor(new Color(0,0,0,0));
                    g2d.fillRect(0, 0, getWidth(), getHeight());
                    g2d.drawImage(test, p.x, p.y, this);
                }
            }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Every attempt to find a workaround has failed...

        Attachments

          Issue Links

            Activity

              People

                tr Tejesh R
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                10 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: