User Defined Classes [Java]

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: User Defined Classes [Java]

Post 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?
ImageImage
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: User Defined Classes [Java]

Post 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();
    }
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: User Defined Classes [Java]

Post 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.
ImageImage
ImageImage
Image
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Re: User Defined Classes [Java]

Post 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.
Post Reply