Page 2 of 3

Re: User Defined Classes [Java]

Posted: Sat 07 Mar, 2009 4:04 am
by Wesley
Okay, here's the download: TrafficLight. I couldn't figure out how to make it an applet. But here it is nonetheless.

Re: User Defined Classes [Java]

Posted: Sat 07 Mar, 2009 4:49 pm
by King Harold
Ok, it works (of course), but why does it try to use 100% cpu?

Re: User Defined Classes [Java]

Posted: Sat 07 Mar, 2009 6:40 pm
by Wesley
King Harold wrote:Ok, it works (of course), but why does it try to use 100% cpu?
I'm not sure I know what you mean.

Re: User Defined Classes [Java]

Posted: Sat 07 Mar, 2009 6:51 pm
by King Harold
It uses 25% CPU on my machine - that's 100% on 1 core. Seems a little much for just switching between 3 states :)

Re: User Defined Classes [Java]

Posted: Sat 07 Mar, 2009 7:40 pm
by Wesley
King Harold wrote:It uses 25% CPU on my machine - that's 100% on 1 core. Seems a little much for just switching between 3 states :)
I can't notice. It goes super fast on my computer, but when I was on the Linux computers at school, the program did run slow. I'm guessing it's because the various ActionListeners. They require too much attention 8) .

Re: User Defined Classes [Java]

Posted: Sat 07 Mar, 2009 8:09 pm
by King Harold
Ok well open TaskManager..
It's not that the program is slow, it's actually too fast. :)
It's probably checking for input a million times per second, or perhaps redrawing itself a million times per second. Or maybe not a million but still too often..

Re: User Defined Classes [Java]

Posted: Sat 07 Mar, 2009 9:11 pm
by Wesley
I see. It takes up about 35% on my CPU. Pretty crazy. I wonder what would happen if there were loops and conditionals instead of the ActionListener "hack" as you call it.

Re: User Defined Classes [Java]

Posted: Sun 08 Mar, 2009 10:01 am
by King Harold
Same thing probably, but maybe you could just put a Sleep in it somewhere, that would help.. Just sleep for 1 milisecond or something that's already a whole lot (seeing as most instructions take a fraction of a nanosecond..)

But I thought those action listeners didn't cause such behaviour? they never did for me anyway..

Re: User Defined Classes [Java]

Posted: Sun 08 Mar, 2009 8:24 pm
by Wesley
I'm not really sure about the action listeners. Remember that I am a beginner at this sort of thing. Those are simply my assumptions.

Thanks to Ben and my brother, I have been able to upload source in its glory.
TrafficLight.java
TrafficLightPanel.java
TrafficLightMain.java

Re: User Defined Classes [Java]

Posted: Sun 08 Mar, 2009 9:13 pm
by King Harold
Ok, I think I see the problem, you put repaint() in the paint, so it will keep painting. Does it work without that? If you remove it, don't forget to repaint it after changing what should be painted (so after a click on the button)

Re: User Defined Classes [Java]

Posted: Mon 09 Mar, 2009 1:23 am
by Wesley
The repaint function is needed if I want to keep painting, else the program will only draw the light once.

Re: User Defined Classes [Java]

Posted: Mon 09 Mar, 2009 1:43 am
by benryves
You should only call repaint() when you need to repaint, ie when the state of the light has changed.

Re: User Defined Classes [Java]

Posted: Mon 09 Mar, 2009 2:58 am
by Wesley
benryves wrote:You should only call repaint() when you need to repaint, ie when the state of the light has changed.
Are you implying that I modify my code. Please let me know if anything should be modified to optimize the program.

Re: User Defined Classes [Java]

Posted: Mon 09 Mar, 2009 5:45 am
by kalan_vod
Ben is saying, after instructing the colors (2 of them) to darken and one to brighten then you will call repaint().

Re: User Defined Classes [Java]

Posted: Mon 09 Mar, 2009 10:58 am
by King Harold
And that you do not call repaint after painting.