Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Traffic Light problems

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Traffic Light problems

    I am working on a program that simulates a traffic light. The program lets the user select one of three lights: red, yellow, or green. When a radio button is selected, the light is turned on, and only one light can be on at a time.
    I have the code written, the only problem that i am having is displaying the traffic light itself. If someone could help me out it would be greatly appreciated.

    Here is what i have coded:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class trafficLights extends JFrame {
     
     
    	private JRadioButton jrbRed, jrbYellow, jrbGreen;
    	private boolean jrbRedClicked = false;
    	private boolean jrbYellowClicked = false;
    	private boolean jrbGreenClicked = false;
     
     
    	public static void main(String[] args) {
    		trafficLights frame = new trafficLights();
    		frame.setTitle("Exercise 17_3");
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.pack();
    		frame.setVisible(true);
    	} // end of main
     
     
    public trafficLights() {
     
    	JPanel jpRadioButtons = new JPanel();
    	jpRadioButtons.setLayout(new FlowLayout());
    	jpRadioButtons.add(jrbRed = new JRadioButton("Red"));
    	jpRadioButtons.add(jrbYellow = new JRadioButton("Yellow"));
    	jpRadioButtons.add(jrbGreen = new JRadioButton("Green"));
    	add(jpRadioButtons, BorderLayout.SOUTH);
     
    	JPanel drawLight = new JPanel();
        drawLight.setLayout(new BorderLayout());
    	add(drawLight, BorderLayout.CENTER);
    	drawLight.add(jpRadioButtons, BorderLayout.SOUTH);
     
     
    	//keyboard mnemonics
    	jrbRed.setMnemonic('R');
    	jrbYellow.setMnemonic('Y');
    	jrbGreen.setMnemonic('G');
     
    	jrbRed.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			if(jrbRed.isSelected())
    			{
    				jrbRedClicked = true;
    				repaint();
    			}
    		}
    	});
    	jrbYellow.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {		
    			if(jrbYellow.isSelected())
    			{
    				jrbYellowClicked = true;
    				repaint();
    			}
    		}
    	});	
    	jrbGreen.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {	
    			if(jrbGreen.isSelected())
    			{
    				jrbGreenClicked = true;
    				repaint();
    			}
    		}
    	}); // end of actionlistener()
     
    } // end of trafficLights()
     
     
    protected void paintComponent(Graphics g) {
    	super.paintComponents(g);
     
    	setBackground(Color.CYAN);
    	g.setColor(Color.BLACK);
    	g.drawRect(260, 50, 80, 160);
    	g.setColor(Color.BLACK);
    	g.drawRect(140, 50, 80, 160);
     
    	if(jrbRedClicked == true)
    	{
    		g.setColor(Color.RED);
    		g.fillOval(280, 60, 40, 40);
    		g.setColor(Color.BLACK);
    		g.drawOval(280, 110, 40, 40);
    		g.drawOval(280, 160, 40, 40);
    		g.drawRect(260, 50, 80, 160);
    		jrbRedClicked = false;
    	}
     
    	if(jrbYellowClicked == true)
    	{
    		g.setColor(Color.YELLOW);
    		g.fillOval(280, 110, 40, 40);
    		g.setColor(Color.BLACK);
    		g.drawOval(280, 50, 80, 160);
    		g.drawOval(280, 160, 40, 40);
    		g.drawRect(260, 50, 80, 160);
    		jrbYellowClicked = false;
    	}
     
    	if(jrbGreenClicked == true)
    	{
    		g.setColor(Color.GREEN);
    		g.fillOval(280, 160, 40, 40);
    		g.setColor(Color.BLACK);
    		g.drawOval(280, 60, 40, 40);
    		g.drawOval(280, 110, 40, 40);
    		g.drawRect(260, 50, 80, 160);
    		jrbGreenClicked = false;
    	}
    }// end of paint component
     
    } // end of public class
    Last edited by sircamolate; November 25th, 2011 at 09:38 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Traffic Light problems

    Add a call to revalidate? You aren't very specific about the problem.

    Also, never use
    if(boolean == true)
    {
     
    }
    Always do
    if(boolean)
    {
     
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Traffic Light problems

    Add a println statement to the paintComponent method to be sure it is being called.
    Add a @Override statement just before the paintComponent definition to be sure that the method is overriding correctly.
    Make the size of the frame big enough to display the light in.

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Traffic Light problems

    why should i never do:
    if(boolean == true)
    {
     
    }

    and do:
    if(boolean)
    {
     
    }

    what's the difference in the output/outcome?

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Traffic Light problems

    Quote Originally Posted by sircamolate View Post
    why should i never do:
    if(boolean == true)
    {
     
    }

    and do:
    if(boolean)
    {
     
    }

    what's the difference in the output/outcome?
    Nothing, it's just ugly. Plus it's an additional calculation that you don't have to do. Plus, what if you want to change the meaning of a boolean? You'd have to go back and change all of your if statements to match.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Traffic Light problems

    Plus many students miscode the statement this way:
    if(boolean = true)
    which makes for a hard bug to find.

  7. The Following User Says Thank You to Norm For This Useful Post:

    KevinWorkman (November 28th, 2011)

Similar Threads

  1. [SOLVED] NIO Problems
    By bgroenks96 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: November 12th, 2011, 06:26 PM
  2. Replies: 5
    Last Post: October 17th, 2011, 07:43 AM
  3. 2 problems...
    By Day2Day in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2010, 02:51 PM
  4. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM