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

 
 


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

what is code of traffic light in java

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 Pages:
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
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
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
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 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
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 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 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 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
java code  sir how to merge the cells in excel using java code please help me and also how to make the text placed in the cell to be center
java code
java code  sir how to merge the cells in excel using java code please help me and also how to make the text placed in the cell to be center
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 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 a connection from javaME with MySQL - SQL
How to make a connection from javaME with MySQL  Hi i`m a final year... have problem connecting my java with MysSQL. I`m using java netbeans IDE 6.5... taha411@gmail.com ym: mitb_86  Hi friend, Code to help in solving
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.
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 a Pdf and inserting data
How to Make a Pdf and inserting data       In this tutorial you will learn how we can make... the byte code into the String make a object of class String class and pass
Make Money
Make Money       Can I make money from my web site by writing articles? Yes, You can make money from your web site. We are providing free information
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.
JAVA code For
JAVA code For   JAVA code For "Traffic signals Identification for vehicles
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 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
code
code    how to write this in java
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
Ant make directory with relative path
Ant make directory with relative path       This example illustrates how to make directory, how to compile java file and how to create jar file. This is a simple program
Java code - Java Beginners
Java code  how to make a program in java(Jbutton) if there is a combination numbers inorder to exit the program else the background color of the frame will turns red
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 employee i card
how to make employee i card  hello friends can anyone help me.. I have to make a project to make Identity card for employees...but i am facing problems with database and jsp connection ... also tell me how to retrive data
how to make combinations - Development process
how to make combinations  import java.util.*; interface Expression... code : for(String token : rule.split(" ")) You not declare "rule" anywhere ; code : parseTree.add(new ActionExpression(act_num)); what the use
code
code   hi I need help in creating a java code that reminds user on a particular date about their festival. i have no clue of how to do it.. am looking forward to seek help from you
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 paging with class and ajax
how to make paging with class and ajax  paging with class and ajax... displayLinks($show){ $this->show = $show; // How many links to show...(str) { if (window.XMLHttpRequest) {// code
code
code  < comp xlink:type="new" xlink:actuate="onload"/> how to write this in java
java code - Java Beginners
java code  sir i want to connect all m pages as i make a start button in swing and i want whn we click on start my login page is come next how we link the pages..plzz send me the code
Code
Code  how to print this?? with the use of only JSP....... 1 23 456 78910   Here is the jsp number pattern example: 1 2 3 4 5 6 7 8 9 10 <%@page language="java"%> <% int k = 1; for(int i=1; i<= 4 ;i
To make java exe or runnable file
To make java exe or runnable file   Helo sir i make a java application.I need to help how run application direct in pc i.e how it packaged, .exe file or setup file
how to code this?
! Step 3: Make the Vending machine continue until the users tell it to stop
how to code this?
. Is this correct (y/n)? n Have a good day! Step 3: Make the Vending machine
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
Java Code - Swing AWT
Java Code  How to Make an application by using Swings JMenuBar and other components for drawing various categories of Charts(Line,Bar etc
make trial application
make trial application  Hello java master, i want to ask something about make java application trial in one month or two month. how to combine my application and give a trial application. what i must to do? Can you give me
how to make list in our own style in pdfwriter
how to make list in our own style in pdfwriter  can u please any of one help me i want to make a list as follows in pdf file from java by pdfwriter account calculation ------------------------- amount -100.00
java code - Java Beginners
java code  sir i want to connect all m pages as i make a start button in swing and i want whn we click on start my login page is come next how we... code: 1)StartDemo.java: import java.awt.*; import javax.swing.*; import
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 a Rectangle type pdf
How to make a Rectangle type pdf       In this program we are going to tell you how we can make a pdf file in the rectangle shape irrespective of the fact whether
Java Code - Java Beginners
Java Code  I need to modify the following code/GUI so that not only does it calculate the volume of a swimming pool, but it tells how much water... foot with a one time cost of $100.00. I just cant figure out how to make

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.