Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A Java Problem. In this problem you are going write a class to model a traffic light that can cycle through colors, red, green, yellow.

A Java Problem. In this problem you are going write a class to model a traffic light that can cycle through colors, red, green, yellow. To do this, you will have to maintain the state of the traffic light. Maintaining state is one of the design pattern discussed in this week's lesson. The state of the TrafficLight indicates which light is lit. It changes every time the TrafficLight cycles.

Specification

TrafficLight will have a constructor that takes the x and y coordinates of the upper left hand corner of the rectangular part of the traffic light as parameters. The TrafficLight is black rectangle at the x,y coordinates with a width of 20 and a height of 60. There are red, green, and yellow circles inside the rectangle to look like a traffic light. The upper left hand corner of the red circle will be at same x,y coordinates as the box. The circles will touch the sides of the box and each other

Define an instance variable to hold the state (which light is lit). Make it an int. And define public constants for red, green, and yellow, named RED, GREEN, YELLOW (These will be ints also)

The constructor takes the x,y coordinates of the upper left hand corner of the TrafficLight as parameters and also sets the state of the light to red (using the constant).

Provide methods

1. public void cycle() changes the state (the color) in the following manner

image text in transcribed

2. public String getLight()gets the color (the current state) of the light as a string "red", "green" or "yellow"

3. public void draw(Graphics2D g2) draws the traffic light and fills the circles with a different color when the light is on or off

image text in transcribed

image text in transcribed

-----------------------------------------------------------------------------------------------------------------------

TrafficLightComponent.java

import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; public class TrafficLightComponent extends JComponent { private static final long serialVersionUID = 1L; public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g; TrafficLight signal = new TrafficLight( 10, 20); signal.draw(g2); String colorWord = (signal.getLight()); g2.drawString(colorWord, 10, 100); TrafficLight signal2 = new TrafficLight( 60, 20); signal2.cycle(); signal2.draw(g2); colorWord = signal2.getLight(); g2.drawString(colorWord, 60, 100); TrafficLight signal3 = new TrafficLight( 110, 20); signal3.cycle(); signal3.cycle(); signal3.draw(g2); colorWord = signal3.getLight(); g2.drawString(colorWord, 110, 100); TrafficLight signal4 = new TrafficLight( 160, 20); signal4.cycle(); signal4.cycle(); signal4.cycle(); signal4.draw(g2); colorWord = signal4.getLight(); g2.drawString(colorWord, 160, 100); } } 

TrafficLightViewer.java

import javax.swing.*; public class TrafficLightViewer { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 400); frame.setTitle("Traffic Lights"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TrafficLightComponent component = new TrafficLightComponent(); frame.add(component); frame.setVisible(true); } } 
If the state was red green yellow now it is green yellow red

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

Step: 3

blur-text-image

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

DB2 9 For Linux UNIX And Windows Advanced Database Administration Certification Certification Study Guide

Authors: Roger E. Sanders, Dwaine R Snow

1st Edition

1583470808, 978-1583470800

More Books

Students also viewed these Databases questions

Question

Evaluate the importance of the employee handbook.

Answered: 1 week ago

Question

1.who the father of Ayurveda? 2. Who the father of taxonomy?

Answered: 1 week ago

Question

Commen Name with scientific name Tiger - Wolf- Lion- Cat- Dog-

Answered: 1 week ago

Question

Understand how to design effective service guarantees.

Answered: 1 week ago