Java – why this swing error occurs when using repaint () instead of GetParent () repaint()?

This problem is based on the problem I came back with a simple swing dice program The original question I posted is here and I have accepted the answer, but I want to know exactly what happened, why the problem happened and how the solution works

I tried to cut the original code to find the core of the problem, and now it looks very different:

>I have two colorpanels, each drawing a color square > when you click the panel, the box should change its color in this order: start with black, then > Red > Green > Blue > Red > Green > Blue > etc. > once the box changes its color, it should not turn black again

However, when I called repaint () in MouseListener, the program behaved very strangely:

>When I click a panel, the color of the box will change > then I click another one, and its box changes color, but the first box also changes, back to black > you can see this behavior in the GIF below:

If you use getparent() Repaint(), this behavior disappears and the program runs as expected:

>If the panels / squares start to "overlap", it seems that there will only be problems. > If you use a layout that stops this layout or does not set the size small, there does not seem to be a problem. > The problem doesn't happen every time. At first, I thought it might involve concurrency problems. > The code I encountered in the original problem does not seem to cause problems to everyone, so my IDE, JDK, etc. may also be relevant: Windows 7, eclipse, Kepler, jdk1 7.0_ 03

The code minus import, etc. is as follows:

public class ColorPanelsWindow extends JFrame{

    static class ColorPanel extends JPanel {

        //color starts off black
        //once it is changed should never be 
        //black again
        private Color color = Color.BLACK;

        ColorPanel(){
            //add listener
            addMouseListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent arg0) {
                    color = rotateColor();
                    repaint();
                    //using getParent().repaint() instead of repaint() solves the problem
                    //getParent().repaint();
                }
            });
        }
        //rotates the color black/blue > red > green > blue
        private Color rotateColor(){
            if (color==Color.BLACK || color == Color.BLUE)
                return Color.RED;
            if (color==Color.RED)
                return Color.GREEN;
            else return Color.BLUE;
        }

        @Override
        public void paintComponent(Graphics g){
            g.setColor(color);
            g.fillRect(0,100,100);
        }
    }

    ColorPanelsWindow(){
        setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new GridLayout(1,0));
        add(new ColorPanel());
        add(new ColorPanel());
        //the size must be set so that the window is too small
        // and the two ColorPanels are overlapping
        setSize(40,40);
//      setSize(300,200);

        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokelater(new Runnable(){

            @Override
            public void run() {
                new ColorPanelsWindow();
            }

        });
    }
}

So my question is, what happened?

Solution

I saw the same problem when using JDK 7u60 on Windows 7 It's definitely a mistake for me

My best guess is that double buffering is a problem

I add this debugging code to the paintcomponent () method

1) When you click the right component, only its paintcomponent () method is called, and the panel is drawn in the correct color

2) When the left component is clicked and the panel is drawn in the correct color, the right panel will revert to black without calling the paintcomponent() method on the right panel This makes me believe that the old buffer is used somehow (this will be an error, I don't know how to solve it)

getParent(). Repaint () works because no matter which panel you click, it forces you to redraw the two components

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>