Question

Write a program that simulates a traffic light. When a radio button is selected, the corresponding light is turned on, a...

Write a program that simulates a traffic light. When a radio button is selected, the corresponding light is turned on, and only one light can be on at a time. No light is on when the program starts. Use Group instead of Pane to combine shapes, and then put the group to the center of a BorderPane will make all shapes staying in center when resizing the window.

using NetBeans Java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Code for the given question:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ItemListener;

import java.awt.event.ItemEvent;

import javax.swing.ButtonGroup;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

public class Name extends JFrame implements ItemListener{

   //Create buttons for traffic light

   private JRadioButton jrbRed;

   private JRadioButton jrbYellow;

   private JRadioButton jrbGreen;

   private ButtonGroup btg = new ButtonGroup();

   //create class light

   private Name.Light light = new Name.Light();

   public static void main(String[] args){

       //Create frame

       Name frame = new Name();

       frame.setDefaultCloseOperation(3);

       frame.setSize(250, 170);

       frame.setLocationRelativeTo(null);

       frame.setVisible(true);

   }

   public Name(){

       setTitle("Traffic Lights");

       JPanel p1 = new JPanel();

       p1.setLayout(new FlowLayout(1));

       p1.add(this.light);

       JPanel p2 = new JPanel();

       p2.setLayout(new FlowLayout());

       p2.add(this.jrbRed = new JRadioButton("Red"));

       p2.add(this.jrbYellow = new JRadioButton("Yellow"));

       p2.add(this.jrbGreen = new JRadioButton("Green"));

       this.jrbRed.setMnemonic('R');

       this.jrbYellow.setMnemonic('Y');

       this.jrbGreen.setMnemonic('G');

       this.btg.add(this.jrbRed);

       this.btg.add(this.jrbYellow);

       this.btg.add(this.jrbGreen);

       setLayout(new BorderLayout());

       add(p1, "Center");

       add(p2, "South");

       this.jrbRed.addItemListener(this);

       this.jrbYellow.addItemListener(this);

       this.jrbGreen.addItemListener(this);

   }

   public void itemStateChanged(ItemEvent e){

       if (this.jrbRed.isSelected()) {

           this.light.turnOnRed();

       }

       if (this.jrbYellow.isSelected()) {

           this.light.turnOnYellow();

       }

       if (this.jrbGreen.isSelected())

           this.light.turnOnGreen();

   }

   class Light extends JPanel{

       /**

       *

       */

       private static final long serialVersionUID = 1L;

       private boolean red;

       private boolean yellow;

       private boolean green;

       public void turnOnRed() {

           this.red = true;

           this.yellow = false;

           this.green = false;

           repaint();

       }

     

       public void turnOnYellow(){

           this.red = false;

           this.yellow = true;

           this.green = false;

           repaint();

     

       }

     

       public void turnOnGreen(){

           this.red = false;

           this.yellow = false;

           this.green = true;

           repaint();

       }

       protected void paintComponent(Graphics g){

           super.paintComponent(g);

           if (this.red) {

               g.setColor(Color.red);

               g.fillOval(10, 10, 20, 20);

               g.setColor(Color.black);

               g.drawOval(10, 35, 20, 20);

               g.drawOval(10, 60, 20, 20);

               g.drawRect(5, 5, 30, 80);

           }

         

           else if (this.yellow) {

               g.setColor(Color.yellow);

               g.fillOval(10, 35, 20, 20);

               g.setColor(Color.black);

               g.drawRect(5, 5, 30, 80);

               g.drawOval(10, 10, 20, 20);

               g.drawOval(10, 60, 20, 20);

           }

           else if (this.green) {

               g.setColor(Color.green);

               g.fillOval(10, 60, 20, 20);

               g.setColor(Color.black);

               g.drawRect(5, 5, 30, 80);

               g.drawOval(10, 10, 20, 20);

               g.drawOval(10, 35, 20, 20);

           }

           else {

               g.setColor(Color.black);

               g.drawRect(5, 5, 30, 80);

               g.drawOval(10, 10, 20, 20);

               g.drawOval(10, 35, 20, 20);

               g.drawOval(10, 60, 20, 20);

           }

       }

       public Dimension getPreferredSize(){

           return new Dimension(40, 90);

       }

   }

   public void actionPerformed(ActionEvent arg0) {

       // TODO Auto-generated method stub

   }

}

Outputs:

Traffic Li.. Red Yellow Green X OOO

Traffic Lig... Green Red OYellow X

Add a comment
Know the answer?
Add Answer to:
Write a program that simulates a traffic light. When a radio button is selected, the corresponding light is turned on, a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class...

    Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class header instance variable UML class diagram encapsulation client visibility (or access) modifier accessor method mutator method calling method method declaration method invocation return statement parameters constructor Goals By the end of this activity you should be able to do the following: > Create a class with methods that accept parameters and return a value Understand the constructor and the toString method of a class...

  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

  • One example of computer-aided design (CAD) is building geometric structures inter- actively. In Chapter 4, we...

    One example of computer-aided design (CAD) is building geometric structures inter- actively. In Chapter 4, we will look at ways in which we can model geometric objects comprised of polygons. Here, we want to examine the interactive part. Let’s start by writing an application that will let the user specify a series of axis- aligned rectangles interactively. Each rectangle can be defined by two mouse positions at diagonally opposite corners. Consider the event listener canvas.addEventListener("mousedown", function() { gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer); if...

  • Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an...

    Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an order at a fast-food burger joint. This class will be used in Part B, when we work with a list of orders. As vou work through this part and Part B, draw a UML diagram of each class in using the UML drawing tool 1) Create a new Lab5TestProject project in Netbeans, right-click on the lab5testproject package and select New>Java Class 2) Call your...

  • Hello! Could you please write your own four paragraph (5-6 sentences per paragraph) take away or...

    Hello! Could you please write your own four paragraph (5-6 sentences per paragraph) take away or reflection of the below information? Please complete in 24 hours if possible. Thank you! RIS BOHNET THINKS firms are wasting their money on diversity training. The problem is, most programs just don’t work. Rather than run more workshops or try to eradicate the biases that cause discrimination, she says, companies need to redesign their processes to prevent biased choices in the first place. Bohnet...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT