January 02, 2017

Traffic Light in java


Traffic Light program in java



Traffic light in java source code


package test;


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


public class TrafficLight extends JFrame implements ActionListener {
    JButton buttonRed, buttonYellow, buttonGreen;

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

    public TrafficLight(){
        super("Java Traffic Light Program");
        getContentPane().setLayout(new GridLayout(2, 1));
        buttonRed = new JButton("Red");
        buttonYellow = new JButton("Yellow");
        buttonGreen = new JButton("Green");
        buttonRed.addActionListener(this);
        buttonYellow.addActionListener(this);
        buttonGreen.addActionListener(this);      

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

        JPanel trafficPanel = new JPanel(new GridLayout(3,1));
        trafficPanel.add(red);
        trafficPanel.add(yellow);
        trafficPanel.add(green);
        JPanel lightPanel = new JPanel(new FlowLayout());
        lightPanel.add(buttonRed);
        lightPanel.add(buttonYellow);
        lightPanel.add(buttonGreen);

        getContentPane().add(trafficPanel);
        getContentPane().add(lightPanel);
        pack();
        }


    public static void main(String[] args){
        TrafficLight trafficLight = new TrafficLight();      
        trafficLight.setVisible(true);
    }  
    public void actionPerformed(ActionEvent e){      
        if (e.getSource() == buttonRed){
            green.turnOn(false);          
            yellow.turnOn(false);
            red.turnOn(true);
        } else if (e.getSource() == buttonYellow){
            yellow.turnOn(true);          
            green.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == buttonGreen){
            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 graphics){
        graphics.setColor( Color.black );
        graphics.fillRect(0,0,getWidth(),getHeight());

        if (change){
            graphics.setColor( on );
        } else {
            graphics.setColor( on.darker().darker().darker() );
        }
        graphics.fillOval( border,border,2*radius,2*radius );
    }
}

http://javabelazy.blogspot.in/

52 comments:

  1. So sites with excellent quality content become valuable to search engines.

    They do their level best to make a site attractive
    and beautiful. While it may take a little while for changes in design culture
    to filter out into the more remote regions of the UK, London because of its position as the capital
    city gets to see new concepts first, thanks to a plethora of exhibitions and shows dedicated to web design.

    ReplyDelete
  2. You will draw more loyal readers to your site and keep your readers coming back because you are sharing the best with them.
    There is absolutely no point in finding one of the great London Accountants only to find that they
    are actually too far away from your place of business to be accessible.
    This means that all of the content that your users can see,
    including buttons, menus and clickable links will be index-able.

    ReplyDelete
  3. So, additional efforts have to be undertaken to develop the visiting page of your website.
    The company can provide online businesses with the means to reach goals and achieve greatly.
    But if you are not, then you have to create a distinct identity for your business.

    ReplyDelete
  4. AnonymousMay 15, 2019

    You will draw more loyal readers to your site and keep
    your readers coming back because you are sharing the best with them.
    There is absolutely no point in finding one of the great London Accountants only to find that they are actually too far away from
    your place of business to be accessible. This means that all of
    the content that your users can see, including buttons, menus and clickable links will be index-able.

    ReplyDelete
  5. AnonymousMay 26, 2019

    Some SEO companies may only offer basic professional SEO services.

    By using the data from Google Adwords, SEO company London experts can match keywords that they want to target and are specific.
    But if you are not, then you have to create a distinct identity for your business.

    ReplyDelete
  6. AnonymousJune 04, 2019

    This gives a website web presence and it gives you a major advantage over your competition. Typically, it's good practice to maintain about a 5% usage for your keywords or phrases.
    You can get the contact details of the various seo
    companies by surfing around the globally web and create sure that
    you go through details of the training organization successfully before you be a part of your name
    for having a bright profession ahead.

    ReplyDelete
  7. AnonymousJune 24, 2019

    But unlike other SEO companies London or elsewhere, we forge
    long term partnerships for long-lasting success. They would try to make your site
    famous through social networking sites like my space, Facebook, Twitter, Digo,
    Digg, Delicious, etc as a part of their
    SEO services London. But if you are not, then you have to create
    a distinct identity for your business.

    ReplyDelete
  8. AnonymousJuly 31, 2019

    So, additional efforts have to be undertaken to
    develop the visiting page of your website. It provides
    flexible operating here we are at the employees and one can have pretty large wage along with the appropriate spare time.
    Whether you promote yourself as a solo or business brand (< learn 10 ways to improve your SEO with Google+) Google+ is now a necessity.

    ReplyDelete
  9. You will draw more loyal readers to your site and keep your readers coming back because you are sharing
    the best with them. There is absolutely no point in finding
    one of the great London Accountants only to find that they are actually too far away
    from your place of business to be accessible. Taking
    a look at other company's websites within a particular niche field can clue you
    in to what's being actively searched for, and what's not.

    ReplyDelete
  10. So sites with excellent quality content become valuable to search engines.
    There is absolutely no point in finding one of the great London Accountants only to find that they are actually too far away from your place of business to be
    accessible. But if you are not, then you have to
    create a distinct identity for your business.

    ReplyDelete
  11. Unquestionably imagine that which you said. Your favorite justification seemed to be on the net the simplest
    thing to understand of. I say to you, I definitely get annoyed even as
    other people think about concerns that they plainly do not realize about.
    You managed to hit the nail upon the highest as smartly as defined out
    the whole thing with no need side effect , people can take a signal.
    Will likely be again to get more. Thanks

    ReplyDelete
  12. I wanted to thank you for this good read!! I definitely enjoyed every little bit
    of it. I've got you book marked to check out new stuff you post…

    ReplyDelete
  13. Yes! Finally something about car park traffic lights.

    ReplyDelete
  14. A person essentially help to make severely posts I'd state.
    That is the very first time I frequented your web page and so far?
    I amazed with the research you made to create this particular submit amazing.
    Fantastic task!

    ReplyDelete
  15. Hi! I could have sworn I've visited this website before but after looking at some of the articles I realized
    it's new to me. Regardless, I'm certainly pleased
    I came across it and I'll be book-marking it and checking back frequently!

    ReplyDelete
  16. My family every time say that I am killing my time here at net, however
    I know I am getting know-how everyday by reading such pleasant posts.

    ReplyDelete
  17. Hi! I could have sworn I've been to your blog before but after going through a few of the posts I realized it's new to me.
    Anyways, I'm certainly delighted I stumbled upon it and I'll be bookmarking it and checking back regularly!

    ReplyDelete
  18. I always spent my half an hour to read this web
    site's content everyday along with a cup of coffee.

    ReplyDelete
  19. Hey there just wanted to give you a quick heads up. The
    words in your post seem to be running off the screen in Opera.
    I'm not sure if this is a formatting issue or something to
    do with browser compatibility but I thought I'd post to let you
    know. The design look great though! Hope you get the problem
    resolved soon. Many thanks

    ReplyDelete
  20. I do not even know how I ended up here, but I
    thought this post was good. I don't know who you are but definitely you're going to a famous
    blogger if you aren't already ;) Cheers!

    ReplyDelete
  21. I’m not that much of a internet reader to be honest but your blogs really nice, keep it up!
    I'll go ahead and bookmark your website to come back later on. All the best

    ReplyDelete
  22. Have you ever thought about writing an ebook
    or guest authoring on other sites? I have a blog centered on the same ideas
    you discuss and would love to have you share some stories/information.
    I know my subscribers would enjoy your work.
    If you're even remotely interested, feel free to shoot me
    an e-mail.

    ReplyDelete
  23. I do not even know the way I stopped up right here,
    but I believed this put up used to be great.

    I don't recognise who you're however definitely you are going to a well-known blogger if you aren't already.
    Cheers!

    ReplyDelete
  24. My partner and I absolutely love your blog and find most of
    your post's to be exactly what I'm looking for.
    Would you offer guest writers to write content to
    suit your needs? I wouldn't mind publishing a
    post or elaborating on many of the subjects you write concerning
    here. Again, awesome web site!

    ReplyDelete
  25. I'm not sure where you're getting your information, but good topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for excellent information I was looking for this info
    for my mission.

    ReplyDelete
  26. My brother suggested I might like this blog.

    He was once totally right. This publish actually made my day.
    You can not imagine just how much time I had spent for this information! Thanks!

    ReplyDelete
  27. Link exchange is nothing else however it is only placing
    the other person's webpage link on your page at suitable place and other
    person will also do same in favor of you.

    ReplyDelete
  28. I've been exploring for a little bit for any
    high quality articles or blog posts in this kind
    of house . Exploring in Yahoo I ultimately stumbled upon this web site.
    Studying this info So i am satisfied to exhibit that I have an incredibly
    just right uncanny feeling I discovered exactly what I needed.

