Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Applets and More Lab Objectives Be able to write an applet Be able to draw recta

ID: 3571180 • Letter: A

Question

Applets and More
Lab Objectives
Be able to write an applet
Be able to draw rectangles, circles, and arcs
Be able to override the paint method
Be able to use a timer
Introduction
In this lab we will create an applet that changes the light on a traffic signal. The applet that you
create will draw the outside rectangle of a traffic signal and fill it in with yellow. Then it will
draw three circles in one column, to resemble the red, orange, and green lights on the traffic
signal. Only one circle at a time will be filled in. It will start with green and cycle through the
orange, red, and back to green to start the cycle again. Each light will remain on for the same
amount of time. To accomplish this cycle, we will use a timer object.
When you finished your applet should appear as shown in figure 1, but with the filled in circle
cycling up from green to orange to red and starting over in a continuous changing of the traffic
light.


Task #1 Create an Applet
1. Copy the file TrafficApplet.java into NetBeans or other Java IDE tools.
2. This class currently has all the constants you will need. Notice that the class extends
JApplet. An applet does not have a constructor or a main method. Instead, it has a method
named init that performs the same operations as a constructor. The init method accepts
no arguments and has a void return type. Inside the init method, we create a timer object
passing in the TIME_DELAY constant and a new TimerListener (We will be creating the
listener class next). You need to add code to call the start method with the timer object to
generate action events.


Task #2 The TimerListener Class
We need to write a private inner class called TimerListener which implements ActionListener.
The outline is given.
1. Inside this class, finish writing the actionPerformed method. This method will check the
status variable to see whether it is currently red, orange, or green. Since we want the
lights to cycle as a traffic signal, we need to cycle in the order: green, orange, red, green,
orange, red, Once the status is determined, the status should then be set to the next
color in the cycle. Notice that at the end of the method we need to redisplay the graphics
components by calling the repaint method.


Task #3 Drawing Graphics
We need to draw the traffic signal by overriding the paint method. For all graphics, use the
named constants included in the class. Notice that we need to call the super.paint(g) first. This is
needed for graphics paint method.
1. The yellow rectangle (solid color) and red traffic signals drawing has been provided as
coding example. Create round lights of orange, and green for the signals. Only one light
will be filled in at a time, when the status indicates that one has been chosen. You will
need to check the status to determine which light to fill in. Remember, the status is
changed only in the actionPerformed method.
2. Put the shade hoods above the lights by drawing black arcs above each light.
3. Try out your applet. You can use Run file from NetBeans, or double click the
applet.html to run the applet from InternetExplorer or other web client such as Firefox.
Your TrafficApplet.class should be in the same directory with the applet.html. You may
need to allow blocked content temporally if asked.

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

public class TrafficApplet extends JApplet
{
   public final int WIDTH = 300;
   public final int HEIGHT = 400;

   public final int X_TRAFFICLIGHT = WIDTH/3;
   public final int Y_TRAFFICLIGHT = HEIGHT/7;
   public final int TRAFFICLIGHT_WIDTH = WIDTH/2;
   public final int TRAFFICLIGHT_HEIGHT = HEIGHT*3/5;
   public final int LIGHT_DIAMETER = TRAFFICLIGHT_HEIGHT/5;
   public final int HOOD_START_ANGLE = 20;
public final int HOOD_ANGLE_SWEPT = 140;
public final int X_LIGHTS = TRAFFICLIGHT_WIDTH/3 + X_TRAFFICLIGHT;
   public final int Y_REDLIGHT = TRAFFICLIGHT_HEIGHT/10 + Y_TRAFFICLIGHT;
   public final int Y_ORANGELIGHT = TRAFFICLIGHT_HEIGHT*4/10 + Y_TRAFFICLIGHT;
   public final int Y_GREENLIGHT = TRAFFICLIGHT_HEIGHT*7/10 + Y_TRAFFICLIGHT;
   public final int TIME_DELAY = 1000;
  
   private String status = "green";   //start with the green light
   private Timer timer;   //will allow lights to cycle
  

   public void init()
   {
       //create a timer object and register an Action Listener
       timer = new Timer (TIME_DELAY, new TimerListener());
       //To do - Task #1 step 2: call the start method of the timer
                  
   }

   private class TimerListener implements ActionListener
   {
       public void actionPerformed(ActionEvent action)
       {
           //changes a red light to a green light
           if (status.equals("red"))
           {
               status = "green";
           }
          
           //To do - Task #2 step 1:
           //changes a green light to an orange light
           // use "else if" to avoid double change
          
           //changes an orange light to a red light
  
          
           //redraw the applet
           repaint();
       }
   }

   public void paint (Graphics g)
   {
       super.paint(g);
              
       //draw the yellow traffic signal rectangle
       g.setColor(Color.yellow);
       g.fillRect(X_TRAFFICLIGHT, Y_TRAFFICLIGHT, TRAFFICLIGHT_WIDTH, TRAFFICLIGHT_HEIGHT);

       //draw the red light
g.setColor(Color.red);
g.drawOval(X_LIGHTS, Y_REDLIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER);
if(status.equals("red"))
       {
          g.fillOval(X_LIGHTS, Y_REDLIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER);
       }      
       //draw the hood for the red light
       g.setColor(Color.black);
       g.drawArc(X_LIGHTS, Y_REDLIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER, HOOD_START_ANGLE, HOOD_ANGLE_SWEPT);
  
       //To do - Task #3 step 1 and step 2:
       //draw the green light if(status.equals("green"))
  
       //draw the hood for the green light

       //draw the orange light if(status.equals("orange"))

       //draw the hood for the orange light  

   }
}

Explanation / Answer

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

public class TrafficApplet extends JApplet {
   public final int WIDTH = 300;
   public final int HEIGHT = 400;
   public final int X_TRAFFICLIGHT = WIDTH / 3;
   public final int Y_TRAFFICLIGHT = HEIGHT / 7;
   public final int TRAFFICLIGHT_WIDTH = WIDTH / 2;
   public final int TRAFFICLIGHT_HEIGHT = HEIGHT * 3 / 5;
   public final int LIGHT_DIAMETER = TRAFFICLIGHT_HEIGHT / 5;
   public final int HOOD_START_ANGLE = 20;
   public final int HOOD_ANGLE_SWEPT = 140;
   public final int X_LIGHTS = TRAFFICLIGHT_WIDTH / 3 + X_TRAFFICLIGHT;
   public final int Y_REDLIGHT = TRAFFICLIGHT_HEIGHT / 10 + Y_TRAFFICLIGHT;
   public final int Y_ORANGELIGHT = TRAFFICLIGHT_HEIGHT * 4 / 10
           + Y_TRAFFICLIGHT;
   public final int Y_GREENLIGHT = TRAFFICLIGHT_HEIGHT * 7 / 10
           + Y_TRAFFICLIGHT;
   public final int TIME_DELAY = 1000;

   private String status = "green"; // start with the green light
   private Timer timer; // will allow lights to cycle

   public void init() {
       // create a timer object and register an Action Listener
       timer = new Timer(TIME_DELAY, new TimerListener());
       timer.start();
       // To do - Task #1 step 2: call the start method of the timer

   }

   private class TimerListener implements ActionListener {
       public void actionPerformed(ActionEvent action) {
           // changes a red light to a green light
           if (status.equals("red")) {
               status = "green";
           }else if (status.equals("green")) {
               status = "orange";
           }else if (status.equals("orange")) {
               status = "red";
           }

           // To do - Task #2 step 1:
           // changes a green light to an orange light
           // use "else if" to avoid double change

           // changes an orange light to a red light

           // redraw the applet
           repaint();
       }
   }

   public void paint(Graphics g) {
       super.paint(g);

       // draw the yellow traffic signal rectangle
       g.setColor(Color.yellow);
       g.fillRect(X_TRAFFICLIGHT, Y_TRAFFICLIGHT, TRAFFICLIGHT_WIDTH,
               TRAFFICLIGHT_HEIGHT);
       // draw the red light
       g.setColor(Color.red);
       g.drawOval(X_LIGHTS, Y_REDLIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER);
       if (status.equals("red")) {
           g.fillOval(X_LIGHTS, Y_REDLIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER);
       }
       // draw the hood for the red light
       g.setColor(Color.black);
       g.drawArc(X_LIGHTS, Y_REDLIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER,
               HOOD_START_ANGLE, HOOD_ANGLE_SWEPT);

       // To do - Task #3 step 1 and step 2:
      
       // draw the green light
           g.setColor(Color.green);
           g.drawOval(X_LIGHTS, Y_GREENLIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER);
           // draw the green light if(status.equals("green"))
           if (status.equals("green")) {
               g.fillOval(X_LIGHTS, Y_GREENLIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER);
           }
           // draw the hood for the green light
           g.setColor(Color.black);
           g.drawArc(X_LIGHTS, Y_GREENLIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER,
                   HOOD_START_ANGLE, HOOD_ANGLE_SWEPT);
          
      
           // draw the orange light
           g.setColor(Color.orange);
           g.drawOval(X_LIGHTS, Y_ORANGELIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER);
           // draw the orange light if(status.equals("orange"))
           if (status.equals("orange")) {
               g.fillOval(X_LIGHTS, Y_ORANGELIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER);
           }
           // draw the hood for the orange light
           g.setColor(Color.black);
           g.drawArc(X_LIGHTS, Y_ORANGELIGHT, LIGHT_DIAMETER, LIGHT_DIAMETER,
                   HOOD_START_ANGLE, HOOD_ANGLE_SWEPT);
      

      

      
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote