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 2 of 2

Thread: Help me out with my code

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    36
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Post Help me out with my code

    I have to display traffic lights and run dialog boxes. When I do this I get the dialog boxes to appear properly but my traffic light stays green for the whole thing, instead of going red, yellow, green. I feel I am pretty close with my code and I just think maybe I'm using repaint wrong or have to do something with the setColor. Here is my working code so far.

    //Import JComponent Graphics, Graphic2D, Color
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import javax.swing.JComponent;
    import javax.swing.JOptionPane;
     
    public class TrafficLight extends JComponent
    {
       private Color lightColor;
       private int radius;
       private int x;
       private int y;
     
     /* public TrafficLight(Color lightColor, int radius, int x, int y)
       {
       this.lightColor = lightColor;
       this.radius = radius;
       this.x = x;
       this.y = y;
       }*/
     
       public void paintComponent(Graphics g)
       {
       //convert g to graphics 2d and use to draw ellipse for circle.
       //set color to lightColor
           Graphics2D g2 = (Graphics2D) g;
            Rectangle box = new Rectangle(0,0,300,400);
            g2.setColor(lightColor.WHITE);
            g2.fill(box);    
     
            Ellipse2D.Double redRing = new Ellipse2D.Double(35,75,200,200);
            g2.setColor(lightColor.RED);
            g2.draw(redRing);
            g2.fill(redRing);
            validate();
            repaint(); 
     
           Ellipse2D.Double yellowRing = new Ellipse2D.Double(35,75,200,200);
            g2.setColor(lightColor.YELLOW);
            g2.draw(yellowRing);
            g2.fill(yellowRing);
            validate();
            repaint();
     
     
     
            Ellipse2D.Double greenRing = new Ellipse2D.Double(35,75,200,200);
            g2.setColor(lightColor.GREEN);
            g2.draw(greenRing);
            g2.fill(greenRing);
            validate();
            repaint();
     
     
     
     
            /* Rectangle body = new Rectangle(x, y + 10, 60, 10);      
             Ellipse2D.double redLight = new Ellipse2D.double(x + 40, y + 20, radius + 10, 10, 10);
             Ellipse2D.Double yellowLight 
               = new Ellipse2D.Double(x + 40, y + 20, radius + 10, 10, 10);
             Ellipse2D.Double greenLight 
               = new Ellipse2D.Double(x + 40, y + 20, radius + 10, 10, 10);*/
      }
     
       public void setColor(Color newLightColor)
       {
         lightColor = newLightColor;
          //return lightColor;
          //newLightColor = lightColor.YELLOW;
     
     
       }
       }



    //Traffic Light viewer
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
     
    public class TrafficLightViewer
    {
       public static void main(String[] args)
       {
     
          JFrame LightVisible = new JFrame();
          LightVisible.setSize(400,400);
          LightVisible.setTitle("Traffic Lights");
          LightVisible.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          TrafficLight TrafficViewerOne = new TrafficLight();
         JOptionPane.showMessageDialog(null,"Ready!");
         //LightVisible.setColor(TrafficViewerOne);
         //LightVisible.setColor(redRing);
          LightVisible.add(TrafficViewerOne);
           LightVisible.setVisible(true);
     
     
         JOptionPane.showMessageDialog(null,"Set!");
         JOptionPane.showMessageDialog(null,"Go!");
        }
     
    }

    Any help is much appreciated, just need some advice in the right direction. It's almost like there should be stop and then repaint the color but I don't know how to this. Do I have to place the showMessageDialog code in a certain spot. Thanks for your time.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help me out with my code

    Traffic lights don't go red, yellow, green, but I'll assume that the requirements are dictated by the assignment.

    You have all of the necessary elements, some of them unused, others used incorrectly. I'm not going to show you anything new by talking you through each of the necessary changes, so I'll mention the important changes and then post your corrected code:

    paintComponent(() should begin with a call to the super's paintComponent() method

    NEVER, NEVER, NEVER call repaint from the paintComponent() method. Doing so creates an infinite loop and indicates you don't understand Swing's painting mechanism. You can learn more about it in Oracle's Custom Painting lesson.

    Use the setColor() method between dialog boxes to change the light's color. (Should be named setLightColor().)

    You might want to move the first dialog to after the setVisible() statement and then alternate change color / new dialog, but that's up to you.

    Here's the working code:
    //Traffic Light viewer
    //Import JComponent Graphics, Graphic2D, Color
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.geom.Ellipse2D;
     
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
     
     
    public class TestClass
    {
        public static void main(String[] args)
        {
            JFrame LightVisible = new JFrame();
            LightVisible.setSize(400,400);
            LightVisible.setTitle("Traffic Lights");
            LightVisible.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            TrafficLight trafficViewerOne = new TrafficLight();
     
            JOptionPane.showMessageDialog(null,"Ready!");
     
            LightVisible.add(trafficViewerOne);
     
            LightVisible.setVisible(true);
     
     
            JOptionPane.showMessageDialog(null,"Set!");
     
            trafficViewerOne.setColor( Color.YELLOW );
     
            JOptionPane.showMessageDialog(null,"Go!");
     
            trafficViewerOne.setColor( Color.GREEN );
        }
     
    }
     
    class TrafficLight extends JComponent
    {
        private Color lightColor;
     
        public TrafficLight()
        {
            // set initial color to green
            lightColor = Color.RED;
        }
     
        public void paintComponent(Graphics g)
        {
            // always begin the paintComponent() method with:
            super.paintComponent( g );
     
            //convert g to graphics 2d and use to draw ellipse for circle.
            //set color to lightColor
            Graphics2D g2 = (Graphics2D) g;
            Rectangle box = new Rectangle(0,0,300,400);
            g2.setColor(Color.WHITE);
            g2.fill(box);    
     
            Ellipse2D.Double lightRing = new Ellipse2D.Double(35,75,200,200);
            g2.setColor( lightColor );
            g2.draw(lightRing);
            g2.fill(lightRing);
        }
     
        public void setColor(Color newLightColor)
        {
            lightColor = newLightColor;
            repaint();
        }
     
    }

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Edmacelroy (September 29th, 2013)

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM