Welcome to Dream.In.Code
Become a Java Expert!

Join 149,499 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,378 people online right now. Registration is fast and FREE... Join Now!




If statements

 
Reply to this topicStart new topic

If statements, Buttons with my if statements

programhelp
22 Mar, 2007 - 06:53 PM
Post #1

New D.I.C Head
*

Joined: 8 Mar, 2007
Posts: 9


My Contributions
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
        
}

User is offlineProfile CardPM
+Quote Post

spullen
RE: If Statements
23 Mar, 2007 - 01:47 PM
Post #2

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
Not too sure, but maybe if the if statements were else-if statements it would work?
User is offlineProfile CardPM
+Quote Post

Ryan747
RE: If Statements
23 Mar, 2007 - 05:10 PM
Post #3

New D.I.C Head
*

Joined: 16 Oct, 2006
Posts: 26


My Contributions
Or you could try this:

CODE

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

User is offlineProfile CardPM
+Quote Post

programhelp
RE: If Statements
25 Mar, 2007 - 12:10 PM
Post #4

New D.I.C Head
*

Joined: 8 Mar, 2007
Posts: 9


My Contributions
I tried both but I still don't get any action.


User is offlineProfile CardPM
+Quote Post

programhelp
RE: If Statements
25 Mar, 2007 - 12:21 PM
Post #5

New D.I.C Head
*

Joined: 8 Mar, 2007
Posts: 9


My Contributions
I guess I was missing my actionListener.

Thanks though!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:20PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month