4 Replies - 1547 Views - Last Post: 11 October 2012 - 01:45 PM Rate Topic: -----

#1 dhtkfk24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-October 12

traffic light does not change light when button is pushed

Posted 11 October 2012 - 01:59 AM

I am working on some problems from the back of the book and am not having successful with one of the program questions that asks to simulate a traffic light and use a push button to change the status of the light starting with red down to green and back upto red afterwards. and while one color is lit, the others should be black. I have my code so far but I am having trouble making the button work. and also I am trying to change the case/break statements into if else statements. also want to organize my code. Thanks
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrafficLightPanel extends JPanel
{

/**
	 * 
	 */
	private static final long serialVersionUID = 1L;



private JFrame jframe;
private JPanel jpanel;
private JButton push;
private TrafficLightPanel tPanel;

private class jbutton implements ActionListener
{
		 graphic g = getGraphic();
		 
	
public void actionPerformed(ActionEvent event)
{
	if (event.getSource()==push)
	{
		tPanel.changeColor();
	}
}
}


public TrafficLightPanel() {
	setBackground (Color.yellow);
	setPreferredSize (new Dimension (250,450));
	jframe = new JFrame ("exercise problem");
	jpanel = new JPanel();
	push = new JButton ("Change Light");
	add(push);
	
	
}

//ActionListener listener = new ActionListener();

/*public void launchApp () {
	jframe.setSize(300,500);
	jpanel.add(push);
	push.addActionListener(new jbutton());
	tPanel = new TrafficLightPanel();
	jframe.getContentPane().add(tPanel);
	jframe.getContentPane().add(jpanel, BorderLayout.NORTH);
	jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	jframe.setVisible(true); 
	}
 */ 

	 
	private int lightcolor = 1;
	public void changeColor() { 
		lightcolor++;
		
		if (lightcolor > 3)
		{ lightcolor = 1; }
		
		repaint();
	}
 
  public void paintComponent (Graphics g) 
  {
	  super.paintComponent(g);
	  g.setColor(Color.gray);
	  g.fillRect(30, 30, 190, 395);
	  
	 g.setColor(Color.black);
	 g.fillOval(70, 295, 100, 100);
	 
	 g.setColor(Color.black);
	 g.fillOval(70, 178, 100, 100);
	 
	 g.setColor(Color.black);
	 g.fillOval(70, 60, 100, 100);
	 
	 switch (lightcolor) {
	 case 1:
		 //green
		 g.setColor(Color.green);
		 g.fillOval(70, 295, 100, 100);
		 break;
		 
	 case 2:
		 //yellow
		 g.setColor(Color.yellow);
		 g.fillOval(70, 178, 100, 100);
		 break;
		 
	 case 3:
		 //red
		 g.setColor(Color.red);
		 g.fillOval(70, 60, 100, 100);
		 break;
		 
	 }
  }
  }



*Edited: {lease :code:

This post has been edited by pbl: 11 October 2012 - 03:45 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: traffic light does not change light when button is pushed

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: traffic light does not change light when button is pushed

Posted 11 October 2012 - 03:48 AM

Please indent your code properly
seems that some of it is commented out including the part that creates the tPanel and add the ActionListener to the button
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: traffic light does not change light when button is pushed

Posted 11 October 2012 - 03:53 AM

This is twice wrong
graphic g = getGraphic();
should be
Graphics g = getGraphic();

but never do this anyhow use the Graphics object that is provided to you as parameter in the paint() method

Also this code as compilation errors
In the future post them when you open a topic

This post has been edited by pbl: 11 October 2012 - 03:54 AM
Reason for edit:: Fixed bold tags

Was This Post Helpful? 0
  • +
  • -

#4 dhtkfk24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-October 12

Re: traffic light does not change light when button is pushed

Posted 11 October 2012 - 05:23 AM

how would i write a code in an if else statement to make the light change when the button is clicked?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: traffic light does not change light when button is pushed

Posted 11 October 2012 - 01:45 PM

You add an ActionListener to the JButton
In the actionPerformed() method you change the state of the button stored in an instance variable and you call repaint()
In the paint() method, you use the appropriate color and position based on the state maintained in the instance variable
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1