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 »

Okay, here's the download: TrafficLight. I couldn't figure out how to make it an applet. But here it is nonetheless.
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 »

Ok, it works (of course), but why does it try to use 100% cpu?
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 »

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.
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 »

It uses 25% CPU on my machine - that's 100% on 1 core. Seems a little much for just switching between 3 states :)
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 »

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) .
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 »

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..
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 »

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.
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 »

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..
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 »

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
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 »

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)
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 »

The repaint function is needed if I want to keep painting, else the program will only draw the light once.
ImageImage
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3089
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: User Defined Classes [Java]

Post by benryves »

You should only call repaint() when you need to repaint, ie when the state of the light has changed.
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 »

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.
ImageImage
ImageImage
Image
User avatar
kalan_vod
Calc King
Posts: 2932
Joined: Sat 18 Dec, 2004 6:46 am
Contact:

Re: User Defined Classes [Java]

Post by kalan_vod »

Ben is saying, after instructing the colors (2 of them) to darken and one to brighten then you will call repaint().
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Re: User Defined Classes [Java]

Post by King Harold »

And that you do not call repaint after painting.
Post Reply