i need help bleas in this code (GUI and Threads )

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ra7l
    New Member
    • Apr 2007
    • 11

    i need help bleas in this code (GUI and Threads )

    hi all bleas h need help in cods

    GUI and Thread

    i need help function
    start ()
    run()
    white ()
    join ()
    sleep ()
    setTitel ()
    suspond ()
    resume ()

    and This class

    class Thear
    class track
    class TestTrack
    class moveTrack
    class addCarrige

    bleas help :)


    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class MoveCar extends JFrame{
        
        static boolean START=false;
        static boolean Check=false;
    	static int SLEEP = 1000;
        static int INDEX_START=0;
        JPanel screen, area , controlroom;
        JButton[] street , keybord;
        String s[] = {"Start","Stop","Fast","Slow" ,"Rest","Check"};
        
        ImageIcon images[] =new ImageIcon[] {  
                        new ImageIcon("images/UpRight.jpg"),
                        new ImageIcon("images/RightDown.jpg"),
                        new ImageIcon("images/NoTrack.jpg"),
                        new ImageIcon("images/NoTrack.jpg"),
                        new ImageIcon("images/NoTrack.jpg"),
                        
                        new ImageIcon("images/Vert.jpg"),
                        new ImageIcon("images/DownRight.jpg"),
                        new ImageIcon("images/Horiz.jpg"),
                        new ImageIcon("images/RightDown.jpg"),
                        new ImageIcon("images/NoTrack.jpg"),
                        
                        new ImageIcon("images/Vert.jpg"),
                        new ImageIcon("images/NoTrack.jpg"),
                        new ImageIcon("images/NoTrack.jpg"),
                        new ImageIcon("images/Vert.jpg"),
                        new ImageIcon("images/NoTrack.jpg"), 
                        
                        new ImageIcon("images/Vert.jpg"),
                        new ImageIcon("images/UpRight.jpg"),
                        new ImageIcon("images/Horiz.jpg"),
                        new ImageIcon("images/RightUp.jpg"),
                        new ImageIcon("images/NoTrack.jpg"), 
                        
                        
                        new ImageIcon("images/DownRight.jpg"),
                        new ImageIcon("images/RightUp.jpg"),
                        new ImageIcon("images/NoTrack.jpg"), 
                        new ImageIcon("images/NoTrack.jpg"), 
                        new ImageIcon("images/NoTrack.jpg") 
        };
        
      
    ImageIcon[]imagesTrain= { "images/TrainUp.jpg","images/TrainLeft.jpg","images/TrainLeft.jpg","images/TrainUp.jpg","images/TrainLeft.jpg",
     "images/TrainLeft.jpg","images/TrainDown.jpg", "images/TrainDown.jpg","images/TrainDown.jpg","images/TrainDown.jpg",
     "images/TrainRight.jpg","images/TrainUp.jpg","images/TrainRight.jpg","images/TrainRight.jpg","images/TrainUp.jpg"};   
    
        
        int[]line={13, 18,17,16,21,20,15,10,5,0,1,6,7,8}; 
        	
        ImageIcon icon;
    
        JLabel labelInfo;
    
        
        public MoveTrain () {   
           screen = new JPanel(new GridLayout(1,1));
           Font f =new Font ("Sanserif",Font.BOLD,18);
           labelInfo = new JLabel("WELCOM" ,JLabel.CENTER);
           labelInfo.setFont(f);
                  
           screen.setBackground(Color.red);
           screen.add(labelInfo);
           getContentPane().add(BorderLayout.NORTH,screen );
            
            street = new JButton[images.length];
            area = new JPanel (new GridLayout(5,5));
            
            for(int i=0;i<images.length;i++){
                street[i] = new JButton("", images[i]);
                area.add(street[i]);
            }  
            getContentPane().add(BorderLayout.CENTER,area );    
            keybord = new JButton[6];
            controlroom = new JPanel    (new GridLayout(3,3));  
            
            for (int i=0; i <6; i++)
            {
                keybord[i] = new JButton(s[i]);
                keybord[i].addActionListener(getAction(s[i]));
                controlroom.add(keybord[i]);
                
            }       
            getContentPane().add(BorderLayout.SOUTH,controlroom );
        }
        
        
    
     
     
    class moveTrain extends Thread
    {
    	Trian t = new Train();
    	int Speed = 10;
    	t.Start();
    	int s = t.getSpeed();
    	Sleep(s);
    }
    void run()
    {
    	while(true)
    		Sleep()
    		Stop
    		t.moveTrain()
    }
    
     
    public class TrainTest      
        public static void main(String[]args){
            MoveCar c= new MoveCar ();
    		MoveTrain mt = new MoveTrain();
            c.setTitle("Train");
            c.setSize(421,550);
            c.setVisible(true);
            c.addWindowListener (
            new WindowAdapter (){
                 public void windowClosing(WindowEvent e ){
                      System.exit(0); 
                 }
            });
    		mt.start();
        }
    }
    this Link imges http://www.4shared.com/file/13555551/ff070bf/images.html

    Thanks all :)
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by ra7l
    hi all bleas h need help in cods
    I'm sorry for saying so, but that code is a mess: typos and logic errors,
    statements at class scope, constructors of one class defined in another class,
    etc.etc. etc. Please try to compile your code, carefully read what the
    compiler has to say about it, and try to fix the errors.

    I can't help you out with this without rewriting the entire shebang (which I'm
    not going to do). Programming is not about glueing pieces of code together.
    You have to carefully organize your classes, methods and statements.

    Best of luck and

    kind regards,

    Jos

    Comment

    • ra7l
      New Member
      • Apr 2007
      • 11

      #3
      ok sir Thanks to comment
      this new code but not use Theard

      i need Theard


      Code:
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      
      
      public class MoveCar extends JFrame{
          
          static boolean START=false;
          static boolean Check=false;
      	static int SLEEP = 1000;
          static int INDEX_START=0;
          JPanel screen, area , controlroom;
          JButton[] street , keybord;
          String s[] = {"Start","Stop","Fast","Slow" ,"Rest","Check"};
          
          ImageIcon images[] =new ImageIcon[] {  
                          new ImageIcon("images/UpRight.jpg"),
                          new ImageIcon("images/RightDown.jpg"),
                          new ImageIcon("images/NoTrack.jpg"),
                          new ImageIcon("images/NoTrack.jpg"),
                          new ImageIcon("images/NoTrack.jpg"),
                          
                          new ImageIcon("images/Vert.jpg"),
                          new ImageIcon("images/DownRight.jpg"),
                          new ImageIcon("images/Horiz.jpg"),
                          new ImageIcon("images/RightDown.jpg"),
                          new ImageIcon("images/NoTrack.jpg"),
                          
                          new ImageIcon("images/Vert.jpg"),
                          new ImageIcon("images/NoTrack.jpg"),
                          new ImageIcon("images/NoTrack.jpg"),
                          new ImageIcon("images/Vert.jpg"),
                          new ImageIcon("images/NoTrack.jpg"), 
                          
                          new ImageIcon("images/Vert.jpg"),
                          new ImageIcon("images/UpRight.jpg"),
                          new ImageIcon("images/Horiz.jpg"),
                          new ImageIcon("images/RightUp.jpg"),
                          new ImageIcon("images/NoTrack.jpg"), 
                          
                          
                          new ImageIcon("images/DownRight.jpg"),
                          new ImageIcon("images/RightUp.jpg"),
                          new ImageIcon("images/NoTrack.jpg"), 
                          new ImageIcon("images/NoTrack.jpg"), 
                          new ImageIcon("images/NoTrack.jpg") 
          };
          
          
      
          ImageIcon[]imagesTrain=new ImageIcon[]{ 
              new ImageIcon("images/TrainDown.jpg"),
              new ImageIcon("images/TrainDown.jpg"),
              new ImageIcon("images/TrainLeft.jpg"),
              new ImageIcon("images/TrainLeft.jpg"),
              new ImageIcon("images/TrainDown.jpg"),
              new ImageIcon("images/TrainLeft.jpg"),
              new ImageIcon("images/TrainUp.jpg"),
              new ImageIcon("images/TrainUp.jpg"),
              new ImageIcon("images/TrainUp.jpg"),
              new ImageIcon("images/TrainUp.jpg"),
              new ImageIcon("images/TrainRight.jpg"),
              new ImageIcon("images/TrainDown.jpg"),
              new ImageIcon("images/TrainRight.jpg"),
              new ImageIcon("images/TrainRight.jpg")
          };
      
      
      
      
          
          int[]line={13, 18,17,16,21,20,15,10,5,0,1,6,7,8}; 
           
      	
          ImageIcon icon;
      
          JLabel labelInfo;
      
          
          public MoveCar () {   
             screen = new JPanel(new GridLayout(1,1));
             Font f =new Font ("Sanserif",Font.BOLD,18);
             labelInfo = new JLabel("WELCOM" ,JLabel.CENTER);
             labelInfo.setFont(f);
                    
             screen.setBackground(Color.red);
             screen.add(labelInfo);
             getContentPane().add(BorderLayout.NORTH,screen );
              
              street = new JButton[images.length];
              area = new JPanel (new GridLayout(5,5));
              
              for(int i=0;i<images.length;i++){
                  street[i] = new JButton("", images[i]);
                  area.add(street[i]);
              }  
              getContentPane().add(BorderLayout.CENTER,area );    
              keybord = new JButton[6];
              controlroom = new JPanel    (new GridLayout(3,3));         
              for (int i=0; i <6; i++)
              {
                  keybord[i] = new JButton(s[i]);
                  keybord[i].addActionListener(getAction(s[i]));
                  controlroom.add(keybord[i]);
                  
              }       
              getContentPane().add(BorderLayout.SOUTH,controlroom );
          }
          
          private ActionListener getAction(String nameAction){
              if(nameAction.equalsIgnoreCase("Start")){              return new ActionListener(){
                      public void actionPerformed(ActionEvent e){
                          try{
                              labelInfo.setText(s[0]);
                              keybord[0].setEnabled(START);
                              START = true;    
                              startTrain();
                          }catch(Exception ex){
                              ex.printStackTrace();
                          }    
                      }
                  };
              }
              if(nameAction.equalsIgnoreCase("Stop")){              return new ActionListener(){
                      public void actionPerformed(ActionEvent e){
                          try{
                              labelInfo.setText(s[1]);
                              keybord[0].setEnabled(START);
                              START=false;
      						keybord[5].setEnabled(Check);
                              Check=false;
                          }catch(Exception ex){
                              ex.printStackTrace();
                          }    
                      }
                  };
              }
              if(nameAction.equalsIgnoreCase("Fast")){
                  return new ActionListener(){
                      public void actionPerformed(ActionEvent e){
                          try{
                              labelInfo.setText(s[2]);
                              SLEEP=500;
                          }catch(Exception ex){
                              ex.printStackTrace();
                          }    
                      }
                  };
              }
              if(nameAction.equalsIgnoreCase("Slow")){
                  return new ActionListener(){
                      public void actionPerformed(ActionEvent e){
                          try{
                              labelInfo.setText(s[3]);
                              SLEEP=5000;
                          }catch(Exception ex){
                              ex.printStackTrace();
                          }    
                      }
                  };
              }
              
      
      		
              return null;
          } 
          
          
      /**
      *     @author Final Heaven
      */
          public void startTrain() { 
              Thread    t    = new Thread(new Runnable()
      		
       {
                  public void run()  {
                      for(int i=INDEX_START; START; i++){                        
                          street[line[(i%14)]].setIcon(imagesTrain[(i%14)]); 
                          try{
                              Thread.currentThread().sleep(SLEEP);
                          }
                          catch(Exception ee){
                              ee.printStackTrace();
                          }
                          INDEX_START=i;
                          if(START)street[line[(i%14)]].setIcon(images[line[(i%14)]]);
      
                      }
                 
      		       }                   
                        
      			
              });
              t.start();
          }
       
       
          public static void main(String[]args){
              MoveCar c= new MoveCar ();
              c.setTitle("Train");
              c.setSize(421,550);
              c.setVisible(true);
              c.addWindowListener (
              new WindowAdapter (){
                   public void windowClosing(WindowEvent e ){
                        System.exit(0); 
                   }
              });
          }
      }


      Thanks :)

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by ra7l
        ok sir Thanks to comment
        this new code but not use Theard

        i need Theard


        Code:
        import java.awt.*;
        import java.awt.event.*;
        import javax.swing.*;
         
         
        public class MoveCar extends JFrame{
         
        static boolean START=false;
        static boolean Check=false;
        	static int SLEEP = 1000;
        static int INDEX_START=0;
        JPanel screen, area , controlroom;
        JButton[] street , keybord;
        String s[] = {"Start","Stop","Fast","Slow" ,"Rest","Check"};
         
        ImageIcon images[] =new ImageIcon[] { 
        new ImageIcon("images/UpRight.jpg"),
        new ImageIcon("images/RightDown.jpg"),
        new ImageIcon("images/NoTrack.jpg"),
        new ImageIcon("images/NoTrack.jpg"),
        new ImageIcon("images/NoTrack.jpg"),
         
        new ImageIcon("images/Vert.jpg"),
        new ImageIcon("images/DownRight.jpg"),
        new ImageIcon("images/Horiz.jpg"),
        new ImageIcon("images/RightDown.jpg"),
        new ImageIcon("images/NoTrack.jpg"),
         
        new ImageIcon("images/Vert.jpg"),
        new ImageIcon("images/NoTrack.jpg"),
        new ImageIcon("images/NoTrack.jpg"),
        new ImageIcon("images/Vert.jpg"),
        new ImageIcon("images/NoTrack.jpg"), 
         
        new ImageIcon("images/Vert.jpg"),
        new ImageIcon("images/UpRight.jpg"),
        new ImageIcon("images/Horiz.jpg"),
        new ImageIcon("images/RightUp.jpg"),
        new ImageIcon("images/NoTrack.jpg"), 
         
         
        new ImageIcon("images/DownRight.jpg"),
        new ImageIcon("images/RightUp.jpg"),
        new ImageIcon("images/NoTrack.jpg"), 
        new ImageIcon("images/NoTrack.jpg"), 
        new ImageIcon("images/NoTrack.jpg") 
        };
         
         
         
        ImageIcon[]imagesTrain=new ImageIcon[]{ 
        new ImageIcon("images/TrainDown.jpg"),
        new ImageIcon("images/TrainDown.jpg"),
        new ImageIcon("images/TrainLeft.jpg"),
        new ImageIcon("images/TrainLeft.jpg"),
        new ImageIcon("images/TrainDown.jpg"),
        new ImageIcon("images/TrainLeft.jpg"),
        new ImageIcon("images/TrainUp.jpg"),
        new ImageIcon("images/TrainUp.jpg"),
        new ImageIcon("images/TrainUp.jpg"),
        new ImageIcon("images/TrainUp.jpg"),
        new ImageIcon("images/TrainRight.jpg"),
        new ImageIcon("images/TrainDown.jpg"),
        new ImageIcon("images/TrainRight.jpg"),
        new ImageIcon("images/TrainRight.jpg")
        };
         
         
         
         
         
        int[]line={13, 18,17,16,21,20,15,10,5,0,1,6,7,8}; 
         
         
        ImageIcon icon;
         
        JLabel labelInfo;
         
         
        public MoveCar () { 
        screen = new JPanel(new GridLayout(1,1));
        Font f =new Font ("Sanserif",Font.BOLD,18);
        labelInfo = new JLabel("WELCOM" ,JLabel.CENTER);
        labelInfo.setFont(f);
         
        screen.setBackground(Color.red);
        screen.add(labelInfo);
        getContentPane().add(BorderLayout.NORTH,screen );
         
        street = new JButton[images.length];
        area = new JPanel (new GridLayout(5,5));
         
        for(int i=0;i<images.length;i++){
        street[i] = new JButton("", images[i]);
        area.add(street[i]);
        } 
        getContentPane().add(BorderLayout.CENTER,area ); 
        keybord = new JButton[6];
        controlroom = new JPanel (new GridLayout(3,3)); 
        for (int i=0; i <6; i++)
        {
        keybord[i] = new JButton(s[i]);
        keybord[i].addActionListener(getAction(s[i]));
        controlroom.add(keybord[i]);
         
        } 
        getContentPane().add(BorderLayout.SOUTH,controlroom );
        }
         
        private ActionListener getAction(String nameAction){
        if(nameAction.equalsIgnoreCase("Start")){ return new ActionListener(){
        public void actionPerformed(ActionEvent e){
        try{
        labelInfo.setText(s[0]);
        keybord[0].setEnabled(START);
        START = true; 
        startTrain();
        }catch(Exception ex){
        ex.printStackTrace();
        } 
        }
        };
        }
        if(nameAction.equalsIgnoreCase("Stop")){ return new ActionListener(){
        public void actionPerformed(ActionEvent e){
        try{
        labelInfo.setText(s[1]);
        keybord[0].setEnabled(START);
        START=false;
        						keybord[5].setEnabled(Check);
        Check=false;
        }catch(Exception ex){
        ex.printStackTrace();
        } 
        }
        };
        }
        if(nameAction.equalsIgnoreCase("Fast")){
        return new ActionListener(){
        public void actionPerformed(ActionEvent e){
        try{
        labelInfo.setText(s[2]);
        SLEEP=500;
        }catch(Exception ex){
        ex.printStackTrace();
        } 
        }
        };
        }
        if(nameAction.equalsIgnoreCase("Slow")){
        return new ActionListener(){
        public void actionPerformed(ActionEvent e){
        try{
        labelInfo.setText(s[3]);
        SLEEP=5000;
        }catch(Exception ex){
        ex.printStackTrace();
        } 
        }
        };
        }
         
         
         
        return null;
        } 
         
         
        /**
        * @author Final Heaven
        */
        public void startTrain() { 
        Thread t = new Thread(new Runnable()
         
        {
        public void run() {
        for(int i=INDEX_START; START; i++){ 
        street[line[(i%14)]].setIcon(imagesTrain[(i%14)]); 
        try{
        Thread.currentThread().sleep(SLEEP);
        }
        catch(Exception ee){
        ee.printStackTrace();
        }
        INDEX_START=i;
        if(START)street[line[(i%14)]].setIcon(images[line[(i%14)]]);
         
        }
         
        		 } 
         
         
        });
        t.start();
        }
         
         
        public static void main(String[]args){
        MoveCar c= new MoveCar ();
        c.setTitle("Train");
        c.setSize(421,550);
        c.setVisible(true);
        c.addWindowListener (
        new WindowAdapter (){
        public void windowClosing(WindowEvent e ){
        System.exit(0); 
        }
        });
        }
        }


        Thanks :)
        Have you gone through a Threads tutorial yet?

        Comment

        • ra7l
          New Member
          • Apr 2007
          • 11

          #5
          Originally posted by r035198x
          Have you gone through a Threads tutorial yet?

          yes more search but dont to know more Threads

          Thanks :)

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by ra7l
            yes more search but dont to know more Threads

            Thanks :)
            Did you read the sun tutorial?
            Last edited by r035198x; Apr 23 '07, 01:00 PM. Reason: fixing the broken link

            Comment

            • ra7l
              New Member
              • Apr 2007
              • 11

              #7
              Originally posted by r035198x
              Did you read the sun tutorial?
              Hi admin The Link is Error
              Thanks to reply :)

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by ra7l
                Hi admin The Link is Error
                Thanks to reply :)
                That's funny. It was opening fine yesterday.

                Comment

                • ra7l
                  New Member
                  • Apr 2007
                  • 11

                  #9
                  Originally posted by r035198x
                  That's funny. It was opening fine yesterday.
                  very funny :)

                  ok admin in fine give me correct cods :)

                  and more Thanks

                  Comment

                  • ra7l
                    New Member
                    • Apr 2007
                    • 11

                    #10
                    i am wait for replay :(

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by ra7l
                      i am wait for replay :(
                      I'm sure if you go through a decent threads tutorial you will be able to write the codes yourself.

                      Comment

                      • teddarr
                        New Member
                        • Oct 2006
                        • 143

                        #12
                        Jos...he's right about the link, I couldn't get it to open either.

                        Ra71...Here's the URL.

                        http://java.sun.com/docs/books/tutorial/essential/concurrency/

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by teddarr
                          Jos...he's right about the link, I couldn't get it to open either.

                          Ra71...Here's the URL.

                          http://java.sun.com/docs/books/tutor...l/concurrency/
                          It's not Jos' fault. I posted the broken link and was surprised later to find that it doesn't open correctly.

                          Comment

                          • ra7l
                            New Member
                            • Apr 2007
                            • 11

                            #14
                            Thanks tedder Thanks admin r035198x

                            The Linke very good Tody

                            Thanks All :)

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by ra7l
                              Thanks tedder Thanks admin r035198x

                              The Linke very good Tody

                              Thanks All :)
                              I edited the link yesterday. I had posted it wrong the first time.

                              Comment

                              Working...