views:

112

answers:

2

Consider this code:

public class StateChartPanel extends JPanel {
    private LightContext LC;

    public StateChartPanel(LightContext lc){
        LC=lc;
    }
    public void paintComponent( Graphics G ){           
        super.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.

A: 

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.

Seth
A: 

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.

camickr
although what you said is more than true (really do learn some conventions) i think this belongs as a comment and not as an answer...
Savvas Dalkitsis
My answer was that the code was probably using a repaint(), but it was just a guess since we can't see the code. I then include a link about how to post proper code with a question. I used this as an answer since I don't know how to include links in a comment. I also gave bonus suggestions for writing better code. I don't know how to add formatting in a comment so the two points would come out as a single string. I guess I could have added two comments but I didn't think that would work either.I should get bonus marks :) Sorry for mixing an "answer" and "suggestion" in the same place
camickr
Well, my answer is still far better than any answer provided by the second annonymous individual. Why don't you try to provide some usefull information to the posting?
camickr