Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners how to make traffic light code in java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


inder
how to make traffic light code in java
1 Answer(s)      3 years and 7 months ago
Posted in : Java Beginners


what is code of traffic light in java


Advertisement
View Answers

May 28, 2011 at 4:06 PM


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

public class TrafficLight extends JFrame implements ActionListener {
    JButton b1, b2, b3;

      Signal green = new Signal(Color.green);
      Signal yellow = new Signal(Color.yellow);
      Signal red = new Signal(Color.red);

    public TrafficLight(){
        super("Traffic Light");
        getContentPane().setLayout(new GridLayout(2, 1));
        b1 = new JButton("Red");
        b2 = new JButton("Yellow");
        b3 = new JButton("Green");
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);        

        green.turnOn(false);
        yellow.turnOn(false);
        red.turnOn(true);

        JPanel p1 = new JPanel(new GridLayout(3,1));
        p1.add(red);
        p1.add(yellow);
        p1.add(green);
        JPanel p2 = new JPanel(new FlowLayout());
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);

        getContentPane().add(p1);
        getContentPane().add(p2);
        pack();
        }


    public static void main(String[] args){
        TrafficLight tl = new TrafficLight();        
        tl.setVisible(true);
    }    
    public void actionPerformed(ActionEvent e){        
        if (e.getSource() == b1){
            green.turnOn(false);            
            yellow.turnOn(false);
            red.turnOn(true);
        } else if (e.getSource() == b2){
            yellow.turnOn(true);            
            green.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == b3){
            red.turnOn(false);            
            yellow.turnOn(false);
            green.turnOn(true);
        }
    }
}     
class Signal extends JPanel{

    Color on;
    int radius = 40;
    int border = 10;
    boolean change;

    Signal(Color color){
        on = color;
        change = true;
    }

    public void turnOn(boolean a){
        change = 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 (change){
            g.setColor( on );
        } else {
            g.setColor( on.darker().darker().darker() );
        }
        g.fillOval( border,border,2*radius,2*radius );
    }
}


Related Tutorials/Questions & Answers:
how to make traffic light code in java
how to make traffic light code in java  what is code of traffic light in java
How to design animated traffic lights
How to design animated traffic lights You would have seen an animated traffic light on the road, I am interested with this so I have decided to design it. You can also make it by using this interesting example so
Advertisements
code for traffic control system simulation
code for traffic control system simulation  any one suggest which functions are used to develop a graphical view to traffic control system using awt and swing
Increase Web Traffic,How to Increase Web Traffic,Increase Website Traffic,Free Tips to Increase Website Traffic
How to Increase Web Traffic This tutorial will help you increase your website traffic. Today website success depends on the steady flow of the web traffic... to increase the traffic to your web site, then you have to look towards
How to Track Website Traffic?
whatever to roll a great SEO campaign everything boils down to your website traffic that actually translates into your website revenue. Thus to track website traffic... traffic and measuring various qualitative attributes is important for your web
How to make query and abstraction in Java
How to make query and abstraction in Java  Hi, I read the lessons... write. I am very beginner in Java I don't know anything in it. I am using PostgreSQL database, and I want to make a query and abstraction for a table. Could
How To Make This Program - Java Beginners
How To Make This Program  How To Make Java Calculation program I want to Write a program in Java that calculates the sum of digits of an input number, prints it, then calculates the sum... output: 59 14 5 please help to make
How to make the single click as double click in java
How to make the single click as double click in java  How to make the single click as double click in java... By clicking on a tab, the tab should respond as clicked twice i.e (double click on the tab/button)... Its very urgent
How to make directory in java
Description: This example demonstrate how to create a directory at specified path. Code: import java.io.File; public ... is that it will make a dir at root directory of C drive
How to make Time picker in Java Swing
How to make Time picker in Java Swing  Hello Sir, Thank u so much for such a quick reply . I followed date picker tutorial and completed..., minutes ,seconds, am,pm etc. Plz send me the code for time picker . Thanks
How to make Time picker in Java Swing
How to make Time picker in Java Swing  Hello Sir, Thank u so much for such a quick reply . I followed date picker tutorial and completed..., minutes ,seconds, am,pm etc. Plz send me the code for time picker . Thanks
How to make a file in java
Description: This example demonstrate how to make a file and write string in it. To create a file in java we use the File Class. The instance of the File... will never change.  Code: import java.io.File
How to make a design document for exception handling in java
How to make a design document for exception handling in java  How to make a design document for exception handling
how to make this java mail works? - Java Beginners
how to make this java mail works?  Dear experts, Recently, I... don't have a clue how to make this file work at all, even after reading... on this java file as below :- package com.XmlEcomBook.Chap05; import java.util.
Traffic Simulator
Traffic Simulator  Hi, I am doing a project on creating a traffic simulator for a particular road junction in java. I am looking for ideas on how I would go about recreating the visual of the junction in which I can have moving
how to make an unreachable object as reachable - Java Beginners
how to make an unreachable object as reachable  i know that an object is garbagecollected when it is unreachable to the programmer but my question is how that unreachable object which was garbage collected will become
how to make a matrix like datagrid in ide in java pls help
how to make a matrix like datagrid in ide in java pls help  how to make a datagrid mainly matrix in ajava standalone app
How to make frame in java
How to make frame in java We are going to discus about frame in java. In this example We have make frame in java Swing. We have created JFrame class...[]) { new FrameExample ().start(); } } Output Download Source Code
How To Make Executable Jar File For Java P{roject - Java Beginners
How To Make Executable Jar File For Java P{roject   Hello Sir I want Help to make Executable File for java Project How I can Make it?  Hi Friend, Try the following code: import java.io.*; import java.util.jar.
Code how to? - Java Beginners
Code how to?  how to access a separate form using a JButton ? Also how to let another Jbutton access an excel spreadsheet for viewing? Thanks
how to make this pattern???
how to make this pattern???  how to make following pattern in javascript
How to make elements invisible ?
How to make elements invisible ?   How to make elements invisible
how make ID - Ajax
how make ID  how make a ID in eyeball chat
Code how to? - Java Beginners
Code how to?  how to access a separate form using a JButton ? Also how to let another Jbutton access an excel spreadsheet for viewing? Thanks  Hi Friend, We are providing you a code that will display a form
how can i make a session invalidate in java when browser is closed
how can i make a session invalidate in java when browser is closed   how can i make a session invalidate in java when browser is closed   ... to make the session invalid when browser/tab is closed like gmail. Thanks
How to make one waiting thread for lock - Java Beginners
How to make one waiting thread for lock  Hi Friends, I am new to java. Can u explain about thread concept.How i know one thread is locked, and how can i force or acquire another thread for lock . Finally how to know thread
How to make a file read-only in java
How to make a file read-only in java A read-only file is any file...(); method is used. In the page you learned how to make a file read only... will not be able to make changes to it. They can be deleted and moved. You can make
how to make exampage in jsp ?
how to make exampage in jsp ?  how to make a online exam page in jsp and servelet
how make excel
how make excel  how make excel spreadsheet IN JAVA. please send this code argently   Hello Friend, Try the following code: import...); } } } For the above code, you need Apache POI library. Thanks
How to make a gzip file in java.
Make a gzip file from any given file. In this tutorial, we will discuss how... Code: import java.io.*; import java.util.zip....; Following is the output if you run the application: C:\>java
JAVA code For
JAVA code For   JAVA code For "Traffic signals Identification for vehicles
Advertisements
 

 

 

DMCA.com