Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Trouble with repainting a traffic light using radio buttons: Any Suggestions?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble with repainting a traffic light using radio buttons: Any Suggestions?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.Graphics;
    import java.awt.event.*;
     
    public class TrafficLight extends JFrame {
     
      private NewLabel lights = new NewLabel();
     
      private JRadioButton red, yellow, green;
     
      public TrafficLight() {
     
        JPanel p1 = new JPanel();
     
        p1.setLayout(new GridLayout(1,3));
     
        add(lights, BorderLayout.CENTER);
        add(p1, BorderLayout.SOUTH);
     
      	p1.add(red = new JRadioButton("Red"));
    	p1.add(yellow = new JRadioButton("Yellow"));
    	p1.add(green = new JRadioButton("Green"));
     
    	//Button Group
    	ButtonGroup group = new ButtonGroup();
    	group.add(red);
    	group.add(yellow);
    	group.add(green);
     
      }
     
      class LightListener implements ActionListener {
      	public void actionPerformed (ActionEvent e) {
     
      	red.addActionListener(new LightListener());
    	green.addActionListener(new LightListener());
    	yellow.addActionListener(new LightListener());
     
    	if (e.getSource() == red)
    		System.out.println("red");
     
    	else if (e.getSource() == yellow)
    		System.out.println("yellow");
     
    	else if (e.getSource() == green)
    		System.out.println("green");
     
      	}
      }
     
     
      public static void main(String[] args) {
        TrafficLight frame = new TrafficLight();
        frame.setTitle("TrafficLight");
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 250);
        frame.setVisible(true);
      }
     
    class NewLabel extends JPanel {
     
     
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
     
     
        g.drawRect(10,10,50,150);
        g.drawOval(10,10,50,50);
        g.drawOval(10,60,50,50);
        g.drawOval(10,110,50,50);
     
     
      }
    }
    }


    I can't make it so that when you click the radio button it repaints the circle the appropriate color. In this case, the colors of a traffic light.

    If the source is red: It needs to paint the top circle red

    If the source is yellow: It needs to paint the center circle yellow

    If the source is red: It needs " " " red...

    I'm having trouble repainting it after the radio buttons are selected. I also don't even know if the radio buttons are doing anything... help is appreciated.

    -Nate
    Last edited by Ha1luciNate; October 10th, 2011 at 01:52 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble with repainting a traffic light using radio buttons: Any Suggestions?

    I don't see any code that changes any color.

    I don't see when you add a LightListener to anything, except in the LightListener itself, which doesn't make sense.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with repainting a traffic light using radio buttons: Any Suggestions?

    No kidding, that's why I'm asking. How do I make it so that when I click the button it changes the color. I have all the listeners setup and it draws the lights. Now I just need it to change the colors.

    Thank you for re-stating my question

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with repainting a traffic light using radio buttons: Any Suggestions?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    //Credit to Dr.Roberson for ControlCircle2 which was used as the basis for this program.
    //Nate Heels:
     
    public class TrafficLight extends JFrame {
      Color x = Color.white;
      Color y = Color.white;
      Color z = Color.white;
     
      private JRadioButton jbtRed = new JRadioButton("red");
      private JRadioButton jbtYellow = new JRadioButton("yellow");
      private JRadioButton jbtGreen = new JRadioButton("green");
     
      private CirclePanel canvas = new CirclePanel();
     
      public TrafficLight() {
        JPanel panel = new JPanel();
        panel.add(jbtRed);
        panel.add(jbtYellow);
        panel.add(jbtGreen);
     
        panel.setBackground(Color.white);
        canvas.setBackground(Color.white);
     
        ButtonGroup group = new ButtonGroup();
    	group.add(jbtRed);
    	group.add(jbtYellow);
    	group.add(jbtGreen);
     
        this.add(canvas, BorderLayout.CENTER);
        this.add(panel, BorderLayout.SOUTH);
     
        jbtRed.addActionListener(new Listener());
        jbtYellow.addActionListener(new Listener());
        jbtGreen.addActionListener(new Listener());
     
      }
     
     
      public static void main(String[] args) {
        JFrame frame = new TrafficLight();
        frame.setTitle("TrafficLight");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 250);
        frame.setVisible(true);
      }
     
      class Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
          System.out.println(new java.util.Date(e.getWhen()));
          if (e.getSource() == jbtRed)
            canvas.paintred();
          else if (e.getSource() == jbtYellow)
        	canvas.paintyellow();
          else if (e.getSource() == jbtGreen)
          	canvas.paintgreen();
        }
      }
     
      class CirclePanel extends JPanel {
     
        public void paintred() {
          x = Color.red;
          y = Color.white;
          z = Color.white;
          repaint();
        }
     
        public void paintyellow() {
          x = Color.white;
          y = Color.yellow;
          z = Color.white;
          repaint();
        }
     
        public void paintgreen() {
          x = Color.white;
          y = Color.white;
          z = Color.green;
          repaint();
        }
     
        protected void paintComponent(Graphics g) {
          super.paintComponent(g);
     
    	g.setColor(x);
    	g.fillOval(10,10,50,50);
     
    	g.setColor(y);
    	g.fillOval(10,60,50,50);
     
    	g.setColor(z);
    	g.fillOval(10,110,50,50);
     
    	g.setColor(Color.black);
        g.drawRect(10,10,50,150);
        }
      }
    }

    I figured it out by taking a completely different approach.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble with repainting a traffic light using radio buttons: Any Suggestions?

    Quote Originally Posted by Ha1luciNate View Post
    Thank you for re-stating my question
    No problem, especially considering that your original post does not include an actual question (usually they end in question marks). You were obviously not far from a solution, but you didn't say what confused you or what you were expecting to happen. You simply posted an incomplete solution without an actual question. What kind of a response were you expecting?

    Anyway, I did my best to point out the things you were missing in the hopes that you would attempt them and come back if you got stuck, maybe with a more specific question. The attitude is unnecessary, and it's only going to decrease your chances of getting help in the future.

    Glad you got it figured out though.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Location
    Chino CA
    Posts
    5
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with repainting a traffic light using radio buttons: Any Suggestions?

    Wait, what part of this code adds the button group to the panel? I'm missing something obvious... (new to programming, sorry)

Similar Threads

  1. [SOLVED] Creating Dynamically Radio Buttons, Labels
    By Onur in forum Java Theory & Questions
    Replies: 15
    Last Post: August 25th, 2011, 08:39 AM
  2. Help with radio buttons and results.
    By Bewitched1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 3rd, 2011, 04:01 PM
  3. radio buttons stop working
    By tabutcher in forum AWT / Java Swing
    Replies: 2
    Last Post: March 5th, 2010, 09:28 AM
  4. [SOLVED] Using html Radio Buttons with Servlets
    By oss_2008 in forum Java Servlet
    Replies: 2
    Last Post: June 25th, 2009, 05:39 AM