Link to home
Start Free TrialLog in
Avatar of romeotheriault
romeotheriault

asked on

Traffic Light -- Very Simple

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;
   }
}



Avatar of Mick Barry
Mick Barry
Flag of Australia image

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().
Avatar of romeotheriault
romeotheriault

ASKER

What exactly do you mean member variable, example?
ASKER CERTIFIED SOLUTION
Avatar of expertmb
expertmb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you all, for the help.
hmmmm objects also should get points
how do i give more than one person points
look at this qn
https://www.experts-exchange.com/questions/20304411/Split-Points.html?query=split+point&clearTAFilter=true

post 0 point question for splitting the points

objects:
congratulations  for 3million points.!!!

cheers!!!
mb...
objects, nice clean code!! I'll read the article and get you some points if I can. Thanks
Thanks all :)