8 Replies - 1796 Views - Last Post: 24 March 2012 - 11:08 AM Rate Topic: ***-- 2 Votes

#1 BudgieBr0phy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-March 12

Traffic Light Question

Posted 24 March 2012 - 06:26 AM

Hey guys,

I am having a little problem with my code at the minute. I have my traffic light system set up so that when I click on the "Red" button, the red light lights up and the yellow and green dim down. The thing I want is, I want to be able to click on the circle that the colour is in, and not a button. And I am not too sure on how to do that.... :whatsthat: :dontgetit:

Another feature I am trying to add is that when I click on a colour, it tells you the name of the colour you just clicked on, and what that colour means by the rules of the road. So when the user clicks on the green circle, it will tell them in a textbox that they have clicked on Green, and that it means it is Ok to drive off now, etc... I can do this bit myself I think! :huh: :/

So if somebody could please help me with that, it would be much appreciated! :D :bigsmile:

Here is what I have so far..

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

public class TrafficLights extends JFrame implements ActionListener {
    JButton b1, b2, b3;


      Signal green = new Signal(Color.green);
      Signal yellow = new Signal(Color.yellow);
      Signal red = new Signal(Color.red);

    public TrafficLights(){
        super("Traffic Lights");
        getContentPane().setLayout(new GridLayout(2, 1));
        b1 = new JButton("Red");
        b2 = new JButton("Yellow");
        b3 = new JButton("Green");
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);

        green.turnOn(false);
        yellow.turnOn(false);
        red.turnOn(true);

        JPanel p1 = new JPanel(new GridLayout(3,1));
        p1.add(red);
        p1.add(yellow);
        p1.add(green);
        JPanel p2 = new JPanel(new FlowLayout());
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);

        getContentPane().add(p1);
        getContentPane().add(p2);
        pack();
        }


    public static void main(String[] args){
        TrafficLights tl = new TrafficLights();
        tl.setVisible(true);
    }
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == b1){
            green.turnOn(false);
            yellow.turnOn(false);
            red.turnOn(true);

        } else if (e.getSource() == b2){
            yellow.turnOn(true);
            green.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == b3){
            red.turnOn(false);
            yellow.turnOn(false);
            green.turnOn(true);
        }
    }
}
class Signal extends JPanel{

    Color on;
    int radius = 40;
    int border = 10;
    boolean change;

    Signal(Color color){
        on = color;
        change = true;
    }

    public void turnOn(boolean a){
        change = a;
        repaint();
    }

    public Dimension getPreferredSize(){
        int size = (radius+border)*2;
        return new Dimension( size, size );
    }

    public void paintComponent(Graphics g){
        g.setColor( Color.black );
        g.fillRect(0,0,getWidth(),getHeight());

        if (change){
            g.setColor( on );
        } else {
            g.setColor( on.darker().darker().darker() );
        }
        g.fillOval( border,border,2*radius,2*radius );
    }
}
 


To anyone that can help me with this problem, thank you VERY much! ;) :clap:

Budgie :gunsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Traffic Light Question

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Traffic Light Question

Posted 24 March 2012 - 06:31 AM

Add a mouseListener to the graphic to determine the x, y coordinates of mouse clicks, then program what you want to happen when the user clicks depending on where the cursor is in the graphic.

Good first post, by the way! Welcome to DIC.

Edit: Also add:

setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

to your JFrame.

This post has been edited by GregBrannon: 24 March 2012 - 06:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 BudgieBr0phy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-March 12

Re: Traffic Light Question

Posted 24 March 2012 - 06:40 AM

I have the Exit Operation added now, forgot to add it earlier... not entirely sure as to where I put the mouseListener though! :/

Sorry
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Traffic Light Question

Posted 24 March 2012 - 06:48 AM

Add it to you Signal JPanel. There are several ways to do it, but something like the following in your Signal constructor would be a start:

addMouseListener( new MouseActionHandler( this ) );

Or you could implement the Mouse Listener interface:

class Signal extends JPanel implements MouseListener

