User Defined Classes [Java]
Moderator: MaxCoderz Staff
Re: User Defined Classes [Java]
Okay, here's the download: TrafficLight. I couldn't figure out how to make it an applet. But here it is nonetheless.
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
Re: User Defined Classes [Java]
Ok, it works (of course), but why does it try to use 100% cpu?
Re: User Defined Classes [Java]
I'm not sure I know what you mean.King Harold wrote:Ok, it works (of course), but why does it try to use 100% cpu?
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
Re: User Defined Classes [Java]
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]
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 .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
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
Re: User Defined Classes [Java]
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..
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]
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.
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
Re: User Defined Classes [Java]
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..
But I thought those action listeners didn't cause such behaviour? they never did for me anyway..
Re: User Defined Classes [Java]
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
Thanks to Ben and my brother, I have been able to upload source in its glory.
TrafficLight.java
TrafficLightPanel.java
TrafficLightMain.java
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
Re: User Defined Classes [Java]
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]
The repaint function is needed if I want to keep painting, else the program will only draw the light once.
- benryves
- Maxcoderz Staff
- Posts: 3089
- Joined: Thu 16 Dec, 2004 10:06 pm
- Location: Croydon, England
- Contact:
Re: User Defined Classes [Java]
You should only call repaint() when you need to repaint, ie when the state of the light has changed.
Re: User Defined Classes [Java]
Are you implying that I modify my code. Please let me know if anything should be modified to optimize the program.benryves wrote:You should only call repaint() when you need to repaint, ie when the state of the light has changed.
Re: User Defined Classes [Java]
Ben is saying, after instructing the colors (2 of them) to darken and one to brighten then you will call repaint().
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
Re: User Defined Classes [Java]
And that you do not call repaint after painting.