开发者

why PaintComponent event in Java happen everytime I use its Graphics Event?

开发者 https://www.devze.com 2022-12-12 18:40 出处:网络
Consider this code: public class StateChartPanel extends JPanel { private LightContext LC; public StateChartPanel(LightContext lc){

Consider this code:

public class StateChartPanel extends JPanel {
    private LightContext LC;

    public StateChartPanel(LightContext lc){
        LC=lc;
    }
    public void paintComponent( Graphics G ){           
        sup开发者_运维百科er.paintComponent( G );
        LC.DrawStateChart((Graphics2D)G);     
    }
}

StateChartPanel is a panel to draw something (a state chart). It sends its Graphics object to LC which use it to draw shapes but whenever it draws something the PaintComponent event of StateChartPanel happens again and it causes my application to hang.


I think what's likely happening is an infinite loop: StateChartPanel.paintComponent is calling LC.DrawStateChart, which is then calling StateChartPanel.paintComponent. You probably have a StateChartPanel as a subcomponent of LC somehwere, and LC.DrawStateChart is calling its own paint() function. Try removing StateChartPanel.paintComponent's call to LC.DrawStateChart and see if that works.


Please learn proper Java Naming conventions. All conventions are typically followed in text books, tutorials or code we post in the forums. So follow them and don't make up your own:

a) variable names should not start with an upper case character

b) method names should not start with an upper case character

If you are looping then its probably because you invoke repaint() somewhere in your invisible code that you didn't post.

If you need more help post your SSCCE that shows the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消