Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Tolls

Thread: why my car can not turn left and turn 5 round,Help!!THx

  1. #1
    seongchog is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Exclamation why my car can not turn left and turn 5 round,Help!!THx

    THis is my question:
    Hi I have to do where I have to make a race between five cars (this assignment is a continuation of a previos 'mini' assignment where I had to do the 'race' with one car).

    The commands that I implemented for the cars is foward (increases distance travelled), reverse (decreases the distance travelled), left and right (left and right doesn't increase or decrease the distance). Now what I need to do is make a race between two cars where what is currently displayed needs to be accompanied by the commands of the other car.

    Console example of the layout.
    To make the two columns you can use \t.

    Car1. . . . . . . . . . . . . . . . .Car5

    Forward 1km. (total). . . . . . Right.
    Left. . . . . . . . . . . . . . . . . Reverse -1km. (total)
    Forward 4km. (total). . . . . . Left.
    Reverse 3km. (total). . . . . . Forward 3km. (total)


    ================================================== ================================================== =======
    Need some help to solve my code .
    Thx You you All.




    ================================================== ================================================== =======
    This is my code:

    Java Code:
    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.URL;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.Timer;
    
    
    
    public class Race1 extends Applet implements Runnable {
        
        
        Image img1;
        Image img2;
        Image img3;
        Image img4;
        Image img5;
    
        URL url12;
        URL url13;
        URL url14;
        URL url15;
        Image img[] = new Image[4];
    
        JLabel jlbl = new JLabel();
    
        int index2 = 0;
        int lap = 0;
        
        
        int x=400, x2=405, x3=400, x4=400, x5=400, y=0,y2=0,y3=0,y4=0,y5=0;  // what mean? control cordinate x ... car move
        
        int index;
        Thread animate = null;
        Timer t1,t2,t3,t4,t5;
    
        
        public void init() {
            img1 = getImage(this.getClass().getResource("car10.png"));
            img2 = getImage(this.getClass().getResource("car20.png"));
            img3 = getImage(this.getClass().getResource("car30.png"));
            img4 = getImage(this.getClass().getResource("car40.png"));
            img5 = getImage(this.getClass().getResource("car50.png"));
    
           setLayout(new BorderLayout());
           ImageIcon img6 = new ImageIcon(getClass().getResource("road.jpg"));
           jlbl.setIcon(img6);
           add(jlbl, BorderLayout.CENTER);
            
           //img6 = getImage(this.getClass().getResource("road.jpg"));
    
            setSize(1000, 300);
    
            URL url2 = this.getClass().getResource("F1.png");
            img[0] = getImage(url2);
    
    
    
            URL url3 = this.getClass().getResource("@.png");
            img[1] = getImage(url3);
    
            URL url4 = this.getClass().getResource("Penang.png");
            img[2] = getImage(url4);
    
            URL url5 = this.getClass().getResource("2013.png");
            img[3] = getImage(url5);
    
            
        }
    
        public void start(){
            
            if (animate == null){
                animate = new Thread(this);
                animate.start();
            
            }//end if start
            
        }//end start function
        
        public void run(){
    
            if(lap <2){
            t1 = new Timer((int)(Math.random()*500),
                    new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        if(x <= 650){
                            x+=10;
                        }
                        else if(x <= 660) {
                            x+=20;
                            y+=100;
                        }
                        else
                            x=400;
                            
                        repaint();
                    }
            
              });//end actionListener
              t1.start();
              
        
            t2 = new Timer((int)(Math.random()*500),
                    new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        if(x2 <= 650){
                            x2+=10;
                        }
                         else if(x2 <= 660) {
                            x2+=20;
                            y2+=100;
                        }
                        else
                            x2=400;
                        repaint();
                    }
             });
             t2.start();
             
    
             t3 = new Timer((int)(Math.random()*500),
                    new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        if(x3 <= 650)
                            x3+=10;
                         else if(x3 <= 660) {
                            x3+=20;
                            y3+=100;
                        }
                        else
                            x3=400;
                        repaint();
                    }
             });
             t3.start();
             
    
             t4 = new Timer((int)(Math.random()*500),
                    new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        if(x4 <= 650)
                            x4+=10;
                         else if(x4 <= 660) {
                            x4+=50;
                            y4+=120;
                        }
                        else
                            x4=400;
                        repaint();
                    }
             });
             t4.start();
             
    
             t5 = new Timer((int)(Math.random()*500),
                    new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        if(x5 <= 650)
                            x5+=10;
                         else if(x5 <= 660) {
                            x5+=50;
                            y5+=120;
                        }
                        else
                            x5=400;
                            repaint();
                    }
             });
             t5.start();
    
             
            }
    
            else{
            
                t1.stop();
                t2.stop();
                t3.stop();
                t4.stop();
                t5.stop();
            }
    
            while(true){
    
           if(index2 <3){
                  index2++;
                  repaint();
            }
           else{
               index2=0;
               repaint();
           }
           try{
                Thread.sleep(1000);
           }catch(Exception e)
           {}
          }
    
        
        }//end run
    
        public void paint(final Graphics g){
    
            //g.dreawImage(Image img, int x, int y, int size-width,
            //int size-hight, ImageObserver observer);
    
            super.paintComponents(g);
            //g.drawImage(img6, 50, 50, 1500,1000, null);
    
            if(x <= 650 && x2 <= 650 && x3 <= 650 && x4 <= 650 && x5 <= 650){
            g.drawImage(img1, x, 490, 90, 45, null);
            g.drawImage(img2, x2, 530,70, 25, null);
            g.drawImage(img3, x3, 555,80, 33, null);
            g.drawImage(img4, x4, 590,80, 35, null);
            g.drawImage(img5, x5, 620,80, 50, null);
            }
    
           else if(x <= 660 || x2 <=660 || x3 <= 660 || x4 <= 660 || x5 <= 660){
            g.drawImage(img1, 999, 700, 90, 45, null);
            g.drawImage(img2, 950, 670, 90, 45, null);
            g.drawImage(img3, 900, 620, 90, 45, null);
            g.drawImage(img4, 880, 590, 90, 45, null);
            g.drawImage(img5, 850, 550, 90, 45, null);
            }
            
    
            
    
            g.drawImage(img[index2],560,300,299,76, null);
            g.drawImage(img[index2],560,300,299,76, null);
            g.drawImage(img[index2],560,300,299,76, null);
            g.drawImage(img[index2],560,300,299,76, null);
            
            
    
    
    
        }
        
        
                
               
        
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: why my car can not turn left and turn 5 round,Help!!THx

    First of all, that is waaay too much code. Code should be posted in the form of an SSCCE, not hundreds of lines of your whole assignment.

    Secondly, what's your actual question? Where are you stuck? What have you tried? What worked, what didn't work? How didn't it work?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    seongchog is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: why my car can not turn left and turn 5 round,Help!!THx

    i need the java scrip use "if , else " statment . 5 cars race in 3 lap , after 3 lap the 5 cars will stop at the begening part . must use applet

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: why my car can not turn left and turn 5 round,Help!!THx

    Java != javascript.

    Recommended reading: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Also, check out the link in my signature on asking questions the smart way.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    seongchog is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: why my car can not turn left and turn 5 round,Help!!THx

    here is my output :why  my car can not turn left and turn 5 round,Help!!THx-untitled.png

    The coding can run but five of the car can not run over the lap and can not run in 5 cars race in 3 lap , and then after 3 lap the 5 cars will stop at the begening part?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: why my car can not turn left and turn 5 round,Help!!THx

    Have you tried debugging the code by adding println statements to show the execution flow and the values of variables as they change and as they are used? The print out will show you what the program is doing so you can fix it.

    Have you tried the program with fewer cars? Does it work correctly?

  7. #7
    seongchog is offline Member
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: why my car can not turn left and turn 5 round,Help!!THx

    I tried the Program with the Cars but the car just going farword and can not turn left when wan turn on the lap .Besides, My cars can not run in 5 cars race in 3 lap , and then after 3 lap the 5 cars will stop at the begening part?

    thx All IT professional~~

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: why my car can not turn left and turn 5 round,Help!!THx

    Produce a simple program that moves a single car around the track.
    Forget about racing 5 cars.
    You want to ensure a single car can move around the track.

    Once you have that the rest should be easy.

    That should also significantly reduce the code above to a level someone here might be able to see what's going on.
    Norm likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Turn GUI Into Use-Able Program
    By Feriscool in forum Eclipse
    Replies: 3
    Last Post: 06-17-2011, 02:23 AM
  2. how can i turn my application into an applet
    By HtotheP in forum Java Applets
    Replies: 2
    Last Post: 12-18-2010, 11:20 AM
  3. Can't turn on breadcrumbs in enclipse
    By Jubei1980 in forum Eclipse
    Replies: 0
    Last Post: 11-24-2008, 10:08 PM
  4. Turn off sounds in Eclipse
    By gio.fou in forum Eclipse
    Replies: 2
    Last Post: 09-06-2008, 08:31 PM

Posting Permissions

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