| | |
Traffic Light
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 11
Reputation:
Solved Threads: 0
//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 );
}
}
}
//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 );
}
}
}
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
•
•
•
•
It compiles just need the buttons to turn the red,green, and yellow light on and off
JAVA Syntax (Toggle Plain Text)
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.
Last edited by VernonDozier; Apr 9th, 2008 at 1:44 am.
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
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
Try changing this:
to this:
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.
JAVA Syntax (Toggle Plain Text)
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); }
JAVA Syntax (Toggle Plain Text)
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.
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
Try this: I changed alittle bit your program, but this should work
Java Syntax (Toggle Plain Text)
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 ); } }
Last edited by new_2_java; Apr 9th, 2008 at 3:50 pm.
![]() |
Similar Threads
- PHP without server (PHP)
- Java code to display a GUI (Java)
- Buy traffic (Promotion and Marketing Plans)
- Help, Help with Traffic Simulation program (Java)
- The Vending Machine Game (Posting Games)
- HJT Amaena (Viruses, Spyware and other Nasties)
- Ethernet Adapter problem? (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: help in java networking
- Next Thread: How to resize an image in JAVA
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






