Results 1 to 1 of 1
  1. #1
    ugur is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default School assigment - problem with switching lights for traffic light in Netbeans

    The purpose of this assigment is that when i press the button on the red button, the red light appears and the same for the other colors, the problem is that it does not work for the other 2 buttons while the code of those buttons is exaclt the same. When i change knophandler 2 & 3 to 1 they turn red if i click on them but i cant seem to let it work the right color.

    Java Code:
    package javaapplication3;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
        class Verkeerslicht extends JPanel{
            private JButton red, orange,green;
            
            
            private final int horizontaal = 50;
            private final int verticaal = 50;
            private final int breedte = 50;
            private final int hoogte = 50;
            private final int hoogte2 = 180;
            private int stater;
            private int stateo;
            private int stateg;
            
            
          public Verkeerslicht (){
              setBackground( Color.gray);
              red = new JButton("red");
              red.addActionListener ( new KnopHandler1());
               
              orange = new JButton("orange");
              orange.addActionListener ( new KnopHandler2()); 
              
              green = new JButton("green");
              green.addActionListener ( new KnopHandler3());
              
              add ( red);
              add (orange);
              add (green);
          }
        
        @Override
      public void paintComponent (Graphics g ) {
          super.paintComponent( g );
          g.setColor(Color.black);
             g.fillRect( 40, 80, 70, hoogte2);
          g.setColor(Color.black);
             g.fillRect( 65, 210, 20, hoogte2);
             
          g.setColor(Color.red);
             g.fillOval( horizontaal, 85, breedte , hoogte);
          g.setColor(Color.orange);
             g.fillOval( horizontaal, 140, breedte ,hoogte);
          g.setColor(Color.green);
             g.fillOval( horizontaal, 195, breedte , hoogte);
             
             
             if (stater == 1) {
              g.setColor(Color.black);
             g.fillRect( 40, 80, 70, hoogte2);
          g.setColor(Color.black);
             g.fillRect( 65, 210, 20, hoogte2);
              g.setColor(Color.red);
             g.fillOval( horizontaal, 85, breedte , hoogte);
             }
             
            
                 
             else if (stateo == 2) {
              g.setColor(Color.black);
             g.fillRect( 40, 80, 70, hoogte2);
          g.setColor(Color.black);
             g.fillRect( 65, 210, 20, hoogte2);
              g.setColor(Color.orange);
             g.fillOval( horizontaal, 140, breedte ,hoogte);
             }
         if (stateg == 3) {
              g.setColor(Color.black);
             g.fillRect( 40, 80, 70, hoogte2);
          g.setColor(Color.black);
             g.fillRect( 65, 210, 20, hoogte2);
              g.setColor(Color.green);
             g.fillOval( horizontaal, 195, breedte , hoogte);
             }
             
        }
      class KnopHandler1 implements ActionListener {
            @Override
          public void actionPerformed(ActionEvent e)
          {
            stater = 1;
            repaint();
          }
            class KnopHandler2 implements ActionListener {
            @Override
          public void actionPerformed(ActionEvent e)
          {
            stateo = 2;
            repaint();
          }
             class KnopHandler3 implements ActionListener {
            @Override
          public void actionPerformed(ActionEvent e)
          {
            stateg = 3;
            repaint();
          }
        }
       }
      }
    }
    Last edited by DarrylBurke; 12-10-2011 at 03:39 PM. Reason: Added code tags

Similar Threads

  1. Traffic signal light
    By rhymeinkpot in forum Threads and Synchronization
    Replies: 1
    Last Post: 07-31-2011, 04:07 PM
  2. Compile problem when switching Java versions
    By siwelb in forum New To Java
    Replies: 2
    Last Post: 04-26-2010, 10:12 PM
  3. Help with my traffic light program
    By topps in forum New To Java
    Replies: 4
    Last Post: 04-09-2010, 03:56 PM
  4. my java assigment problem!
    By lieni in forum New To Java
    Replies: 16
    Last Post: 01-28-2010, 01:06 AM
  5. intro to java problem for school
    By thr33 in forum New To Java
    Replies: 1
    Last Post: 10-16-2009, 02:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •