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

Painting a component to an off-screen image should not affect the double buffer

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P3 P3
    • 7
    • 6, 6u10, 7
    • client-libs
    • generic, x86
    • generic, windows_xp

        When implementing visual effects with Swing
        it's often necessary to paint a component to an off-screen buffer

        unfortunately it causes problems if this component or any of its children
        are double-buffered

        Run the following test and see the unexpected visual artefacts

        JDK 6u10 or previous version

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

        public class Test {

            private static void createGui() {
                final JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
                // panel is double buffered
                final JPanel panel = new JPanel();
                panel.setSize(100, 100);

                JButton b = new JButton("Test") {

                    protected void paintComponent(Graphics g) {
                        super.paintComponent(g);

                        // Note - the panel is not added to the frame
                        BufferedImage im = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
                        panel.paint(im.getGraphics());
                    }
                };
                frame.add(b);

                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

            public static void main(String[] args) throws Exception {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        Test.createGui();
                    }
                });
            }
        }

              art Artem Ananiev (Inactive)
              alexp Alexander Potochkin (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: