Java Newbie need help

Joined
Oct 28, 2010
Messages
1
Reaction score
0
Hello all, attempting to make a program for an assignment that models a traffic light. I followed the instructions perfectly but for some reason my button will not change the active lights color no matter what i do. Any insight on this would be awesome. Here is my code so far, i have three separate files and i feel as though I'm missing something crucial. My thoughts are that the switch statement is not working but I've tried messing with it and at least in this configuration i get the green circle to fill but it wont change upon pressing the button....

import java.awt.*;
import javax.swing.*;

public class TrafficLightDriver
{
//------------------------------------------------
public static void main (String[] args)

{
JFrame frame = new JFrame ("TrafficLight");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new TrafficControlPanel());
frame.setPreferredSize (new Dimension(300, 200));

frame.pack();
frame.setVisible(true);
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import javax.swing.*;

public class TrafficLight extends JPanel
{
private int on;
private Color color1, color2, color3;
private final int GREEN = 1;
private final int AMBER = 2;
private final int RED = 3;

public TrafficLight(int state)
{
on = state;
//logic to decide what colors to fill based on what state the light is in
if (on==GREEN)
{
color1 = Color.green;
color2 = Color.black;
color3 = Color.black;
}
else if (on==AMBER)
{
color1 = Color.black;
color2 = Color.yellow;
color3 = Color.black;
}
else if (on==RED)
{
color1 = Color.black;
color2 = Color.black;
color3 = Color.green;
}
}
public void paint(Graphics page)
{

//draws the traffic light
super.paintComponent(page);

page.setColor(Color.gray);
page.fillRect(10,10,50,100);

page.setColor(color1);
page.fillOval(20,20,20,20);

page.setColor(color2);
page.fillOval(20,40,20,20);

page.setColor(color3);
page.fillOval(20,60,20,20);


}
public void change()
{
switch(on)
{
case GREEN:
on= on + 1;
break;
case AMBER:
on= on +1;
break;
case RED:
on = on -2;
break;
}
repaint();
}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TrafficControlPanel extends JPanel
{

private JButton push;
TrafficLight trafficLight1;

public TrafficControlPanel()
{

push = new JButton("Change Light");
push.addActionListener (new ButtonListener());
trafficLight1 = new TrafficLight(1);

add(push);

setPreferredSize(new Dimension(300,200));
}
public void paintComponent (Graphics page)
{
super.paintComponent(page);

trafficLight1.paint(page);
}

//Listener for button push
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
trafficLight1.change();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top