3

I'm trying to let a car image follow a path. The car has to "drive" at a constant speed and it has to look smooth. I'm able to let the car follow a list of points but I don't know how I can make the car drive smooth (the car moves to the next point in the list every 2 seconds now) with a constant speed and how to take hardcoded turns. Anyone who can help?

Code

Track class where the track underlay is loaded.

public class Track{

BufferedImage track;
Point trackPosition;
static final Point TRACK_POS = new Point(0, 0);
static final Point SENSOR_POS = new Point(250, 70);

public Track(){
    try {
        track = ImageIO.read(Roundabout.class.getResource("track.png"));
    } catch (Exception ex) {
        System.out.println("Problem loading track image: " + ex);
    }
    trackPosition=new Point(TRACK_POS.x,TRACK_POS.y);
}

  public void paint (Graphics g)
{
    g.drawImage(track,TRACK_POS.x, TRACK_POS.y, null);
}
}

Car class

public class Car extends JComponent implements Vehicle{

 BufferedImage car;
 Point carPosition;
 static final Point START_POS = new Point(10, 150);

 int counter=0;


public Car(){
    try {
        car = ImageIO.read(Car.class.getResource("beetle_red.gif"));
    } catch (Exception e) {
        System.out.println("Problem loading car images: " + e);
    }

    carPosition = new Point(START_POS.x, START_POS.y);

}

public void paint (Graphics g)
{

  g.drawImage(car,carPosition.x, carPosition.y, null); //original paint


}

  public Point getCarPosition() {
    return new Point(carPosition.x,carPosition.y);
}

public void update(){

    repaint();
    if(counter < Lane.firstLane.size()){
             carPosition.x = Lane.firstLane.get(counter).x;
             carPosition.y= Lane.firstLane.get(counter).y;
             System.out.println("Pos: "+getCarPosition());
             counter++;
     }
    else{
        System.out.println("Destination reached");
    }
    repaint();    
 }
}

Lane class

public class Lane {

public static List<Point> firstLane = new ArrayList<>(Arrays.asList(new Point(10,135),new Point(124,190),new Point(363,190),new Point(469,210)));

}

Roundabout (main class)

public class Roundabout extends JFrame{

Track track=new Track();
TrafficLight trafficLight=new TrafficLight();
Car car=new Car();
ArrayList<Car> cars = new ArrayList<>();


byte[] array=new byte[]{0,2,1,1}; //test byte array


class Surface extends JPanel {

private void doDrawing(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(Color.blue);

    Dimension size = getSize();
    Insets insets = getInsets();

    int w = size.width - insets.left - insets.right;
    int h = size.height - insets.top - insets.bottom;

    /* Draw the track first */
    track.paint(g);

    /* Draw a car */
    car.paint(g);
    cars.add(car); //add to list

    trafficLight.paint(g);

}

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);
    doDrawing(g);
}
}

public Roundabout(){
    initUI();
}

private void initUI() {

    setTitle("Roundabout");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(new Surface());

    setSize(580, 550);
    setLocationRelativeTo(null);
    moveCar();


}


public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {

            Roundabout roundabout=new Roundabout();
            roundabout.setVisible(true);
        }
    });

}

public void moveCar() {
    Runnable helloRunnable = new Runnable() {
        public void run() {

           car.update();
           repaint();


        }
    };

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    executor.scheduleAtFixedRate(helloRunnable, 0, 2, TimeUnit.SECONDS);
}
}
Sybren
  • 1,071
  • 3
  • 17
  • 51
  • Does `JFrame` has `paintComponent(...)`method? How can you override that in `Roundabout`class!!!! For `JComponent`override this method, not the `paint(...) – nIcE cOw Mar 02 '15 at 12:03
  • How can I fix that and can you help me with the questions in my OP? – Sybren Mar 02 '15 at 12:46
  • Just give me some time, creating one example (coming soon...). Instead of drawing, directly, try using `JLabel`` – nIcE cOw Mar 02 '15 at 13:00
  • Sorry, I was looking at the wrong question, and answered yours. My BAD :( – nIcE cOw Mar 02 '15 at 14:17

1 Answers1

0

Essentially, you need to move your car along a line segment, then another line segment, etc...

Here is an explanation of motion along a line

Relevant code, with t being in the range 0 to 1 (essentially t = percent of line crossed at said time):

x = xstart + (xend-xstart) * t y = ystart + (yend-ystart) * t

It's all math.

In order to determine what speed (as change in t) each segment needs to be cruised at, you'll need to determine its length.

The math is easy:

if t+=(1/10) per 1 second for 10px long line, then its speed is 1 px per second. For a line 339 px long, you would need 339 seconds and t+=(1/339).

Community
  • 1
  • 1
Laurel
  • 5,965
  • 14
  • 31
  • 57