cjgone wrote:I never knew clicking a button to change afake light could be so freakin' difficult. Can ya quickly summzarize the code?
Well, the
TrafficLightMain class is simply the driver for the program, to make the whole thing appear, basically. It doesn't do too much.
The
TrafficLightPanel class organizes the
TrafficLight class and a
JButton component.
The
TrafficLight class simply creates methods to be used to create the traffic light itself (with the
paintComponent method).
cjgone wrote:I would have expected:
Create a new class for the traffic light. Draw it using some draw function. If some button is clicked to make light red, then you call some function to change the color (red, yellow, green).
I could very well have done that. I also could have made a program that did the exact same thing, but with only one class. The
objective of my assignment; however, is to create classes that have their own methods that can be called by other classes.
As far as the "
if" idea goes, my class hasn't gone over loops yet, so I can't use that route to complete the program, so I used the "hint" my professor gave, which was to create a new
ActionListener for each new light and delete the old
ActionListener; therefore, creating my own loop, in a way.
cjgone wrote:But I look at teh code and I don't see anything that means anything, like wtf is redlistener.
RedListener, like the other listeners, is a tool (instance of an
ActionListener) used in Java to detect user interaction. I created my own listeners so I could declare a time for each light to come on and when, then to destroy the previous light (to make it turn off).
cjgone wrote:And how does the drawing work? Only need to change one variable and it auto updates or something? =X Is there like some function like a main statement that calls the initializations and stuff? And why is like everything in a class.
I'm not sure I understand your questions correctly. However, classes are what are used in object-oriented programming. They are basically what create the methods and functions that can be used when creating programs (or other classes).
I also added a neat little gimmick to give the light a more "real" touch. I made so when the light has a specific light "on" the other lights look like they're "off" by drawing darker circles before drawing the light that is "on". Take a look at the .gif file I attached and notice how my green light is brighter than the rest. Once the "Change" button is pressed, the green light turns "off" (it simply is destroyed and the darker colored circle is drawn), and the yellow light turns "on" and will be brighter than the rest. I tried to upload my .jar file so you could understand exactly what my program does, but I guess the forum doesn't accept that type of file.