简体   繁体   中英

How can I stop an image from flickering/dislocating when a JFrame is resized?

I have a JFrame. Within that JFrame I have a JLayeredPane layed out with an OverlayLayout that contains multiple Jpanels. in one of those JPanels I have a BufferedImage. When the JFrame is resized, the image quickly dissapears and dislocates, then it jumps back again, dislocates again, back again, and so on.

I have tried a lot of things to stop the image from flickering, but I don't know what the cause of the problem exactly is.

The Jpanel that holds the image contains the following code to render the image:

protected void paintComponent (Graphics g) {
    super.paintComponent(g);
    g.drawImage(myBufferedImage, 0, 0, 200, 200, null);
}  

In trying to reconstruct and simplify the problem, I got a working version of what I wanted. I still don't know what the problem is with my other code though. This is the code that works:

import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Main {

    public Main() {
        // Create the JFrame:
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(600, 400);
        window.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        // Create the pane to hold layers:
        JLayeredPane layers = new JLayeredPane();
        layers.setLayout(new OverlayLayout(layers));
        // Add two layers:
        layers.add(new MyGraphics());
        layers.add(new MyImage());
        //
        window.add(layers);
        window.setVisible(true);
    }

    public static void main(String[] args) {
        Main app = new Main();
    }


    public class MyImage extends JPanel {

        public BufferedImage source;

        public MyImage () {
            this.setPreferredSize(new Dimension(180,180));
            this.setLocation(0,0);
            try {
                this.source = ImageIO.read(new File("image.jpg"));
            } catch (IOException ie) {
                ie.printStackTrace();
            }
        }       

        protected void paintComponent (Graphics g) {
            super.paintComponent(g);
            g.drawImage(this.source, 0, 0, 180, 180, null);
        }

    }

    public class MyGraphics extends JPanel {

        public MyGraphics () {
            this.setOpaque(false);
            this.setPreferredSize(new Dimension(180,180));
            this.setLocation(0,0);
        }

        protected void paintComponent (Graphics g) {
            super.paintComponent(g);
            g.drawLine(0, 0, 180, 180);
        }

    }

}

Try adding below line of code in constructor:

public FlickerDemo()
   {
      // No flickering during resize
      System.setProperty("sun.awt.noerasebackground", "true");
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM