Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need some serious help with my traffic light - GUI help!

setpreferredsize

  • Please log in to reply
3 replies to this topic

#1 vctrshowalter

vctrshowalter

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 02 November 2011 - 09:03 AM

So my prompt is this:
Design and implement an application that draws a traffic light and uses a push button to change the state of the light. Derive the drawing surface from the JPanel class and use another panel to organize the drawing surface and the button.

I understand well enough how to make buttons and listeners (i think) but the actual drawing of the circles for the light is not working out.

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

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

TrafficLightPanel panel = new TrafficLightPanel();

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

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

//************************************************************
//	Victoria Showalter		Lab 6a
//	Creates a traffic light.
//************************************************************

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

public class TrafficLightPanel extends JPanel
{
	private int count;
	private JButton status;
	private JLabel label;

	
	//--------------------------------------------------------
	//	Constructor: Sets up the GUI
	//--------------------------------------------------------
	public TrafficLightPanel ()
	{
		int red = 0;
		int yellow = 1;
		int green = 2;
		
		status = new JButton ("Change!");
		status.addActionListener (new ButtonListener());
		
		label = new JLabel ("Traffic");
		
		add (status);
		add (label);

		setPreferredSize (new Dimension (300,200));
		setBackground (Color.black);

	}
	
	//--------------------------------------------------------
	//	Represents a new listener for button push.
	//--------------------------------------------------------
	private class ButtonListener implements ActionListener
	{
		//----------------------------------------------------
		//	Changes the color of the traffic light when the 
		//	button is pushed.
		//----------------------------------------------------
		public void actionPerformed (ActionEvent event)
		{
			count++;
			count = count % 3; // take the remainder of count/3, yielding 0, 1 or 2.
		}
		
	//--------------------------------------------------------
	//	Draws traffic light
	//--------------------------------------------------------
	public void paintComponent (Graphics page)
		{
			TrafficLightPanel.super.paintComponent (page);
			
		if (count == 0)
			{
				page.setColor(Color.red);
				page.fillOval (20, 30, 50, 65);
			}
		if (count == 1)
			{
				page.setColor(Color.yellow);
				page.fillOval (20, 30, 50, 65);
			}
		if (count == 2)
			{
				page.setColor(Color.green);
				page.fillOval (20, 30, 50, 65);
			}

		page.setColor(Color.white);
		page.drawString("Ready...Set...GO!!!",10,180);
		}

	}
	

}


How do I physically make the traffic light? I have no errors thusfar and it compiles, but only displays a button that says "Change!" on a black screen. Please and thank you
  • 0

#2 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 02 November 2011 - 02:23 PM

It looks like you've accidentally added your paintComponent method inside of your inner class! This should fix your problem. Then you'll be able to see how to fix your lights.
  • 0

#3 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 03 November 2011 - 07:51 AM

Do you have prof Darlene? I've got the same assignment. See this thread: http://forum.codecal...rough-help.html
  • 0

#4 vctrshowalter

vctrshowalter

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 06 November 2011 - 08:52 AM

Yes, I do have prof Darlene! and thank you, I'm fixing it now and I think I can make it work.
  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download