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



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

Have Programming Question? Ask it here!
 
 
 


inder
how to make traffic light code in java
1 Answer(s)      3 years and 3 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 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 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 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
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 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.
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
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 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  Hi Friend, We are providing you a code that will display a form
how make ID - Ajax
how make ID  how make a ID in eyeball chat
How to make elements invisible ?
How to make elements invisible ?   How to make elements invisible
how to make this pattern???
how to make this pattern???  how to make following pattern in javascript
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
JAVA code For
JAVA code For   JAVA code For "Traffic signals Identification for vehicles
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
how create package of this java code
how create package of this java code  Hi,i have email code in java which is working fine with out making package but when i want to make package of this code its stop working.Its showing exception while I execute it.I have to use
how create package of this java code
how create package of this java code  Hi,i have email code in java which is working fine with out making package but when i want to make package of this code its stop working.Its showing exception while I execute it.I have to use
How to make a list from file text in J2ME - Java Beginners
How to make a list from file text in J2ME  I was trying to make a method that read file from text and make a list of it, I have tried ReadHelpText method and want to make some improvement, I've tried like this private String
how to make a program on array
how to make a program on array  When you make a program on array that the element will move downward and upward and when you input twice 0 then thats the time that it will not move. pls. give me a formula...tnx
How to make selectOneMenu scrollable
How to make selectOneMenu scrollable  how to make the selectOneMenu scrollable? I have around 20 values in drop down and i want the scrollable functionality after 5 values. Kindly reply as soon as possible. Thanks Shikha
how to make multiple rectangles
how to make multiple rectangles  I,m a beginner , m sorry.... please help my code as of now:(but this doesn't work) import java.awt.Color...(JFrame.EXIT_ON_CLOSE); //Set JFrame size setSize(400,400); //Make JFrame
how to make use of jerry's java image processing filter in java application?is there anyway to do this?
how to make use of jerry's java image processing filter in java application?is there anyway to do this?   how to make use of jerry's java image processing filter in java application?is there anyway to do
how to generate code - Java Beginners
how to generate code   hi friends, this is poornima i want to generate code about online shopping by using servlets,jsp,html,jdbc and eclipse (IDE) could you please tell me about what are steps included. Thanks in advance
How to convert this Java code into GUI?
How to convert this Java code into GUI?   import java.util.Scanner; public class StudentMarks { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks
how to make paging with function ?
how to make paging with function ?  how to make paging with function ?   //this page is display.php <?php</p> $con=mysql_connect("localhost","root",""); if(!$con) { die("you can connect please check". mysql
How to make a zip file in java
Description: In this example we will discuss about how to create zip file from the text file. In the following code  ZipOutputStream class from... zip file. Code: import java.io.*; import java.util.zip.
How to avoid Java Code in JSP-Files?
How to avoid Java Code in JSP-Files?  How to avoid Java Code in JSP-Files
how to make a field left justification
how to make a field left justification  how to make a field left justification.need code.thanks in advance
how to make a label left alignment?
how to make a label left alignment?  how to make a label left alignment
How to make a chain, make a chain, chain
How to make a chain       To learn how to design a chain in the photoshop has become so easy by this example, it has some important instruction to make it so follow
How to make bubbles, make bubbles, bubbles
How to make bubbles      ... to make it easily to draw. New File: First take colored background file. Draw... Overlay, use same both settings as given below. Duplicate: Now make
how to make paging with class and ajax
how to make paging with class and ajax  paging with class and ajax
How to make a transparent text
How to make a transparent text Learn how to make a transparent...) and make formatting as looking here. Write Text: Now write your text as you like. I have written here "Text". After writing text make copy (Ctr
How to make halo effect
How to make halo effect       Now get ready to learn a simple way to make a halo effect on the picture, follow to make it. Take a picture: Capture any picture to give
 

 

 

DMCA.com