Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Traffic Light -- Very Simple

Posted on 2004-10-05
11
Medium Priority
?
523 Views
Last Modified: 2012-05-05
Hi, not to sure how I can get this code working. The teacher wants us to use the Circle class that was in the book even though we don't need all of it's functionality but I'm not sure how to get it to paint the circles from within the switch statment in the TrafficLightPanel class. Just need some guidance on this one. Thanks

//****************************************************************************************
// TrafficLight.java                   Author: Romeo Theriault
//
// Not sure just yet.
//****************************************************************************************

import javax.swing.JFrame;

public class TrafficLight
{
      //-----------------------------------------------------------------------------
      // Creates the main program frame.
      //-----------------------------------------------------------------------------
      public static void main (String[] args)
      {
            JFrame frame = new JFrame ("Traffic Light");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            
            frame.getContentPane().add(new TrafficLightPanel());
            
            frame.pack();
            frame.setVisible(true);
      }
}






//********************************************************************
//  TrafficLightPanel.java       Authors: Romeo Theriault
//
//  Demonstrates a graphical user interface and an event listener.
//
//  Do i need to make the Random generator private or public?      
//********************************************************************

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

public class TrafficLightPanel extends JPanel
{
 
  private int state = 1;
  private JButton push;
  private Circle redLight, yellowLight, greenLight;
  //----------------------------------------------------------------------------------
  //  Constructor: Sets up the main characteristics of the traffic light.
  //----------------------------------------------------------------------------------
  public TrafficLightPanel ()
  {
             
                   
      //----------------------------Image stuff---------------------------------------------------                  
            
            redLight = new Circle (50, Color.red, 35, 50);
            greenLight = new Circle (50, Color.green, 35, 125);
            yellowLight = new Circle (50, Color.yellow, 35, 200);
            setBackground (Color.black);                   // set background color
            setPreferredSize (new Dimension(120, 250));   // set the size of the whole window
      //----------------------------------------------------------------------------------------------
      
      //----------------------------Button stuff----------------------------------------------------
            
            
            push = new JButton ("Change!");  // Makes a new button called "push"
            push.addActionListener (new ButtonListener());   // Makes a new button listener attached to button "push"
            
            add (push);     // adds the button to the panel
      //----------------------------------------------------------------------------------------------
 
  }
      
      //---------------------------------------------------------------------------------
      //  Draw a traffic light
      //---------------------------------------------------------------------------------
            public void paintComponent (Graphics page)
            {
                  super.paintComponent (page);
                  
            
                  redLight.draw(page);      
            }

      //-------------------------------------------------------------------------------------------
      // Represents a listener for button "push" events.
      //---------------------------------------------------------------------------------------------
      private class ButtonListener implements ActionListener
      {
         public void actionPerformed (ActionEvent event)
            {
                  
                  
            //      switch (state)
                   //{
                  //      default:
    //                        redLight.draw(page);
                   //            state = state + 1;
                         //      break;
            //            case 2:
             //                  greenLight.draw(page);
             //                  state = state + 1;
             //                  break;
            //            case 3:
              //                  yellowLight.draw(page);
              //                  state = 1;                              
            //                  break;            
            //            }      
            //      
            //      repaint();
            }
      
      }
}








//********************************************************************
//  Circle.java       Author: Lewis/Loftus
//
//  Represents a circle with a particular position, size, and color.
//********************************************************************

import java.awt.*;

public class Circle
{
   private int diameter, x, y;
   private Color color;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this circle with the specified values.
   //-----------------------------------------------------------------
   public Circle (int size, Color shade, int upperX, int upperY)
   {
      diameter = size;
      color = shade;
      x = upperX;
      y = upperY;
   }

   //-----------------------------------------------------------------
   //  Draws this circle in the specified graphics context.
   //-----------------------------------------------------------------
   public void draw (Graphics page)
   {
      page.setColor (color);
      page.fillOval (x, y, diameter, diameter);
   }

   //-----------------------------------------------------------------
   //  Diameter mutator.
   //-----------------------------------------------------------------
   public void setDiameter (int size)
   {
      diameter = size;
   }

   //-----------------------------------------------------------------
   //  Color mutator.
   //-----------------------------------------------------------------
   public void setColor (Color shade)
   {
      color = shade;
   }

   //-----------------------------------------------------------------
   //  X mutator.
   //-----------------------------------------------------------------
   public void setX (int upperX)
   {
      x = upperX;
   }


   //-----------------------------------------------------------------
   //  Y mutator.
   //-----------------------------------------------------------------
   public void setY (int upperY)
   {
      y = upperY;
   }

   //-----------------------------------------------------------------
   //  Diameter accessor.
   //-----------------------------------------------------------------
   public int getDiameter ()
   {
      return diameter;
   }

   //-----------------------------------------------------------------
   //  Color accessor.
   //-----------------------------------------------------------------
   public Color getColor ()
   {
      return color;
   }

   //-----------------------------------------------------------------
   //  X accessor.
   //-----------------------------------------------------------------
   public int getX ()
   {
      return x;
   }

   //-----------------------------------------------------------------
   //  Y accessor.
   //-----------------------------------------------------------------
   public int getY ()
   {
      return y;
   }
}



0
Comment
Question by:romeotheriault
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12233955
Add a new member valiable referencing the current light, and paint that light in paintComponent() (instead of the red one).

Then in your switch change which laight is current, and call repaint().
0
 

Author Comment

by:romeotheriault
ID: 12234019
What exactly do you mean member variable, example?
0
 
LVL 6

Accepted Solution

by:
expertmb earned 500 total points
ID: 12234033
//****************************************************************************************
// TrafficLight.java                   Author: Romeo Theriault
//
// Not sure just yet.
//****************************************************************************************

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

public class TrafficLight
{
     //-----------------------------------------------------------------------------
     // Creates the main program frame.
     //-----------------------------------------------------------------------------
     public static void main (String[] args)
     {
          JFrame frame = new JFrame ("Traffic Light");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

          frame.getContentPane().add(new TrafficLightPanel());

          frame.pack();
          frame.setVisible(true);
     }
}






//********************************************************************
//  TrafficLightPanel.java       Authors: Romeo Theriault
//
//  Demonstrates a graphical user interface and an event listener.
//
//  Do i need to make the Random generator private or public?
//********************************************************************


class TrafficLightPanel extends JPanel
{

  private int state = 1;
  private JButton push;
  private Circle redLight, yellowLight, greenLight, light;

  Graphics page = null;
  //----------------------------------------------------------------------------------
  //  Constructor: Sets up the main characteristics of the traffic light.
  //----------------------------------------------------------------------------------
  public TrafficLightPanel ()
  {


     //----------------------------Image stuff---------------------------------------------------

          redLight = new Circle (50, Color.red, 35, 50);
          greenLight = new Circle (50, Color.green, 35, 125);
          yellowLight = new Circle (50, Color.yellow, 35, 200);
          setBackground (Color.black);                   // set background color
          setPreferredSize (new Dimension(120, 250));   // set the size of the whole window
          light = new Circle (50, Color.red, 35, 50);



     //----------------------------------------------------------------------------------------------

     //----------------------------Button stuff----------------------------------------------------


          push = new JButton ("Change!");  // Makes a new button called "push"
          push.addActionListener (new ButtonListener());   // Makes a new button listener attached to button "push"

          add (push);     // adds the button to the panel
     //----------------------------------------------------------------------------------------------

  }

     //---------------------------------------------------------------------------------
     //  Draw a traffic light
     //---------------------------------------------------------------------------------
          public void paintComponent (Graphics g)
          {
               super.paintComponent (g);

                        page = g;
               light.draw(g);
          }

     //-------------------------------------------------------------------------------------------
     // Represents a listener for button "push" events.
     //---------------------------------------------------------------------------------------------
     private class ButtonListener implements ActionListener
     {
        public void actionPerformed (ActionEvent event)
          {


               switch (state)
                {
                    default:
                        redLight.draw(page);
                          state = state + 1;
                          light = new Circle (50, Color.red, 35, 50);
                          break;
                    case 2:
                          greenLight.draw(page);
                          state = state + 1;
                          light = greenLight;
                          break;
                    case 3:
                           yellowLight.draw(page);
                           state = 1;
                           light = yellowLight;
                         break;
                    }

               repaint();
          }

     }
}








//********************************************************************
//  Circle.java       Author: Lewis/Loftus
//
//  Represents a circle with a particular position, size, and color.
//********************************************************************


class Circle
{
   private int diameter, x, y;
   private Color color;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this circle with the specified values.
   //-----------------------------------------------------------------
   public Circle (int size, Color shade, int upperX, int upperY)
   {
      diameter = size;
      color = shade;
      x = upperX;
      y = upperY;
   }

   //-----------------------------------------------------------------
   //  Draws this circle in the specified graphics context.
   //-----------------------------------------------------------------
   public void draw (Graphics page)
   {
      page.setColor (color);
      page.fillOval (x, y, diameter, diameter);
   }

   //-----------------------------------------------------------------
   //  Diameter mutator.
   //-----------------------------------------------------------------
   public void setDiameter (int size)
   {
      diameter = size;
   }

   //-----------------------------------------------------------------
   //  Color mutator.
   //-----------------------------------------------------------------
   public void setColor (Color shade)
   {
      color = shade;
   }

   //-----------------------------------------------------------------
   //  X mutator.
   //-----------------------------------------------------------------
   public void setX (int upperX)
   {
      x = upperX;
   }


   //-----------------------------------------------------------------
   //  Y mutator.
   //-----------------------------------------------------------------
   public void setY (int upperY)
   {
      y = upperY;
   }

   //-----------------------------------------------------------------
   //  Diameter accessor.
   //-----------------------------------------------------------------
   public int getDiameter ()
   {
      return diameter;
   }

   //-----------------------------------------------------------------
   //  Color accessor.
   //-----------------------------------------------------------------
   public Color getColor ()
   {
      return color;
   }

   //-----------------------------------------------------------------
   //  X accessor.
   //-----------------------------------------------------------------
   public int getX ()
   {
      return x;
   }

   //-----------------------------------------------------------------
   //  Y accessor.
   //-----------------------------------------------------------------
   public int getY ()
   {
      return y;
   }
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 12234114
public class TrafficLightPanel extends JPanel
{
 
  private int state = 1;
  private JButton push;
  private Circle redLight, yellowLight, greenLight;
  private Circle onLight;

  //----------------------------------------------------------------------------------
  //  Constructor: Sets up the main characteristics of the traffic light.
  //----------------------------------------------------------------------------------
  public TrafficLightPanel ()
  {
           
               
     //----------------------------Image stuff---------------------------------------------------              
         
          redLight = new Circle (50, Color.red, 35, 50);
          greenLight = new Circle (50, Color.green, 35, 125);
          yellowLight = new Circle (50, Color.yellow, 35, 200);
          onLight = redLight;
          setBackground (Color.black);                   // set background color
          setPreferredSize (new Dimension(120, 250));   // set the size of the whole window
     //----------------------------------------------------------------------------------------------
     
     //----------------------------Button stuff----------------------------------------------------
         
         
          push = new JButton ("Change!");  // Makes a new button called "push"
          push.addActionListener (new ButtonListener());   // Makes a new button listener attached to button "push"
         
          add (push);     // adds the button to the panel
     //----------------------------------------------------------------------------------------------
 
  }
     
     //---------------------------------------------------------------------------------
     //  Draw a traffic light
     //---------------------------------------------------------------------------------
          public void paintComponent (Graphics page)
          {
               super.paintComponent (page);
               
         
               onLight.draw(page);    
          }

     //-------------------------------------------------------------------------------------------
     // Represents a listener for button "push" events.
     //---------------------------------------------------------------------------------------------
     private class ButtonListener implements ActionListener
     {
        public void actionPerformed (ActionEvent event)
          {
               
               
               switch (state)
               {
                    default:
                        onLight = redLight;
                          state = state + 1;
                          break;
                    case 2:
                          onLight = greenLight;
                          state = state + 1;
                          break;
                    case 3:
                           onLight = yellowLight;
                           state = 1;                        
                         break;          
                    }    
               
               repaint();
          }
     
     }
}
0
 

Author Comment

by:romeotheriault
ID: 12234149
Thank you all, for the help.
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12234174
hmmmm objects also should get points
0
 

Author Comment

by:romeotheriault
ID: 12234250
how do i give more than one person points
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12234311
look at this qn
http://www.experts-exchange.com/Community_Support/Q_20304411.html?query=split+point&clearTAFilter=true

post 0 point question for splitting the points

objects:
congratulations  for 3million points.!!!

cheers!!!
mb...
0
 

Author Comment

by:romeotheriault
ID: 12236342
objects, nice clean code!! I'll read the article and get you some points if I can. Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 12244152
Thanks all :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question