Page 3 of 3

Re: User Defined Classes [Java]

Posted: Tue 10 Mar, 2009 1:49 am
by Wesley
Ay, I don't really know what to do to my code to make that change! Could you specify what changes to lines in my code I should make?

Re: User Defined Classes [Java]

Posted: Tue 10 Mar, 2009 1:54 am
by benryves
Remove line 45 in TrafficLight.java (repaint()) - this forces the control to repaint itself straight after painting itself, bogging down the CPU in an infinite loop.

Now, you only want to trigger a repaint when the control needs to be repainted. In your case, this is after the state of the traffic lights has changed, so add repaint() to the end of the TrafficLight.red(), TrafficLight.yellow() and TrafficLight.green() methods, like this:

Code: Select all

    public void red() {
        color = Color.red;
        y = 5;
        repaint();
    }

Re: User Defined Classes [Java]

Posted: Tue 10 Mar, 2009 3:34 am
by Wesley
Awesome, I got a direct answer this time! Ben, that works perfectly. There is hardly any CPU being used up by the program now. Thanks!

Too bad I already turned in the assignment.

Re: User Defined Classes [Java]

Posted: Tue 10 Mar, 2009 12:26 pm
by King Harold
Don't worry they usually aren't looking for that kind of thing..
I mean, in one of my handwritten exams for imperative programming I forgot an entire while loop, and I still got an 8.. Ok that's not as good as the 9.5 and 9.7 I had for the other 2 hand written exams but forgetting an complete while loop.. dang.. I would have given myself a 3 or maybe a 4..
Compared to that, what's the location of the repaint to them?
I bet you get at least a 6 for delivering something that does exactly what it has to do, with additional points for "nice code" and maybe for nice graphics even, at least that's what it would be like here.