Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that simulates a race , . The car moves from left to right.

ID: 3542797 • Letter: W

Question

Write a Java program that simulates a race, . The car moves from left to right. When it hits the right end, it restarts from the left and continues the same process. You can use a timer to control animation. Redraw the car with a new base coordinates (x, y), as shown in Figure 16.23b. Also let the user pause/resume the animation with a button press/release and increase/decrease the carspeed by pressing the UP and DOWN arrow keys.


here is my code so far


package car;


import java.awt.Color;

import java.awt.Graphics;

import java.awt.Polygon;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Timer;


public class Car extends JFrame

{

   public Car ()


   

   {

       add(new CarImage());

   }

    

    

    public static void main(String[] args)

    {

        JFrame frame = new Car();

        String Car = null;

        frame.setTitle(Car);

        frame.setSize(300, 150);

        frame.setLocationRelativeTo(null);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }

    

    class CarImage extends JPanel

    {

        protected int x = 0;

        protected int y = 150;

        protected int z = 300;

        protected int c = 0;

        public CarImage ()

                

        {

        

        Timer timer1 = new Timer(50,new ActionListener()

    

        

        {

           

               

                @Override

            public void actionPerformed(ActionEvent e)

                    

            {

              x += 10;

              c++;

              repaint ();

            }

        });

        

        timer1.start();

        

      }          

                

        

      

        @Override

           public void paintComponent (Graphics g)

           {

               super.paint(g);

               x = 0;

               y = getHeight();

               z = getWidth();

               g.setColor(Color.WHITE);

               g.fillRect(0, 0,z, y);

               

               Polygon  polygon = new  Polygon();

               polygon.addPoint( 10,- 21);

               polygon.addPoint(x + 20, y - 31);

               polygon.addPoint(x + 30, y - 31);

               polygon.addPoint(x + 40, y - 21);

               if (x < z - 40)

               {

                   g.setColor(Color.BLACK);

                   g.fillOval(x + 10, y - 11,10,10);

                   g.fillOval(x + 10, y - 11,10,10);

                   g.setColor(Color.BLUE);

                   g.fillRect(x , y - 21,50,10);

                   g.setColor(Color.GRAY);

                   g.fillPolygon(polygon);

                   g.setColor(Color.red);

                   

               }

               else

                   x = 0;

               if (c<z-86)

                   g.drawString("  ",c, y-51);

               else

                   c = 0;

                   

               }

           }

            

        

        

        

    }

    

Explanation / Answer

Fixed : You were resetting x to 0


Please rate


import java.awt.Color;

import java.awt.Graphics;

import java.awt.Polygon;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Timer;


public class Car extends JFrame

{

public Car ()

{

add(new CarImage());

}

public static void main(String[] args)

{

JFrame frame = new Car();

String Car = null;

frame.setTitle(Car);

frame.setSize(300, 150);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

class CarImage extends JPanel

{

protected int x = 0;

protected int y = 150;

protected int z = 300;

protected int c = 0;

public CarImage ()

{

Timer timer1 = new Timer(50,new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

x += 10;

c++;

repaint();

}

});

timer1.start();

}

@Override

public void paintComponent (Graphics g)

{

//x=0;

y = getHeight();

z = getWidth();

g.setColor(Color.WHITE);

g.fillRect(0, 0,z, y);

Polygon polygon = new Polygon();

polygon.addPoint( 10,- 21);

polygon.addPoint(x + 20, y - 31);

polygon.addPoint(x + 30, y - 31);

polygon.addPoint(x + 40, y - 21);

if (x < z - 40)

{

g.setColor(Color.BLACK);

g.fillOval(x + 10, y - 11,10,10);

g.fillOval(x + 10, y - 11,10,10);

g.setColor(Color.BLUE);

g.fillRect(x , y - 21,50,10);

g.setColor(Color.GRAY);

g.fillPolygon(polygon);

g.setColor(Color.red);

}

else

x = 0;

if (c<z-86)

g.drawString(" ",c, y-51);

else

c = 0;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote