Traffic Light

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 11
Reputation: Techboy52 is an unknown quantity at this point 
Solved Threads: 0
Techboy52 Techboy52 is offline Offline
Newbie Poster

Traffic Light

 
0
  #1
Apr 8th, 2008
//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 );
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 11
Reputation: Techboy52 is an unknown quantity at this point 
Solved Threads: 0
Techboy52 Techboy52 is offline Offline
Newbie Poster

Re: Traffic Light

 
0
  #2
Apr 9th, 2008
It compiles just need the buttons to turn the red,green, and yellow light on and off
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Traffic Light

 
0
  #3
Apr 9th, 2008
Originally Posted by Techboy52 View Post
It compiles just need the buttons to turn the red,green, and yellow light on and off
This doesn't compile for me.
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import javax.swing.border.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. public class TrafficLight extends JPanel {
  8. public TrafficLight() {
  9. JButton btn1 = new JButton("Green");
  10. JButton btn2 = new JButton("Yellow");
  11. JButton btn3 = new JButton("Red");
  12. btn1.addActionListener(new ButtonListener());
  13. add(btn1);
  14. btn2.addActionListener(new ButtonListener());
  15. add(btn2);
  16. btn2.addActionListener(new ButtonListener());
  17. add(btn2);
  18.  
  19. }
  20.  
  21. public static void main(String[] args) {
  22. JFrame f = new JFrame("Traffic Light");
  23. JPanel lights = new JPanel( new GridLayout(0,3) );
  24. lights.add( new TrafficSignal(Color.green) );
  25. lights.add( new TrafficSignal(Color.yellow) );
  26. lights.add( new TrafficSignal(Color.red) );
  27. // lights.add(new JButton("Green"));
  28. // lights.add(new JButton("Yellow"));
  29. // lights.add(new JButton("Red"));
  30.  
  31.  
  32. f.setContentPane( lights );
  33. f.pack();
  34. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35. f.setLocationRelativeTo(null);
  36. f.setVisible(true);
  37. }
  38. }
  39.  
  40. class ButtonListener implements ActionListener {
  41. ButtonListener() {
  42. }
  43.  
  44. public void actionPerformed(ActionEvent e) {
  45. if (e.getActionCommand().equals("Green")) {
  46. btn1.setEnabled(true);
  47. btn2.setEnabled(false);
  48. btn3.setEnabled(false);
  49. }
  50. if (e.getActionCommand().equals("Yellow")) {
  51. btn2.setEnabled(true);
  52. btn1.setEnabled(false);
  53. btn3.setEnabled(false);
  54. } else {
  55. btn3.setEnabled(true);
  56. btn1.setEnabled(false);
  57. btn2.setEnabled(false);
  58. }
  59. }
  60. class TrafficSignal extends JPanel
  61.  
  62.  
  63. {
  64.  
  65. Color on;
  66. int radius = 75;
  67. int border = 10;
  68. boolean active;
  69.  
  70.  
  71.  
  72.  
  73. TrafficSignal(Color color) {
  74. on = color;
  75. active = true;
  76. }
  77.  
  78. public Dimension getPreferredSize() {
  79. int size = (radius+border)*2;
  80. return new Dimension( size, size );
  81. }
  82.  
  83. public void paintComponent(Graphics g) {
  84. g.setColor( Color.black );
  85. g.fillRect(0,0,getWidth(),getHeight());
  86.  
  87. if (active) {
  88. g.setColor( on );
  89. } else {
  90. g.setColor( on.darker().darker().darker() );
  91. }
  92. g.fillOval( border,border,2*radius,2*radius );
  93. }
  94. }
  95. }

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.
Last edited by VernonDozier; Apr 9th, 2008 at 1:44 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 11
Reputation: Techboy52 is an unknown quantity at this point 
Solved Threads: 0
Techboy52 Techboy52 is offline Offline
Newbie Poster

Re: Traffic Light

 
0
  #4
Apr 9th, 2008
ok thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 11
Reputation: Techboy52 is an unknown quantity at this point 
Solved Threads: 0
Techboy52 Techboy52 is offline Offline
Newbie Poster

Re: Traffic Light

 
0
  #5
Apr 9th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,711
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Traffic Light

 
0
  #6
Apr 9th, 2008
What did you try to do? and what errors do you get?
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Traffic Light

 
0
  #7
Apr 9th, 2008
Try changing this:
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import javax.swing.border.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. public class TrafficLight extends JPanel {
  8. public TrafficLight() {
  9. JButton btn1 = new JButton("Green");
  10. JButton btn2 = new JButton("Yellow");
  11. JButton btn3 = new JButton("Red");
  12. btn1.addActionListener(new ButtonListener());
  13. add(btn1);
  14. btn2.addActionListener(new ButtonListener());
  15. add(btn2);
  16. btn2.addActionListener(new ButtonListener());
  17. add(btn2);
  18.  
  19. }
to this:
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import javax.swing.border.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. public class TrafficLight extends JPanel {
  8.  
  9. JButton btn1;
  10. JButton btn2;
  11. JButton btn3;
  12.  
  13. public TrafficLight() {
  14. btn1 = new JButton("Green");
  15. btn2 = new JButton("Yellow");
  16. btn3 = new JButton("Red");
  17. btn1.addActionListener(new ButtonListener());
  18. add(btn1);
  19. btn2.addActionListener(new ButtonListener());
  20. add(btn2);
  21. btn2.addActionListener(new ButtonListener());
  22. add(btn2);
  23.  
  24. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: Traffic Light

 
0
  #8
Apr 9th, 2008
Try this: I changed alittle bit your program, but this should work

  1. import java.awt.*;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7.  
  8. public class TrafficLight extends JFrame implements ActionListener {
  9. private static JButton btn1 = null;
  10. private static JButton btn2 = null;
  11. private static JButton btn3 = null;
  12.  
  13. private static TrafficSignal green = new TrafficSignal(Color.green);
  14. private static TrafficSignal yellow = new TrafficSignal(Color.yellow);
  15. private static TrafficSignal red = new TrafficSignal(Color.red);
  16.  
  17. public TrafficLight(){
  18. super("Traffic Light");
  19. getContentPane().setLayout(new GridLayout(2, 1));
  20. btn1 = new JButton("Green");
  21. btn2 = new JButton("Yellow");
  22. btn3 = new JButton("Red");
  23. btn1.addActionListener(this);
  24. btn2.addActionListener(this);
  25. btn3.addActionListener(this);
  26.  
  27. green.turnOn(true);
  28. yellow.turnOn(false);
  29. red.turnOn(false);
  30.  
  31. JPanel lights = new JPanel( new FlowLayout() );
  32. lights.add( green );
  33. lights.add( yellow );
  34. lights.add( red );
  35. JPanel btnPane = new JPanel(new FlowLayout());
  36. btnPane.add(btn1);
  37. btnPane.add(btn2);
  38. btnPane.add(btn3);
  39.  
  40. getContentPane().add(lights);
  41. getContentPane().add(btnPane);
  42. pack();
  43. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44. setLocationRelativeTo(null);
  45. }
  46.  
  47. public static void main(String[] args){
  48. TrafficLight tl = new TrafficLight();
  49. tl.setVisible(true);
  50. }
  51.  
  52.  
  53. public void actionPerformed(ActionEvent e){
  54. if (e.getSource() == btn1){
  55. green.turnOn(true);
  56. yellow.turnOn(false);
  57. red.turnOn(false);
  58. } else if (e.getSource() == btn2){
  59. yellow.turnOn(true);
  60. green.turnOn(false);
  61. red.turnOn(false);
  62. } else if (e.getSource() == btn3){
  63. red.turnOn(true);
  64. yellow.turnOn(false);
  65. green.turnOn(false);
  66. }
  67. }
  68. }
  69.  
  70. class TrafficSignal extends JPanel {
  71.  
  72. Color on;
  73. int radius = 75;
  74. int border = 10;
  75. boolean active;
  76.  
  77. TrafficSignal(Color color){
  78. on = color;
  79. active = true;
  80. }
  81.  
  82. public void turnOn(boolean a) {
  83. active = a;
  84. repaint();
  85. }
  86.  
  87. public Dimension getPreferredSize(){
  88. int size = (radius+border)*2;
  89. return new Dimension( size, size );
  90. }
  91.  
  92. public void paintComponent(Graphics g){
  93. g.setColor( Color.black );
  94. g.fillRect(0,0,getWidth(),getHeight());
  95.  
  96. if (active){
  97. g.setColor( on );
  98. } else {
  99. g.setColor( on.darker().darker().darker() );
  100. }
  101. g.fillOval( border,border,2*radius,2*radius );
  102. }
  103. }
Last edited by new_2_java; Apr 9th, 2008 at 3:50 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC