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?
CODE
/**
* @(#)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
}