This is an assignment I am working on for uni and I have done quite alot by myself and with some hints along the way from others who know Java better than I do. So I am working on a GUI Traffic Simulator (creating the GUI and drawing it is easy) and for the most part I have wrapped my mind around the coding and got it working no worries.

It may seem odd, but I am now working on getting the lights to work and only show one colour at a time and not 3 that are shown when the GUI is run and it is drawn by the code. I am thinking I would use an if statement (which I don't know how to do) that would only display the light that is active and set the other 2 to the same colour as the background or to opaque or translucent. There may seem to be a lot of commented out lines, I am leaving them in so I know how I did that part or what I used as a guideline.

The way it is meant to work using a MVC style is the Controller will call the function/s and the View will display them on the GUI. The javacode is coded below and I will attach the files and the jar file. If it is needed another way let me know how to do it and I will send it or attach it here. Also there is absolutely nothing wrong with the jar file inside of the zip, I have worked on this on university computers that have a great AV system as do my own personal computers at home with Kaspersky AV.

Finally if you copy the code here or the java files, they will need to be put in a folder with 3 folders inside of that with the package names otherwise it won't work without refactoring each file.

package Controller;

import View.MyFrame;
//import Model.TrafficLight;

/**
 *
 * @author Simon Marshall
 * @author jc209673
 */
public class Simulator {

    /**
     *
     * @param args initializes and runs the simulation
     */
    public static void main(String[] args) {

        MyFrame frame = new MyFrame();
        frame.setVisible(true);
    }
}
package Model;

/**
 *
 * @author Simon Marshall
 * @author jc209673
 */
public class Cars {

    // Cars are in an ArrayList
    // Cars spawn at start of lane in horizontal or vertical
    // Cannot move unless there is at least 1 free space ahead of its current location

}
package Model;

/**
 *
 * @author Simon Marshall
 * @author jc209673
 */
public class TrafficLight {

    // Red, Yellow and Green states
    // When Red: cars do not proceed into intersection and wait until green.
    // When Yellow: cars may not proceed through intersection.
    // When Green: cars proceed as normal.
    //simulator calls and view will read - implement this.

    boolean isRed; // Set to false by default
    boolean isOrange; // Set to false by default
    boolean isGreen; // Set to false by default

    public boolean getIsGreen() {
        return isGreen;
    }

    public void setIsGreen(boolean isGreen) {
        this.isGreen = isGreen;
    }

    public boolean getsIsOrange() {
        return isOrange;
    }

    public void setIsOrange(boolean isOrange) {
        this.isOrange = isOrange;
    }

    public boolean getIsRed() {
        return isRed;
    }

    public void setIsRed(boolean isRed) {
        this.isRed = isRed;
    }
}
package View;

import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
//import Model.TrafficLight;
//import java.util.Random;

/**
 *
 * @author Simon Marshall
 * @author jc209673
 */
public class Display extends JPanel {

    /**
     * Sets the background colour to Green (0x008000) and sets the GUI size to a static 600 pixels in width by  approximately 600 pixels  in height
     */
    public Display() {
        setBackground(new Color(0x008000));
        setPreferredSize(new Dimension(600, 600));
    }

    /**
     * repaints the GUI
     */
    public void run() {
        repaint();
    }

    /**
     *
     * @param g Draws Horizontal and Vertical Lanes and Traffic Lights for Horizontal and Vertical Lanes
     */
    @Override
    public void paintComponent(Graphics g) {
        g.setColor(new Color(0x008000));
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(Color.GRAY);

        g.fillRect(getWidth() / 2 - 30, 0, 60, getHeight()); // Sets the vertical lane
        g.fillRect(0, getHeight() / 2 - 30, getWidth(), 60); // Sets the horizontal lane

        g.setColor(Color.BLACK); // Draws and sets the colour for the Horizontal and Vertical Traffic Light Box
        g.fillRect(getWidth() / 2 + 50, 0, 90, 45); // Draws the Vertical Traffic Lights
        g.fillRect(0, getHeight() / 3 - 10, 90, 45); // Draws the Horizontal Traffic Lights

        g.setColor(Color.RED); // Draws red light for Horizontal Lights
        g.fillOval(10, getHeight() / 3 + 5, 15, 15);
        
        g.setColor(Color.ORANGE); // Draws orange light for Horizontal Lights
        g.fillOval(10 + 25, getHeight() / 3 + 5, 15, 15);
        
        g.setColor(Color.GREEN); // Draws green light for Horizontal Lights
        g.fillOval(10 + 50, getHeight() / 3 + 5, 15, 15);
        
        g.setColor(Color.RED);
        g.fillOval(getWidth() / 2 + 65, 15, 15, 15); // Draws red light for Vertical Lights

        g.setColor(Color.ORANGE);
        g.fillOval(getWidth() / 2 + 90, 15, 15, 15); // Draws orange light for Vertical Lights
        
        g.setColor(Color.GREEN);
        g.fillOval(getWidth() / 2 + 115, 15, 15, 15); // Draws green light for Vertical Lights

    }
}
package View;

//import java.awt.Dimension;
//import javax.swing.BoxLayout; // old layout
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Simon Marshall
 * @author jc209673
 */
public class MyFrame extends JFrame { //implements ActionListener {

    private Display display;
    private JSlider jslider;
    private JComboBox jcombobox;
    private JButton jbutton;
    private JLabel HSliderValue;
    private JLabel VSliderValue;
    private JLabel SimCycleValue;
    
    private ChangeListener HSliderListener = new ChangeListener() {
// Watches for a change in the slider and updates the label dynamically to reflect the change
        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();
            HSliderValue.setText("" + source.getValue());
        }
    };
    private ChangeListener VSliderListener = new ChangeListener() {
// Watches for a change in the slider and updates the label dynamically to reflect the change
        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();
            VSliderValue.setText("" + source.getValue());
        }
    };
    private ChangeListener SimCycleListener = new ChangeListener() {
// Watches for a change in the slider and updates the label dynamically to reflect the change
        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();
            SimCycleValue.setText("" + source.getValue());
        }
    };

    /**
     * Adds a JPanel to the bottom of the JFrame and adds Labels, Comboboxes, Sliders and Event Handlers
     */
    public MyFrame() {


        // populate 1st and 2nd comboboxes using an uneditable combobox
        //http://download.oracle.com/javase/tutorial/index.html
        //http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html#uneditable
        //http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

        setTitle("Demo Version 2.00a");
        setVisible(true);

        display = new Display();
        add(display, BorderLayout.CENTER);

        JPanel MenuPanel = new JPanel();
        this.getContentPane().add(MenuPanel, BorderLayout.SOUTH);

        //MenuPanel.setLayout(new BoxLayout(MenuPanel, BoxLayout.Y_AXIS)); // use a gridlayout 7 columns 2 rows // old Panel layout
        MenuPanel.setLayout(new GridLayout(7, 2));

        //String[] petStrings = {"Bird", "Cat", "Dog", "Rabbit", "Pig"}; //example of an uneditable combobox
        //Create the combo box, select item at index 4.
        //Indices start at 0, so 4 specifies the pig.
        //JComboBox petList = new JComboBox(petStrings);
        //petList.setSelectedIndex(4);
        //petList.addActionListener(this);

        //jcombobox = new JComboBox(); // Menu Option 1 - 1, 2 or 3 as choices with 2 as default.
        String[] optionOneStrings = {"1", "2", "3"};
        MenuPanel.add(new JLabel("How many Horizontal Lanes? [min 1, max 3]"));
        jcombobox = new JComboBox(optionOneStrings);
        jcombobox.setSelectedIndex(1);
        //jcombobox.addActionListener((ActionListener) this);
        MenuPanel.add(jcombobox);

        JComboBox jcombobox1 = new JComboBox(); // Menu Option 2 - 1, 2, 3 or 4 as choices with 2 as default
        String[] optionTwoStrings = {"1", "2", "3", "4"};
        MenuPanel.add(new JLabel("How many Vertical Lanes? [min 1, max 4]"));
        //jcombobox1 optionTwoStrings = new JComboBox(optionTwoStrings);
        jcombobox1 = new JComboBox(optionTwoStrings);
        jcombobox1.setSelectedIndex(1);
        //jcombobox1.addActionListener((ActionListener) this);
        MenuPanel.add(jcombobox1);

        JPanel VSliderPanel = new JPanel();
        JSlider jslidervertical = new JSlider(); // Menu Option 3 - 0.1 to 1.0 as choices with 0.5 as default
        jslidervertical.addChangeListener(VSliderListener);
        MenuPanel.add(new JLabel("Probability of a car entering Vertical Lane? [min 0%, max 100%]"));
        MenuPanel.add(VSliderPanel);
        VSliderValue = new JLabel("50");
        VSliderPanel.add(jslidervertical, BorderLayout.CENTER);
        VSliderPanel.add(VSliderValue);

        JPanel HSliderPanel = new JPanel();
        JSlider jsliderhorizontal = new JSlider(); // Menu Option 4 - 0.1 to 1.0 as choices with 0.5 as default
        jsliderhorizontal.addChangeListener(HSliderListener);
        MenuPanel.add(new JLabel("Probability of a car entering Horizontal Lane? [min 0%, max 100%]"));
        MenuPanel.add(HSliderPanel);
        HSliderValue = new JLabel("50");
        HSliderPanel.add(jsliderhorizontal, BorderLayout.CENTER);
        HSliderPanel.add(HSliderValue);

        String[] optionFourStrings = {"Yes", "No"};
        JComboBox jcombobox4 = new JComboBox(); // Menu Option 5 - yes or no as choices
        MenuPanel.add(new JLabel("Run one simulation cycle?"));
        jcombobox4 = new JComboBox(optionFourStrings);
        jcombobox4.setSelectedIndex(1);
        //jcombobox4.addActionListener((ActionListener) this);
        MenuPanel.add(jcombobox4);

        JPanel SimCyclePanel = new JPanel();
        JSlider jslidersimulationcycle = new JSlider(); // Menu Option 6 - 1 to 100 as choices with 50 as default
        jslidersimulationcycle.addChangeListener(SimCycleListener);
        MenuPanel.add(new JLabel("Run number of simulation cycles [min 1, max 100]"));
        MenuPanel.add(SimCyclePanel);
        SimCycleValue = new JLabel("50");
        SimCyclePanel.add(jslidersimulationcycle, BorderLayout.CENTER);
        SimCyclePanel.add(SimCycleValue);

        //JButton jbuttonStart = new JButton(); // Start simulation
        //MenuPanel.add(new JLabel("Start Simulation"));
        MenuPanel.add(new JButton("Start Simulation"));
        //MenuPanel.add(jbuttonStart);

        //JButton jbuttonStop = new JButton(); // Stop simulation
        //MenuPanel.add(new JLabel("Stop Simulation"));
        MenuPanel.add(new JButton("Stop Simulation"));
        //MenuPanel.add(jbuttonStop);

        //JButton jbuttonExit = new JButton(); // Exit simulation
        //MenuPanel.add(new JLabel("Exit Simulation"));
        //MenuPanel.add(new JButton("Exit Simulation"));
        //MenuPanel.add(jbuttonExit);

        //        jslider = new JSlider();
        //        jslider.addChangeListener(new ChangeListener() {
        //
        //            public void stateChanged(ChangeEvent e) {
        //                System.out.println(jslider.getValue());
        //            }
        //        });
        //
        //        MenuPanel.add(new JLabel("test"));
        //        MenuPanel.add(jslider);

        display.repaint();
        pack();
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);



    }
    //public void actionPerformed(ActionEvent ae) {
    //System.out.println(ae.getActionCommand(ae.paramString())); //add something to identify which combobox option has being changed.
}
//}

what exactly is your question? if it is for custom made code, the answer is: no
for doing something similar as to what you ask with an if-statement

// this is, assuming you have enable/disable Green/Orange/Red Light functions
public void setLight(int light){
  if ( light == 1 ){
    enableGreenLight();
    disableOrangeLight();
    disableRedLight();
  }
  else{
    if ( light == 2 ){
      disableGreenLight();
      enableOrangeLight();
      disableRedLight();
    }
    else{
      disableGreenLight();
      disableOrangeLight();
      enableRedLight();
    }
  }
}
commented: Just what I was needing to progress +1

I haven't done the function/s that enables/disables the lights but what you have posted is what I needed, just need to figure out those enable and disable functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.