    I so much for sure will make sure to do
    not fail to remember this site and provides it a
    look regularly.

    ReplyDelete
  29. Howdy would you mind sharing which blog platform you're working with?
    I'm looking to start my own blog soon but I'm having a
    hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I'm looking for something completely unique.
    P.S Sorry for being off-topic but I had to ask!

    ReplyDelete
  30. obviously like your web site but you need to test the spelling
    on quite a few of your posts. Several of them are rife with spelling problems and I in finding it very troublesome to tell the truth on the
    other hand I will definitely come again again.

    ReplyDelete
  31. My partner and I stumbled over here coming from a different
    web address and thought I might as well check things out.

    I like what I see so i am just following you.

    Look forward to exploring your web page repeatedly.

    ReplyDelete
  32. What's Happening i'm new to this, I stumbled upon this I've found
    It positively helpful and it has aided me out loads.
    I hope to give a contribution & aid other users like its aided me.
    Great job.

    ReplyDelete
  33. I do accept as true with all of the ideas you've offered in your
    post. They are very convincing and will certainly work.
    Still, the posts are very brief for beginners. Could you please lengthen them a bit from subsequent time?
    Thank you for the post.

    ReplyDelete
  34. Hi everybody, here every person is sharing these kinds of
    knowledge, thus it's nice to read this webpage, and I used to pay a quick visit this website daily.

    ReplyDelete
  35. Ahaa, its good discussion about this post at
    this place at this website, I have read all that, so now me also commenting at this place.

    ReplyDelete
  36. Hi, constantly i used to check weblog posts here in the
    early hours in the daylight, because i love to find out more and more.

    ReplyDelete
  37. My brother recommended I might like this web site. He was
    totally right. This put up actually made my day. You cann't consider
    simply how much time I had spent for this information! Thanks!

    ReplyDelete
  38. you're in point of fact a good webmaster. The website loading velocity is incredible.
    It seems that you are doing any distinctive trick. Also, The contents are masterpiece.
    you have done a great process in this topic!

    ReplyDelete
  39. I like reading a post that will make men and women think.
    Also, thank you for allowing me to comment!

    ReplyDelete
  40. Wow! This blog looks exactly like my old one! It's on a totally different subject
    but it has pretty much the same layout and design. Outstanding choice of colors!

    ReplyDelete
  41. Hello! I could have sworn I've visited this site before but after looking at a few of the articles I realized it's
    new to me. Anyways, I'm certainly pleased I discovered it and I'll be bookmarking
    it and checking back often!

    ReplyDelete
  42. Hey there! This is my first visit to your blog! We are a group
    of volunteers and starting a new initiative
    in a community in the same niche. Your blog provided us beneficial information to work on. You have done
    a marvellous job!

    ReplyDelete
  43. I like the valuable info you provide in your articles.
    I will bookmark your weblog and check again here frequently.

    I'm quite certain I will learn plenty of new stuff right here!
    Good luck for the next!

    ReplyDelete
  44. hi!,I like your writing so so much! proportion we keep in touch more about your
    article on AOL? I need a specialist on this space to resolve my problem.
    May be that is you! Having a look forward to look you.

    ReplyDelete
  45. I am regular reader, how are you everybody?
    This article posted at this web site is actually nice.

    ReplyDelete
  46. You've made some good points there. I checked on the net for more info about the issue and found most people will go along with your views on this
    web site.

    ReplyDelete
  47. Informative article, totally what I needed.

    ReplyDelete
  48. I seriously love your blog.. Pleasant colors & theme. Did you make this website
    yourself? Please reply back as I'm planning to create my own personal website
    and want to find out where you got this from or just what the
    theme is named. Appreciate it!

    ReplyDelete
  49. At this time I am going to do my breakfast, afterward having my
    breakfast coming yet again to read additional news.

    ReplyDelete
  50. Tremendous issues here. I'm very happy to see your
    article. Thanks so much and I am having a look forward to contact you.
    Will you please drop me a mail?

    ReplyDelete
  51. Nice weblog right here! Additionally your website loads up very fast!
    What web host are you the use of? Can I am getting your
    associate hyperlink to your host? I desire my web site loaded up as fast as yours
    lol

    ReplyDelete
  52. Hello are using Wordpress for your blog platform?
    I'm new to the blog world but I'm trying to get started and
    set up my own. Do you need any coding expertise to make your own blog?

    Any help would be really appreciated!

    ReplyDelete

Your feedback may help others !!!

Facebook comments