If statements

Buttons with my if statements

Page 1 of 1

4 Replies - 2048 Views - Last Post: 25 March 2007 - 01:21 PM Rate Topic: -----

#1 programhelp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-March 07

If statements

Post icon  Posted 22 March 2007 - 07:53 PM

I have this simple little program to display a car in different locations with the press of each button, I was getting a display earlier of my car and of the traffic light but wanted to black out the lights that were not in use, now I get nothing but a display of the buttons and the black box that incloses the lights. Any suggestions?
/**
 * @(#)TrafficLight.java
 *
 * TrafficLight Applet application
 *
 * @author 
 * @version 1.00 2007/3/8
 */
 
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;


public class TrafficLight extends Applet implements ActionListener {
	
	//declarations
	private Button btnRed, btnYellow, btnGreen;
	private JPanel panel1;
	//setting the buttons to false
	private boolean btnRedClicked = false;
	private boolean btnYellowClicked = false;
	private boolean btnGreenClicked = false;
	private Image car;
		
	public void init() 
	{
		//load image of car
		car=getImage(getDocumentBase(),"car.jpg");
		//sets up the layout for the panel
		setLayout(new BorderLayout());
		//sets up the panel 
		panel1 = new JPanel();
		//sets up and adds the buttons to the panel
		btnGreen = new Button("Go");
		btnYellow = new Button("Wait");
		btnRed = new Button("Stop");
		panel1.add(btnRed);
		panel1.add(btnYellow);
		panel1.add(btnGreen);
		//adding panel to border layout
		add(panel1, BorderLayout.NORTH);
	}

	public void paint(Graphics g)
		{
		//set background color
		setBackground(Color.cyan);
		//make traffic light
		g.setColor(Color.black);
		g.fillRect(260,50,80,160);
		
		if(btnRedClicked == true)
		{
		//do the following if the stop button is clicked
		g.drawImage(car,40,270,220,100,this);	
		g.setColor(Color.red);
		g.fillOval(280,60,40,40);
		g.setColor(Color.black);
		g.fillOval(280,110,40,40);
		g.fillOval(280,160,40,40);
		btnRedClicked = false;
		}
	
		if(btnYellowClicked == true)
		{
		//do the following if the wait button is clicked
		g.drawImage(car,375,270,220,100,this);	
		g.setColor(Color.yellow);
		g.fillOval(280,110,40,40);
		g.setColor(Color.black);
		g.fillOval(280,60,40,40);
		g.fillOval(280,160,40,40);
		btnYellowClicked = false;
		}
				
		if(btnGreenClicked == true)
		{
		//do the following if the wait button is clicked
		g.drawImage(car,300,270,220,100,this);	
		g.setColor(Color.green);
		g.fillOval(280,160,40,40);
		g.setColor(Color.black);
		g.fillOval(280,110,40,40);
		g.fillOval(280,60,40,40);
		btnGreenClicked = false;
		}
				
	}
	public void actionPerformed(ActionEvent event)
	{
		//creating the action from the press of each button
		//displays the Green light
		if(event.getSource() == btnGreen)
		{
		btnGreenClicked = true;
		repaint();
		}//end of btnGreen
		
		//displays the Yellow light	
		if(event.getSource() == btnYellow)
		{
		btnYellowClicked = true;
		repaint();
		}//end of btnYellow
	
		//displays the Red light
		if(event.getSource() == btnRed)
		{
		btnRedClicked = true;
		repaint();
		}//end btnRed if
	}//end actionPerformed
		
}


Is This A Good Question/Topic? 0
  • +

Replies To: If statements

#2 spullen  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 356
  • Joined: 22-March 07

Re: If statements

Posted 23 March 2007 - 02:47 PM

Not too sure, but maybe if the if statements were else-if statements it would work?
Was This Post Helpful? 0
  • +
  • -

#3 Ryan747  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 16-October 06

Re: If statements

Posted 23 March 2007 - 06:10 PM

Or you could try this:

public void actionPerformed(ActionEvent e){
   if(e.getActionCommand().equals("Go")){
	   //action
   }else if(e.getActionCommand().equals("Wait")){
	   //action
   }
}


Was This Post Helpful? 0
  • +
  • -

#4 programhelp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-March 07

Re: If statements

Posted 25 March 2007 - 01:10 PM

I tried both but I still don't get any action.
Was This Post Helpful? 0
  • +
  • -

#5 programhelp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-March 07

Re: If statements

Posted 25 March 2007 - 01:21 PM

I guess I was missing my actionListener.

Thanks though!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1