Java Newbie need help

Discussion in 'Java' started by Bobbb, Oct 28, 2010.

  1. Bobbb

    Bobbb

    Joined:
    Oct 28, 2010
    Messages:
    1
    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();
    }
    }
    }
    Bobbb, Oct 28, 2010
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    547
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  2. /usr/ceo
    Replies:
    32
    Views:
    1,894
    Mark Space
    Sep 15, 2008
  3. ElementX
    Replies:
    9
    Views:
    482
    RedGrittyBrick
    Oct 1, 2008
  4. Ty Foster
    Replies:
    0
    Views:
    402
    Ty Foster
    Sep 22, 2010
  5. Stuart Palmer

    Newbie Question!! NEED JAVA CHAT APPLET

    Stuart Palmer, Jun 28, 2003, in forum: Javascript
    Replies:
    1
    Views:
    113
    !............®.............!
    Jun 28, 2003
Loading...

Share This Page