Sourabh
Sourabh

Reputation: 119

does calling a repaint() inside paint() lead to infinite recursion?

I have been developing a java application related to graphics . From my understanding everytime i use the repaint() method anywhere in the code , the overridden paintComponent is called . But i have also used repaint() inside the paintComponent itself .why is it not leading to infinite recursion ? the code works fine .

              panel = new JPanel(){
        public void paintComponent(Graphics g)
        {

            {

                panel.revalidate();
                panel.repaint();
                c.revalidate();
                c.repaint();
                revalidate();
                repaint();
                for(int i=0;i<linecount-1;i+=2)
                {
                    Line2D line = new Line2D.Double(xco[i],yco[i],xco[i+1],yco[i+1]);
                    Graphics2D g2d = (Graphics2D) g;
                    if(divide[i]==1)
                    {

                    //System.out.print(xco[i]+" "+yco[i]);
                    //System.out.println();

                     g2d.setStroke(drawingStroke);
                     g2d.draw(line);
                     g2d.setStroke(simple);
                    }
                    else
                    {
                        g2d.setStroke(simple);
                        g2d.draw(line);
                    }
                    //g.drawLine(xco[i],yco[i],xco[i+1],yco[i+1]);
                }

                //g.drawLine(x1,y1,x2,y2);
                panel.revalidate();
                panel.repaint();
                c.revalidate();
                c.repaint();
                revalidate();
                repaint();
            }
        }
    };

}

Upvotes: 3

Views: 244

Answers (1)

Robin Green
Robin Green

Reputation: 33063

It does not lead to infinite recursion - on the Java stack - because repaint merely schedules a new paint to be done, it doesn't actually call paint or paintComponent immediately.

However it's still a bad idea to do this.

Upvotes: 5

Related Questions