Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ / Java Program to Implement Traffic signal / / Using Java Swing Components / / Importing required classesimport java.awt. * ;import java.awt.event. * ;import

// Java Program to Implement Traffic signal// Using Java Swing Components// Importing required classesimport java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;// Main class// Extending JFrame class and// Implementing ItemListener interfacepublic class Traffic_Signalextends JFrame implements ItemListener {// Setting the buttons for the layoutJRadioButton jr1;JRadioButton jr2;JRadioButton jr3;// Setting the field areaJTextField j1= new JTextField(10);ButtonGroup b = new ButtonGroup();String msg ="";// Initially setting the co-ordinates to 0,0,0int x =0, y =0, z =0;public Traffic_Signal(String msg){super(msg);setLayout(new FlowLayout());// Assigning name to the button declared above// with help of JRadioButton classjr1= new JRadioButton("Red");jr2= new JRadioButton("Yellow");jr3= new JRadioButton("Green");jr1.addItemListener(this);jr2.addItemListener(this);jr3.addItemListener(this);add(jr1);add(jr2);add(jr3);b.add(jr1);b.add(jr2);b.add(jr3);add(j1);// Method 1// To add a windowaddWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){// It haults here itselfSystem.exit(0);}});}// Method 2// To change colors of traffic signalpublic void itemStateChanged(ItemEvent ie){// If it is redif (ie.getSource()== jr1){if (ie.getStateChange()==1){// Then display message- Stopmsg = "Stop!";x =1;// Repainting the box with original one// Practically blackrepaint();}else {msg ="";}}// If state is Orange or technically jr2if (ie.getSource()== jr2){if (ie.getStateChange()==1){// Then display message-// Get ready in waiting statemsg = "Get Ready to go!";y =1;// Again repainting the buttonrepaint();}else {msg ="";}}// If state is Greenif (ie.getSource()== jr3){if (ie.getStateChange()==1){// Then display message- Gomsg ="Go!!";z =1;repaint();}else {msg ="";}}j1.setText(msg);}// Method 3// handling the paint graphics and// dimensions of the buttons via// setting co-ordinatespublic void paint(Graphics g){g.drawRect(100,105,110,270);g.drawOval(120,150,60,60);g.drawOval(120,230,60,60);g.drawOval(120,300,60,60);// Case: Redif (x ==1){g.setColor(Color.RED);g.fillOval(120,150,60,60);g.setColor(Color.WHITE);g.fillOval(120,230,60,60);g.setColor(Color.WHITE);g.fillOval(120,300,60,60);x =0;}// Case: Orangeif (y ==1){g.setColor(Color.WHITE);g.fillOval(120,150,60,60);g.setColor(Color.YELLOW);g.fillOval(120,230,60,60);g.setColor(Color.WHITE);g.fillOval(120,300,60,60);y =0;}// Case: Greenif (z ==1){g.setColor(Color.WHITE);g.fillOval(120,150,60,60);g.setColor(Color.WHITE);g.fillOval(120,230,60,60);g.setColor(Color.GREEN);g.fillOval(120,300,60,60);z =0;}}// Method 4// Main driver methodpublic static void main(String args[]){// Creating object of Jframe class inside main()// methodJFrame jf = new Traffic_Signal("Traffic Light");// Setting size and making traffic signal// operational using setVisible() methodjf.setSize(500,500);jf.setVisible(true);}}
Project 1: Add 2 more lights to the traffic signal. Explain what you did and how you did it.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2017 Skopje Macedonia September 18 22 2017 Proceedings Part 3 Lnai 10536

Authors: Yasemin Altun ,Kamalika Das ,Taneli Mielikainen ,Donato Malerba ,Jerzy Stefanowski ,Jesse Read ,Marinka Zitnik ,Michelangelo Ceci ,Saso Dzeroski

1st Edition

3319712721, 978-3319712727

More Books

Students also viewed these Databases questions

Question

Can workers be trained in ethics? How? Defend your answer.

Answered: 1 week ago