简体   繁体   中英

Why is this JPanel still being repainted?

I have a custom JLayeredPane and two custom child JPanel 's. I repaint everything in a while loop in a Thread . I have used RepaintManager.currentManager(backPanel).markCompletelyClean(backPanel); to stop the repainting the backPanel JPanel . As to not waste system resources. However, This does not work. As we can see in the paintComponent method, if you run and compile the code below, you will see that the statement "Reccursion" is reccursively repeated in the output, from the System.out.println("Reccursion") method call in the paintComponent method.

Why is the backPanel JPanel still being repainted and why is the method System.out.println("Reccursion") still being called, When I have used RepaintManager.currentManager(backPanel).markCompletelyClean(backPanel); .

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Main extends JLayeredPane {
    static JFrame frame;
    static Main main;
    static Dimension screenSize;
    public Main() {     
        JPanel backPanel = new BackPanel();
        JPanel frontPanel = new FrontPanel();
        add(backPanel, new Integer(7));
        add(frontPanel, new Integer(8));

        new Thread(() -> {
            while (true){
                repaint();
            }
        }).start();

        RepaintManager.currentManager(backPanel).markCompletelyClean(backPanel);

    }

    public static void main(String[] args) {
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        frame = new JFrame("Game"); // Just use the constructor

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        main = new Main();
        frame.add(main, BorderLayout.CENTER);

        frame.pack();
        frame.setSize(screenSize);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);



    }
    public class BackPanel extends JPanel{
        public boolean drawn = false;
        public BackPanel(){
            setVisible(true);
            setOpaque(false);
            setSize(screenSize);
            JLabel test1 = new JLabel("Test1");
            JLabel test2 = new JLabel("Test2");
            add(test1);
            add(test2);
        }
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillRect(0, 0, screenSize.width, 200);
            System.out.print("Reccursion");
        }
    }
    public class FrontPanel extends JPanel{

        public FrontPanel(){
            setVisible(true);
            setOpaque(false);
            setSize(screenSize);
            JLabel test = new JLabel("Test");
            add(test);
        }
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.blue);
            g.fillRect(0+screenSize.width/2, 0, screenSize.width/4, 300);
        }
    }
}

The method "paintComponent" is called quite often and without your "permission" (it's been giving me a hard time too...) You can't do anything about it, just don't generate new data everytime in this method or the appearance may change as often. Good luck

https://books.google.fr/books?id=KXz6AQAAQBAJ&pg=PA211&lpg=PA211&dq=paintcomponent+called+repeatedly&source=bl&ots=AEql59yy51&sig=Ezny1GptXRthd1e0tuQFdM5peoo&hl=fr&sa=X&ved=0ahUKEwjun4j6rJHOAhWK2xoKHYaPCEQ4ChDoAQgkMAE#v=onepage&q=paintcomponent%20called%20repeatedly&f=false

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