You can learn more about programming mouse listeners here.
Was This Post Helpful? 1
  • +
  • -

#5 BudgieBr0phy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-March 12

Re: Traffic Light Question

Posted 24 March 2012 - 07:30 AM

Awesome! I will try and fix this up and I will update the code in the first post if I have any more problems! :)

Thanks Greg! :)

Budgie
Was This Post Helpful? 0
  • +
  • -

#6 BudgieBr0phy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-March 12

Re: Traffic Light Question

Posted 24 March 2012 - 09:17 AM

I have made no progress on this yet! >.< I just want to :gun_bandana: down this program and start a new one! I am determined to finish it! I just can't! >.<

Kevin(Budgie)
Was This Post Helpful? 0
  • +
  • -

#7 BudgieBr0phy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-March 12

Re: Traffic Light Question

Posted 24 March 2012 - 10:53 AM

Ok, so I have narrowed it down now... I am only getting one error now! But I don't know how to fix it! :/

This is the error;
C:\Users\BudgieBrophy\Desktop\TrafficLights.java:84: cannot find symbol
symbol  : class MouseActionHandler
location: class Signal
    addMouseListener(new MouseActionHandler(this));
                         ^
1 error

Tool completed with exit code 1




And here is the edited code from earlier;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.event.MouseListener.*;

public class TrafficLights extends JFrame implements ActionListener {

    JPanel p1, p2, p3;


      Signal green = new Signal(Color.green);
      Signal yellow = new Signal(Color.yellow);
      Signal red = new Signal(Color.red);

    public TrafficLights(){
        super("Traffic Lights");
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        getContentPane().setLayout(new GridLayout(2, 1));


        green.turnOn(false);
        yellow.turnOn(false);
        red.turnOn(true);

        JPanel p1 = new JPanel(new GridLayout(3,1));
        p1.add(red);
        p1.add(yellow);
        p1.add(green);
        JPanel p2 = new JPanel(new FlowLayout());
        p2.add(red);
        p2.add(yellow);
        p2.add(green);

        getContentPane().add(p1);
        getContentPane().add(p2);
        pack();
        }


    public static void main(String[] args){
        TrafficLights tl = new TrafficLights();
        tl.setVisible(true);
    }
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == red){
            green.turnOn(false);
            yellow.turnOn(false);
            red.turnOn(true);

        } else if (e.getSource() == yellow){
            yellow.turnOn(true);
            green.turnOn(false);
            red.turnOn(false);
        } else if (e.getSource() == green){
            red.turnOn(false);
            yellow.turnOn(false);
            green.turnOn(true);
        }
    }
}
class Signal extends JPanel{



	Color on;
    int radius = 40;
    int border = 10;
    boolean change;


    Signal(Color color){
        on = color;
        change = true;
    addMouseListener(new MouseActionHandler(this));

    }

    public void turnOn(boolean a){
        change = a;
        repaint();
    }

    public Dimension getPreferredSize(){
        int size = (radius+border)*2;
        return new Dimension( size, size );
    }

    public void paintComponent(Graphics g){
        g.setColor( Color.black );
        g.fillRect(0,0,getWidth(),getHeight());

        if (change){
            g.setColor( on );
        } else {
            g.setColor( on.darker().darker().darker() );
        }
        g.fillOval( border,border,2*radius,2*radius );
    }
}


Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Traffic Light Question

Posted 24 March 2012 - 10:59 AM

Where did you declare your MouseActionHandler class?
Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Traffic Light Question

Posted 24 March 2012 - 11:08 AM

Dangit! Lost a post. Oh well, here it goes again . . .

MouseActionHandler is an example name of a class that you would write to handle mouse actions. You can use that name or any other you like, but if using that name, a skeleton of what that class might look like is here:
class MouseActionHandler extends MouseInputAdapter
{
   // you then add handlers for the mouse events
   // you want to capture and react to in your
   // program, like:
   public void mousePressed( MouseEvent e )
   {
      // add code here for handling moused presses
      // on the object of interest
   {

}  // end class MouseActionHandler

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1