We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,401 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion   Closed to New Replies

Traffic Light

//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 );
 }
 }
}
7
Contributors
15
Replies
4 Years
Discussion Span
7 Months Ago
Last Updated
19
Views
Techboy52
Newbie Poster
13 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

Techboy52
Newbie Poster
13 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18

ok thanks

Techboy52
Newbie Poster
13 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

Techboy52
Newbie Poster
13 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

javaAddict
Nearly a Senior Poster
Team Colleague
3,338 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 450
Skill Endorsements: 7

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.

VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18

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 );
    }
}
new_2_java
Junior Poster
127 posts since Apr 2007
Reputation Points: 7
Solved Threads: 6
Skill Endorsements: 0

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

amitie.boo
Newbie Poster
5 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

stultuske
Industrious Poster
4,380 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

can this work on bluej

amitie.boo
Newbie Poster
5 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

stultuske
Industrious Poster
4,380 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

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

amitie.boo
Newbie Poster
5 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

stultuske
Industrious Poster
4,380 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

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
Newbie Poster
5 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,525 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
 
© 2013 DaniWeb® LLC
Page rendered in 0.1290 seconds using 2.87MB