//Hey I need some help
//Write a JAVA application that displays a traffic light (three circles inside a rectangle with //red, yellow, and green color) and three buttons with red, yellow and green titles. The //application should turn the appropriate light on when the appropriate button is clicked. Two //lights can not be on at the same time. 
//This program compiles but it does not implement my action listner buttons can anyone help!!!


import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

 public class TrafficLight extends JPanel
 {
    public TrafficLight()
    {
    JButton btn1 = new JButton("Green");
    JButton btn2 = new JButton("Yellow");
    JButton btn3 = new JButton("Red");
    btn1.addActionListener(new ButtonListener());
    add(btn1);
    btn2.addActionListener(new ButtonListener());
    add(btn2);
    btn2.addActionListener(new ButtonListener());
    add(btn2);

    }

 public static void main(String[] args) 
 {
    JFrame f = new JFrame("Traffic Light");
    JPanel lights = new JPanel( new GridLayout(0,3) );
    lights.add( new TrafficSignal(Color.green) );
    lights.add( new TrafficSignal(Color.yellow) );
    lights.add( new TrafficSignal(Color.red) );
 // lights.add(new JButton("Green"));
 // lights.add(new JButton("Yellow"));
  //  lights.add(new JButton("Red"));


    f.setContentPane( lights );
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
 }
}

class ButtonListener implements ActionListener 
{
  ButtonListener() 
  {
  }

  public void actionPerformed(ActionEvent e)
         {
        if (e.getActionCommand().equals("Green")) 
            {
            btn1.setEnabled(true);
            btn2.setEnabled(false);
            btn3.setEnabled(false);
        } 
        if (e.getActionCommand().equals("Yellow"))
        {
            btn2.setEnabled(true);
            btn1.setEnabled(false);
            btn3.setEnabled(false);
        }
            else 
        {
            btn3.setEnabled(true);
            btn1.setEnabled(false);
            btn2.setEnabled(false);
        }
    }
  class TrafficSignal extends JPanel


 {

        Color on;
        int radius = 75;
        int border = 10;
        boolean active;




 TrafficSignal(Color color) 
 {
    on = color;
    active = true;
 }

 public Dimension getPreferredSize() 
 {
    int size = (radius+border)*2;
    return new Dimension( size, size );
 }

 public void paintComponent(Graphics g) 
 {
  g.setColor( Color.black );
    g.fillRect(0,0,getWidth(),getHeight());

 if (active) 
 {
 g.setColor( on );
 } 
 else 
 {
 g.setColor( on.darker().darker().darker() );
 }
 g.fillOval( border,border,2*radius,2*radius );
 }
 }
}

Recommended Answers

All 15 Replies

It compiles just need the buttons to turn the red,green, and yellow light on and off

It compiles just need the buttons to turn the red,green, and yellow light on and off

This doesn't compile for me.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrafficLight extends JPanel {
    public TrafficLight() {
        JButton btn1 = new JButton("Green");
        JButton btn2 = new JButton("Yellow");
        JButton btn3 = new JButton("Red");
        btn1.addActionListener(new ButtonListener());
        add(btn1);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        
    }
    
    public static void main(String[] args) {
        JFrame f = new JFrame("Traffic Light");
        JPanel lights = new JPanel( new GridLayout(0,3) );
        lights.add( new TrafficSignal(Color.green) );
        lights.add( new TrafficSignal(Color.yellow) );
        lights.add( new TrafficSignal(Color.red) );
// lights.add(new JButton("Green"));
// lights.add(new JButton("Yellow"));
// lights.add(new JButton("Red"));
        
        
        f.setContentPane( lights );
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

class ButtonListener implements ActionListener {
    ButtonListener() {
    }
    
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Green")) {
            btn1.setEnabled(true);
            btn2.setEnabled(false);
            btn3.setEnabled(false);
        }
        if (e.getActionCommand().equals("Yellow")) {
            btn2.setEnabled(true);
            btn1.setEnabled(false);
            btn3.setEnabled(false);
        } else {
            btn3.setEnabled(true);
            btn1.setEnabled(false);
            btn2.setEnabled(false);
        }
    }
    class TrafficSignal extends JPanel
            
            
    {
        
        Color on;
        int radius = 75;
        int border = 10;
        boolean active;
        
        
        
        
        TrafficSignal(Color color) {
            on = color;
            active = true;
        }
        
        public Dimension getPreferredSize() {
            int size = (radius+border)*2;
            return new Dimension( size, size );
        }
        
        public void paintComponent(Graphics g) {
            g.setColor( Color.black );
            g.fillRect(0,0,getWidth(),getHeight());
            
            if (active) {
                g.setColor( on );
            } else {
                g.setColor( on.darker().darker().darker() );
            }
            g.fillOval( border,border,2*radius,2*radius );
        }
    }
}

The compiler can't find the buttons in actionPerformed in lines 45 - 58. I'm assuming those are the buttons that are defined in your constructor in lines 9 - 11. They go out of scope as soon as the constructor has been implemented.

ok thanks

well I was wondering is there a way to fix it to get the buttons to turn the lights on and off. When I try to move the code within the scope i get some weird errors.
Need help

What did you try to do? and what errors do you get?

Try changing this:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrafficLight extends JPanel {
    public TrafficLight() {
        JButton btn1 = new JButton("Green");
        JButton btn2 = new JButton("Yellow");
        JButton btn3 = new JButton("Red");
        btn1.addActionListener(new ButtonListener());
        add(btn1);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        
    }

to this:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrafficLight extends JPanel {

    JButton btn1;
    JButton btn2;
    JButton btn3;

    public TrafficLight() {
        btn1 = new JButton("Green");
        btn2 = new JButton("Yellow");
        btn3 = new JButton("Red");
        btn1.addActionListener(new ButtonListener());
        add(btn1);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        btn2.addActionListener(new ButtonListener());
        add(btn2);
        
    }

You are now declaring btn1, btn2, and btn3 outside of the constructor in a way that they'll be available for the whole class to use. See if that helps the scoping problem.

Try this: I changed alittle bit your program, but this should work

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrafficLight extends JFrame implements ActionListener {
    private static JButton btn1 = null;
    private static JButton btn2 = null;
    private static JButton btn3 = null;
    
    private static TrafficSignal green = new TrafficSignal(Color.green);
    private static TrafficSignal yellow = new TrafficSignal(Color.yellow);
    private static TrafficSignal red = new TrafficSignal(Color.red);
    
    public TrafficLight(){
        super("Traffic Light");
        getContentPane().setLayout(new GridLayout(2, 1));
        btn1 = new JButton("Green");
        btn2 = new JButton("Yellow");
        btn3 = new JButton("Red");
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);        
        
        green.turnOn(true);
        yellow.turnOn(false);
        red.turnOn(false);
        
        JPanel lights = new JPanel( new FlowLayout() );
        lights.add( green );
        lights.add( yellow );
        lights.add( red );
        JPanel btnPane = new JPanel(new FlowLayout());
        btnPane.add(btn1);
        btnPane.add(btn2);
        btnPane.add(btn3);
        
        getContentPane().add(lights);
        getContentPane().add(btnPane);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);            
    }
    
    public static void main(String[] args){
        TrafficLight tl = new TrafficLight();        
        tl.setVisible(true);
    }

    
    public void actionPerformed(ActionEvent e){        
        if (e.getSource() == btn1){
            green.turnOn(true);            
            yellow.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == btn2){
            yellow.turnOn(true);            
            green.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == btn3){
            red.turnOn(true);            
            yellow.turnOn(false);
            green.turnOn(false);
        }
    }
}    
    
class TrafficSignal extends JPanel {
    
    Color on;
    int radius = 75;
    int border = 10;
    boolean active;
    
    TrafficSignal(Color color){
        on = color;
        active = true;
    }
    
    public void turnOn(boolean a) {
        active = a;
        repaint();        
    }
    
    public Dimension getPreferredSize(){
        int size = (radius+border)*2;
        return new Dimension( size, size );
    }
    
    public void paintComponent(Graphics g){
        g.setColor( Color.black );
        g.fillRect(0,0,getWidth(),getHeight());
    
        if (active){
            g.setColor( on );
        } else {
            g.setColor( on.darker().darker().darker() );
        }
        g.fillOval( border,border,2*radius,2*radius );
    }
}

i wanna try this but can u tell me what shud be the input so that i can get the output

if you want to try this: try this.
if you have questions about how: don't revive a dead thread, start a new one.

can this work on bluej

don't see why not. still no reason to keep this thread alive.

this is compiling !!! and can u tell me what shud be written in the method call after creating an object?

no, I can not. because I don't really know what code you are running, or what it is you are trying to do.
but, again: for new questions -> create a new thread.

the above source code is compiling but i dono what has to be done so that i can see the output of traffic lights!! can u plzz tell me ?i hav to submit this project on traffic lights to my teacher

amitie.boo
If this is a project that have to submit to your teacher then you can't just copy code from this web site. That's called cheating. We don't help peaple cheat here.

If you need any help with code that you have written yourself please start anther thread. This thread is closed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.