How do you make a Traffic Light?
I'm looking to make a flashinglight class using a next( ) method. This will change the color from red, yellow and green. I need a counter that increments each time a next( ) message is received and then resets to zero when it reaches 3. The counter needs to access a color array.
Any help will be much appreciated.
What do you have so far, other than your spec?
> What do you have so far, other than your spec?He has the hope that some idiot will do his homework for him and allow him to spend more time in the pub.
This is a different version I came up with but I couldn't figure out how to include a the next method and counter. I have the color array done I think.
import animax.*;
import java.awt.*;
class light
{
public static void main(String args[])
{
AnimationFrame f = new AnimationFrame(500,500);
Container c = f.getContentPane();
Color[] colors =
{
Color.red,
Color.yellow,
Color.green
};
RoundRectGraphic graphic;
Counter counter;
graphic = new RoundRectGraphic(200,200,100,colors,c);
counter = new Counter(0,colors.length - 1);
counter.addGraphic(graphic);
Player player = new Player(500);
player.addListener(counter);
player.play();
}
}
Message was edited by:
helloworld2007
> He has the hope that some idiot will do his homework> for him and allow him to spend more time in the pub.I may just be that idiot... nah, im going to enjoy the snow!
First off, Class names should start with an uppercase letter.
"light" is not a (socially, lol) acceptable Class name.
Second, when posting code on the forums you should weed out
extraneous code and make the code compilable if possible.
Third, use code tags --> [ code] [/ code] (no spaces)
Fourth, consider String[] args over String args[].
A reader can more easily identify the type as an array.
Fifth, consider Color.RED over Color.red.
Constants are typically all uppercase.
Sixth, your Color array is initialized in the main() method which is
a huge mistake. It should be an instance variable of the Light class.
Here, ive cleaned up your code a bit:
import java.awt.*;
public class Light{
public static void main(String[] args){
/*
AnimationFrame f = new AnimationFrame(500,500);
Container c = f.getContentPane();
RoundRectGraphic graphic;
Counter counter;
graphic = new RoundRectGraphic(200,200,100,colors,c);
counter = new Counter(0,colors.length - 1);
counter.addGraphic(graphic);
Player player = new Player(500);
player.addListener(counter);
player.play();
*/
}
public void next(){
}
Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
}
Thanks I will take note of your advice. I only started using java last week.
Well come on. Get to it. Lets figure this out.The code is actually almost done.It looks to me like all you need is a counter and a littlecode in the next method to increment and reset it.
Do I need something like:public void next()counter++if(counter==3)counter = 0Probably completely wrong. :(
no way dude youre so close.
public void next(){
counter ++;
if(counter > 3){
counter = 1;
}
}
int counter;
Cheers. So how do I integrate this into the final code, using the color array?
> Cheers. So how do I integrate this into the final
> code, using the color array?
import java.awt.*;
// import animax.*;
public class Light{
public static void main(String[] args){
/*
AnimationFrame f = new AnimationFrame(500,500);
Container c = f.getContentPane();
RoundRectGraphic graphic;
Counter counter;
graphic = new RoundRectGraphic(200,200,100,colors,c);
counter = new Counter(0,colors.length - 1);
counter.addGraphic(graphic);
Player player = new Player(500);
player.addListener(counter);
player.play();
*/
Light light = new Light();
for(int i = 0; i < 5; i++){
light.next();
System.out.println("Light: " + light.getColor().toString());
}
}
public void next(){
counter ++;
if(counter > 2){
counter = 0;
}
}
public Color getColor(){
return colors[counter];
}
Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
int counter = 0;
}
Cheers. I'll see if it works.
>I only started using java last week.Then why use graphics so early?
That's what we're being taught.
BTW, I made a slip up. The Color array should be static (and possibly final).
static Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
then
public Color getColor(){
return Light.colors[counter];
}
Well whoever is teaching you is not using a decent approach IMO. There is not mention of Graphics int the SCJP exam at all, yet it is still hard to pass and a lot of ground to cover for a new developer. You can happily program in Java professionally without ever using Swing or AWT.
Do I not need the animax import?It doesn't seem to be coming up with anything.Do I not need a rectangle or something to display the colors?
Why would any of us know about your Animax class?You are here for help on making a next() method.Anything else is your responsibility.
I got it working now. :) Thanks for the help TuringPest.