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
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
-----------------------------------------------------------------------------------------------------------------------
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); } }
import java.awt.Color;
import java.awt.Graphics2D;
public class TrafficLight {
private static final int RED = 0;
private static final int GREEN = 1;
private static final int YELLOW = 2;
private static final int WIDTH = 20;
private static final int HEIGHT = 60;
// Instance varibles
private int x;
private int y;
private int light;
/**
* Constructor
*
* @param x
*
- x coordinate of the traffic light
* @param y
*
- y coordinate of the traffic light
*/
public TrafficLight(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Returns the current state of the light as "red",
"green" or "yellow"
*/
public String getLight() {
if (this.light == RED)
return
"red";
else if (this.light == GREEN)
return
"green";
else if (this.light ==
YELLOW)
return
"yellow";
else
return "";
}
/**
* Changes the current state of the traffic light to
the next state if
* current is RED, next will be GREEN if current is
GREEN, next will be
* YELLOW if current is YELLOW, next will be RED
*/
public void cycle() {
if (this.light == RED)
this.light =
GREEN;
else if (this.light == GREEN)
this.light =
YELLOW;
else if (this.light ==
YELLOW)
this.light =
RED;
}
/**
* Draws a traffic light at x, y of Color c
*
* @param x
*
- x coordinate of the light
* @param y
*
- y coordinate of the light
* @param w
*
- width of the light
* @param h
*
- height of the light
* @param c
*
- color of the light
*/
private void drawLight(int x, int y, int w, int h,
Color c, Graphics2D g2) {
g2.setColor(c);
g2.fillOval(x, y, w, h);
}
/**
* Draws the traffic light
*/
public void draw(Graphics2D g2) {
// Draw the traffic light box
g2.drawRect(x, y, WIDTH,
HEIGHT);
// Draw the lights
if (this.light == RED) {
// Set red
color
drawLight(x, y,
WIDTH, WIDTH, Color.RED, g2);
// Set yellow
color for off
drawLight(x, y +
WIDTH, WIDTH, WIDTH, new Color(255, 165, 0), g2);
// Set green
color for off
drawLight(x, y +
WIDTH + WIDTH, WIDTH, WIDTH, new Color(85, 107, 47), g2);
} else if (this.light == GREEN)
{
// Set red
color
drawLight(x, y,
WIDTH, WIDTH, new Color(128, 0, 0), g2);
// Set yellow
color for off
drawLight(x, y +
WIDTH, WIDTH, WIDTH, new Color(255, 165, 0), g2);
// Set green
color for off
drawLight(x, y +
WIDTH + WIDTH, WIDTH, WIDTH, Color.GREEN, g2);
} else if (this.light == YELLOW)
{
// Set red
color
drawLight(x, y,
WIDTH, WIDTH, new Color(128, 0, 0), g2);
// Set yellow
color for off
drawLight(x, y +
WIDTH, WIDTH, WIDTH, Color.YELLOW, g2);
// Set green
color for off
drawLight(x, y +
WIDTH + WIDTH, WIDTH, WIDTH, new Color(85, 107, 47), g2);
}
// Reset color
g2.setColor(Color.BLACK);
}
}
SAMPLE OUTPUT:
A Java Problem. In this problem you are going write a class to model a traffic...
Assignment Λ You shall write a Java program that accepts 5 command-line arguments and generates an image of a Sierpinski triangle, as a 24- bit RGB PNG image file. Specifications The command-line arguments shall consist of the following 1. The width (in pixels) of the image, as a positive decimal integer 2. The height (in pixels) of the image, as a positive decimal integer 3. The minimum area (in pixels) that a triangle must have in order to be drawn,...
The program given here will draw the Olympic logo. Modify the program as the following: 1.Change the class name to Test1OlympicYourFirstNameYourLastName 2.Change the OlympicRingPanel class name to OlympicRingPanelYourfirstNameYourLastName 3.Modify the program so that the first ring will be filled with random color with a random alpha value. I have shown 3 sample output here for your references. 4.Modify the paintComponent so your name will be displayed under the Olympic rings.The color of your name should be random color as well....
GUI in java: Run the below code before doing the actionlistener: import java.awt.*; import javax.swing.*; public class DrawingFrame extends JFrame { JButton loadButton, saveButton, drawButton; JComboBox colorList, shapesList; JTextField parametersTextField; DrawingFrame() { super("Drawing Application"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JToolBar toolbar = new JToolBar(); toolbar.setRollover(true); toolbar.add(loadButton=new JButton("Load")); toolbar.add(saveButton=new JButton("Save")); toolbar.addSeparator(); toolbar.add(drawButton=new JButton("Draw")); toolbar.addSeparator(); toolbar.addSeparator(); toolbar.add(new...
import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...
import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...
Hi! I'm working on building a GUI that makes cars race across the screen. So far my code produces three cars, and they each show up without error, but do not move across the screen. Can someone point me in the right direction? Thank you! CarComponent.java package p5; import java.awt.*; import java.util.*; import javax.swing.*; @SuppressWarnings("serial") public class CarComponent extends JComponent { private ArrayList<Car> cars; public CarComponent() { cars = new ArrayList<Car>(); } public void add(Car car) { cars.add(car); } public...
JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...
2. Consider the following problem associated with synchronizing traffic lights. A particular traffic light (Light A) has a cycle as follows: Red = 1 min Green = 1.5 min Yellow = 0.5 min Some distance before Light A is another light (Light B). Due to varying drivers and conditions, the travel time between the two lights varies from vehicle to vehicle. Data suggest that 40% of all cars leaving Light B at a time when Light A is red are...
Designing a Traffic Light Controller [50 pts] [Your originality, thinking method and execution would be graded] In Gotham city, traffic lights have three outputs as Red (R), Yellow (Y) or Green (G). Let's assume you're given a microchip with clock input 0.2 Hz. As you know.cars can travel when the light is Green and should not pass the traffic light when the light is Yellow or Red. We are asked to design the traffic light controller in the busiest road...
(JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...